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 /* 468*4c506522SGleb Smirnoff * Do the actual work of freeing a struct ifnet, and layer 2 common 469*4c506522SGleb Smirnoff * 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 47927d37320SRobert Watson if (if_com_free[ifp->if_alloctype] != NULL) 48027d37320SRobert Watson if_com_free[ifp->if_alloctype](ifp->if_l2com, 48127d37320SRobert Watson ifp->if_alloctype); 482fc74a9f9SBrooks Davis 483d6f157eaSRobert Watson #ifdef MAC 484d6f157eaSRobert Watson mac_ifnet_destroy(ifp); 485d6f157eaSRobert Watson #endif /* MAC */ 486215940b3SXin LI if (ifp->if_description != NULL) 487215940b3SXin LI free(ifp->if_description, M_IFDESCR); 488d6f157eaSRobert Watson IF_AFDATA_DESTROY(ifp); 48902f4879dSRobert Watson IF_ADDR_LOCK_DESTROY(ifp); 490d659538fSSam Leffler ifq_delete(&ifp->if_snd); 491fc74a9f9SBrooks Davis free(ifp, M_IFNET); 492c0c9ea90SSam Leffler } 493fc74a9f9SBrooks Davis 494242a8e72SRobert Watson /* 495242a8e72SRobert Watson * This version should only be called by intefaces that switch their type 496242a8e72SRobert Watson * after calling if_alloc(). if_free_type() will go away again now that we 497242a8e72SRobert Watson * have if_alloctype to cache the original allocation type. For now, assert 498242a8e72SRobert Watson * that they match, since we require that in practice. 499242a8e72SRobert Watson */ 500242a8e72SRobert Watson void 501242a8e72SRobert Watson if_free_type(struct ifnet *ifp, u_char type) 502242a8e72SRobert Watson { 503242a8e72SRobert Watson 504242a8e72SRobert Watson KASSERT(ifp->if_alloctype == type, 505242a8e72SRobert Watson ("if_free_type: type (%d) != alloctype (%d)", type, 506242a8e72SRobert Watson ifp->if_alloctype)); 507242a8e72SRobert Watson 508242a8e72SRobert Watson ifp->if_flags |= IFF_DYING; /* XXX: Locking */ 509*4c506522SGleb Smirnoff 510*4c506522SGleb Smirnoff IFNET_WLOCK(); 511*4c506522SGleb Smirnoff KASSERT(ifp == ifnet_byindex_locked(ifp->if_index), 512*4c506522SGleb Smirnoff ("%s: freeing unallocated ifnet", ifp->if_xname)); 513*4c506522SGleb Smirnoff 514*4c506522SGleb Smirnoff ifindex_free_locked(ifp->if_index); 515*4c506522SGleb Smirnoff IFNET_WUNLOCK(); 516*4c506522SGleb Smirnoff 517242a8e72SRobert Watson if (!refcount_release(&ifp->if_refcount)) 518242a8e72SRobert Watson return; 519242a8e72SRobert Watson if_free_internal(ifp); 520242a8e72SRobert Watson } 521242a8e72SRobert Watson 522242a8e72SRobert Watson /* 523242a8e72SRobert Watson * This is the normal version of if_free(), used by device drivers to free a 524242a8e72SRobert Watson * detached network interface. The contents of if_free_type() will move into 525242a8e72SRobert Watson * here when if_free_type() goes away. 526242a8e72SRobert Watson */ 527242a8e72SRobert Watson void 528242a8e72SRobert Watson if_free(struct ifnet *ifp) 529242a8e72SRobert Watson { 530242a8e72SRobert Watson 531242a8e72SRobert Watson if_free_type(ifp, ifp->if_alloctype); 532242a8e72SRobert Watson } 533242a8e72SRobert Watson 534242a8e72SRobert Watson /* 535242a8e72SRobert Watson * Interfaces to keep an ifnet type-stable despite the possibility of the 536242a8e72SRobert Watson * driver calling if_free(). If there are additional references, we defer 537242a8e72SRobert Watson * freeing the underlying data structure. 538242a8e72SRobert Watson */ 539db7f0b97SKip Macy void 54027d37320SRobert Watson if_ref(struct ifnet *ifp) 54127d37320SRobert Watson { 54227d37320SRobert Watson 54327d37320SRobert Watson /* We don't assert the ifnet list lock here, but arguably should. */ 54427d37320SRobert Watson refcount_acquire(&ifp->if_refcount); 54527d37320SRobert Watson } 54627d37320SRobert Watson 54727d37320SRobert Watson void 54827d37320SRobert Watson if_rele(struct ifnet *ifp) 54927d37320SRobert Watson { 55027d37320SRobert Watson 551242a8e72SRobert Watson if (!refcount_release(&ifp->if_refcount)) 552242a8e72SRobert Watson return; 553242a8e72SRobert Watson if_free_internal(ifp); 55427d37320SRobert Watson } 55527d37320SRobert Watson 55627d37320SRobert Watson void 557d659538fSSam Leffler ifq_init(struct ifaltq *ifq, struct ifnet *ifp) 558db7f0b97SKip Macy { 559db7f0b97SKip Macy 560db7f0b97SKip Macy mtx_init(&ifq->ifq_mtx, ifp->if_xname, "if send queue", MTX_DEF); 561db7f0b97SKip Macy 562db7f0b97SKip Macy if (ifq->ifq_maxlen == 0) 563db7f0b97SKip Macy ifq->ifq_maxlen = ifqmaxlen; 564db7f0b97SKip Macy 565db7f0b97SKip Macy ifq->altq_type = 0; 566db7f0b97SKip Macy ifq->altq_disc = NULL; 567db7f0b97SKip Macy ifq->altq_flags &= ALTQF_CANTCHANGE; 568db7f0b97SKip Macy ifq->altq_tbr = NULL; 569db7f0b97SKip Macy ifq->altq_ifp = ifp; 570db7f0b97SKip Macy } 571db7f0b97SKip Macy 572db7f0b97SKip Macy void 573d659538fSSam Leffler ifq_delete(struct ifaltq *ifq) 574db7f0b97SKip Macy { 575db7f0b97SKip Macy mtx_destroy(&ifq->ifq_mtx); 576db7f0b97SKip Macy } 577db7f0b97SKip Macy 578fc74a9f9SBrooks Davis /* 579a45cbf12SBrooks Davis * Perform generic interface initalization tasks and attach the interface 580e0c14af9SMarko Zec * to the list of "active" interfaces. If vmove flag is set on entry 581e0c14af9SMarko Zec * to if_attach_internal(), perform only a limited subset of initialization 582e0c14af9SMarko Zec * tasks, given that we are moving from one vnet to another an ifnet which 583e0c14af9SMarko Zec * has already been fully initialized. 584a45cbf12SBrooks Davis * 585a45cbf12SBrooks Davis * XXX: 586a45cbf12SBrooks Davis * - The decision to return void and thus require this function to 587a45cbf12SBrooks Davis * succeed is questionable. 588a45cbf12SBrooks Davis * - We should probably do more sanity checking. For instance we don't 589a45cbf12SBrooks Davis * do anything to insure if_xname is unique or non-empty. 590df8bae1dSRodney W. Grimes */ 591df8bae1dSRodney W. Grimes void 59272fd1b6aSDag-Erling Smørgrav if_attach(struct ifnet *ifp) 593df8bae1dSRodney W. Grimes { 594e0c14af9SMarko Zec 595e0c14af9SMarko Zec if_attach_internal(ifp, 0); 596e0c14af9SMarko Zec } 597e0c14af9SMarko Zec 598e0c14af9SMarko Zec static void 599e0c14af9SMarko Zec if_attach_internal(struct ifnet *ifp, int vmove) 600e0c14af9SMarko Zec { 601df8bae1dSRodney W. Grimes unsigned socksize, ifasize; 6021ce9bf88SPoul-Henning Kamp int namelen, masklen; 60372fd1b6aSDag-Erling Smørgrav struct sockaddr_dl *sdl; 60472fd1b6aSDag-Erling Smørgrav struct ifaddr *ifa; 605df8bae1dSRodney W. Grimes 606fc74a9f9SBrooks Davis if (ifp->if_index == 0 || ifp != ifnet_byindex(ifp->if_index)) 607fc74a9f9SBrooks Davis panic ("%s: BUG: if_attach called without if_alloc'd input()\n", 608fc74a9f9SBrooks Davis ifp->if_xname); 609fc74a9f9SBrooks Davis 610f6dfe47aSMarko Zec #ifdef VIMAGE 611f6dfe47aSMarko Zec ifp->if_vnet = curvnet; 612bc29160dSMarko Zec if (ifp->if_home_vnet == NULL) 613bc29160dSMarko Zec ifp->if_home_vnet = curvnet; 614f6dfe47aSMarko Zec #endif 615f6dfe47aSMarko Zec 6160dad3f0eSMax Laier if_addgroup(ifp, IFG_ALL); 6170dad3f0eSMax Laier 61898b9590eSPoul-Henning Kamp getmicrotime(&ifp->if_lastchange); 619bc9d2991SBrooks Davis ifp->if_data.ifi_epoch = time_uptime; 620fc74a9f9SBrooks Davis ifp->if_data.ifi_datalen = sizeof(struct if_data); 621d6f157eaSRobert Watson 6227cc5b47fSKip Macy KASSERT((ifp->if_transmit == NULL && ifp->if_qflush == NULL) || 6237cc5b47fSKip Macy (ifp->if_transmit != NULL && ifp->if_qflush != NULL), 6247cc5b47fSKip Macy ("transmit and qflush must both either be set or both be NULL")); 6257cc5b47fSKip Macy if (ifp->if_transmit == NULL) { 626db7f0b97SKip Macy ifp->if_transmit = if_transmit; 627db7f0b97SKip Macy ifp->if_qflush = if_qflush; 6287cc5b47fSKip Macy } 6297cc5b47fSKip Macy 630e0c14af9SMarko Zec if (!vmove) { 631e70cd263SRobert Watson #ifdef MAC 63230d239bcSRobert Watson mac_ifnet_create(ifp); 633e70cd263SRobert Watson #endif 634e70cd263SRobert Watson 635df8bae1dSRodney W. Grimes /* 636e0c14af9SMarko Zec * Create a Link Level name for this device. 637df8bae1dSRodney W. Grimes */ 6389bf40edeSBrooks Davis namelen = strlen(ifp->if_xname); 63936c19a57SBrooks Davis /* 640e0c14af9SMarko Zec * Always save enough space for any possiable name so we 641e0c14af9SMarko Zec * can do a rename in place later. 64236c19a57SBrooks Davis */ 64336c19a57SBrooks Davis masklen = offsetof(struct sockaddr_dl, sdl_data[0]) + IFNAMSIZ; 644df8bae1dSRodney W. Grimes socksize = masklen + ifp->if_addrlen; 645df8bae1dSRodney W. Grimes if (socksize < sizeof(*sdl)) 646df8bae1dSRodney W. Grimes socksize = sizeof(*sdl); 647ccb82468SBrooks Davis socksize = roundup2(socksize, sizeof(long)); 648df8bae1dSRodney W. Grimes ifasize = sizeof(*ifa) + 2 * socksize; 649a8773564SBrooks Davis ifa = malloc(ifasize, M_IFADDR, M_WAITOK | M_ZERO); 6501099f828SRobert Watson ifa_init(ifa); 651df8bae1dSRodney W. Grimes sdl = (struct sockaddr_dl *)(ifa + 1); 652df8bae1dSRodney W. Grimes sdl->sdl_len = socksize; 653df8bae1dSRodney W. Grimes sdl->sdl_family = AF_LINK; 6549bf40edeSBrooks Davis bcopy(ifp->if_xname, sdl->sdl_data, namelen); 6551ce9bf88SPoul-Henning Kamp sdl->sdl_nlen = namelen; 656df8bae1dSRodney W. Grimes sdl->sdl_index = ifp->if_index; 657df8bae1dSRodney W. Grimes sdl->sdl_type = ifp->if_type; 6584a0d6638SRuslan Ermilov ifp->if_addr = ifa; 659df8bae1dSRodney W. Grimes ifa->ifa_ifp = ifp; 660df8bae1dSRodney W. Grimes ifa->ifa_rtrequest = link_rtrequest; 661df8bae1dSRodney W. Grimes ifa->ifa_addr = (struct sockaddr *)sdl; 662df8bae1dSRodney W. Grimes sdl = (struct sockaddr_dl *)(socksize + (caddr_t)sdl); 663df8bae1dSRodney W. Grimes ifa->ifa_netmask = (struct sockaddr *)sdl; 664df8bae1dSRodney W. Grimes sdl->sdl_len = masklen; 665df8bae1dSRodney W. Grimes while (namelen != 0) 666df8bae1dSRodney W. Grimes sdl->sdl_data[--namelen] = 0xff; 66759562606SGarrett Wollman TAILQ_INSERT_HEAD(&ifp->if_addrhead, ifa, ifa_link); 668e0c14af9SMarko Zec /* Reliably crash if used uninitialized. */ 669e0c14af9SMarko Zec ifp->if_broadcastaddr = NULL; 670e0c14af9SMarko Zec } 67152db6805SMarko Zec #ifdef VIMAGE 67252db6805SMarko Zec else { 67352db6805SMarko Zec /* 67452db6805SMarko Zec * Update the interface index in the link layer address 67552db6805SMarko Zec * of the interface. 67652db6805SMarko Zec */ 67752db6805SMarko Zec for (ifa = ifp->if_addr; ifa != NULL; 67852db6805SMarko Zec ifa = TAILQ_NEXT(ifa, ifa_link)) { 67952db6805SMarko Zec if (ifa->ifa_addr->sa_family == AF_LINK) { 68052db6805SMarko Zec sdl = (struct sockaddr_dl *)ifa->ifa_addr; 68152db6805SMarko Zec sdl->sdl_index = ifp->if_index; 68252db6805SMarko Zec } 68352db6805SMarko Zec } 68452db6805SMarko Zec } 68552db6805SMarko Zec #endif 686d94ccb09SBrooks Davis 687457f48e6SGleb Smirnoff IFNET_WLOCK(); 688603724d3SBjoern A. Zeeb TAILQ_INSERT_TAIL(&V_ifnet, ifp, if_link); 68921ca7b57SMarko Zec #ifdef VIMAGE 690006e9db4SRobert Watson curvnet->vnet_ifcnt++; 69121ca7b57SMarko Zec #endif 692457f48e6SGleb Smirnoff IFNET_WUNLOCK(); 693457f48e6SGleb Smirnoff 69469fb23b7SMax Laier if (domain_init_status >= 2) 69531b1bfe1SHajimu UMEMOTO if_attachdomain1(ifp); 69631b1bfe1SHajimu UMEMOTO 69725a4adceSMax Laier EVENTHANDLER_INVOKE(ifnet_arrival_event, ifp); 69821ca7b57SMarko Zec if (IS_DEFAULT_VNET(curvnet)) 699f3b90d48SAndrew Thompson devctl_notify("IFNET", ifp->if_xname, "ATTACH", NULL); 70025a4adceSMax Laier 7017b6edd04SRuslan Ermilov /* Announce the interface. */ 7027b6edd04SRuslan Ermilov rt_ifannouncemsg(ifp, IFAN_ARRIVAL); 703df8bae1dSRodney W. Grimes } 7046182fdbdSPeter Wemm 70531b1bfe1SHajimu UMEMOTO static void 70672fd1b6aSDag-Erling Smørgrav if_attachdomain(void *dummy) 70731b1bfe1SHajimu UMEMOTO { 70831b1bfe1SHajimu UMEMOTO struct ifnet *ifp; 70931b1bfe1SHajimu UMEMOTO int s; 71031b1bfe1SHajimu UMEMOTO 71131b1bfe1SHajimu UMEMOTO s = splnet(); 712603724d3SBjoern A. Zeeb TAILQ_FOREACH(ifp, &V_ifnet, if_link) 71331b1bfe1SHajimu UMEMOTO if_attachdomain1(ifp); 71431b1bfe1SHajimu UMEMOTO splx(s); 71531b1bfe1SHajimu UMEMOTO } 71669fb23b7SMax Laier SYSINIT(domainifattach, SI_SUB_PROTO_IFATTACHDOMAIN, SI_ORDER_SECOND, 71731b1bfe1SHajimu UMEMOTO if_attachdomain, NULL); 71831b1bfe1SHajimu UMEMOTO 71931b1bfe1SHajimu UMEMOTO static void 72072fd1b6aSDag-Erling Smørgrav if_attachdomain1(struct ifnet *ifp) 72131b1bfe1SHajimu UMEMOTO { 72231b1bfe1SHajimu UMEMOTO struct domain *dp; 72331b1bfe1SHajimu UMEMOTO int s; 72431b1bfe1SHajimu UMEMOTO 72531b1bfe1SHajimu UMEMOTO s = splnet(); 72631b1bfe1SHajimu UMEMOTO 727234a35c7SHajimu UMEMOTO /* 728234a35c7SHajimu UMEMOTO * Since dp->dom_ifattach calls malloc() with M_WAITOK, we 729234a35c7SHajimu UMEMOTO * cannot lock ifp->if_afdata initialization, entirely. 730234a35c7SHajimu UMEMOTO */ 731234a35c7SHajimu UMEMOTO if (IF_AFDATA_TRYLOCK(ifp) == 0) { 732234a35c7SHajimu UMEMOTO splx(s); 733234a35c7SHajimu UMEMOTO return; 734234a35c7SHajimu UMEMOTO } 73569fb23b7SMax Laier if (ifp->if_afdata_initialized >= domain_init_status) { 736234a35c7SHajimu UMEMOTO IF_AFDATA_UNLOCK(ifp); 737234a35c7SHajimu UMEMOTO splx(s); 7386237419dSRobert Watson printf("if_attachdomain called more than once on %s\n", 7396237419dSRobert Watson ifp->if_xname); 740234a35c7SHajimu UMEMOTO return; 741234a35c7SHajimu UMEMOTO } 74269fb23b7SMax Laier ifp->if_afdata_initialized = domain_init_status; 743234a35c7SHajimu UMEMOTO IF_AFDATA_UNLOCK(ifp); 744234a35c7SHajimu UMEMOTO 74531b1bfe1SHajimu UMEMOTO /* address family dependent data region */ 74631b1bfe1SHajimu UMEMOTO bzero(ifp->if_afdata, sizeof(ifp->if_afdata)); 74731b1bfe1SHajimu UMEMOTO for (dp = domains; dp; dp = dp->dom_next) { 74831b1bfe1SHajimu UMEMOTO if (dp->dom_ifattach) 74931b1bfe1SHajimu UMEMOTO ifp->if_afdata[dp->dom_family] = 75031b1bfe1SHajimu UMEMOTO (*dp->dom_ifattach)(ifp); 75131b1bfe1SHajimu UMEMOTO } 75231b1bfe1SHajimu UMEMOTO 75331b1bfe1SHajimu UMEMOTO splx(s); 75431b1bfe1SHajimu UMEMOTO } 75531b1bfe1SHajimu UMEMOTO 7566182fdbdSPeter Wemm /* 757ec002feeSBruce M Simpson * Remove any unicast or broadcast network addresses from an interface. 75845778b37SPeter Edwards */ 75945778b37SPeter Edwards void 76045778b37SPeter Edwards if_purgeaddrs(struct ifnet *ifp) 76145778b37SPeter Edwards { 76245778b37SPeter Edwards struct ifaddr *ifa, *next; 76345778b37SPeter Edwards 76445778b37SPeter Edwards TAILQ_FOREACH_SAFE(ifa, &ifp->if_addrhead, ifa_link, next) { 7654b97d7afSYaroslav Tykhiy if (ifa->ifa_addr->sa_family == AF_LINK) 76645778b37SPeter Edwards continue; 76745778b37SPeter Edwards #ifdef INET 76845778b37SPeter Edwards /* XXX: Ugly!! ad hoc just for INET */ 7694b97d7afSYaroslav Tykhiy if (ifa->ifa_addr->sa_family == AF_INET) { 77045778b37SPeter Edwards struct ifaliasreq ifr; 77145778b37SPeter Edwards 77245778b37SPeter Edwards bzero(&ifr, sizeof(ifr)); 77345778b37SPeter Edwards ifr.ifra_addr = *ifa->ifa_addr; 77445778b37SPeter Edwards if (ifa->ifa_dstaddr) 77545778b37SPeter Edwards ifr.ifra_broadaddr = *ifa->ifa_dstaddr; 77645778b37SPeter Edwards if (in_control(NULL, SIOCDIFADDR, (caddr_t)&ifr, ifp, 77745778b37SPeter Edwards NULL) == 0) 77845778b37SPeter Edwards continue; 77945778b37SPeter Edwards } 78045778b37SPeter Edwards #endif /* INET */ 78145778b37SPeter Edwards #ifdef INET6 7824b97d7afSYaroslav Tykhiy if (ifa->ifa_addr->sa_family == AF_INET6) { 78345778b37SPeter Edwards in6_purgeaddr(ifa); 78445778b37SPeter Edwards /* ifp_addrhead is already updated */ 78545778b37SPeter Edwards continue; 78645778b37SPeter Edwards } 78745778b37SPeter Edwards #endif /* INET6 */ 78845778b37SPeter Edwards TAILQ_REMOVE(&ifp->if_addrhead, ifa, ifa_link); 7891099f828SRobert Watson ifa_free(ifa); 79045778b37SPeter Edwards } 79145778b37SPeter Edwards } 79245778b37SPeter Edwards 79345778b37SPeter Edwards /* 79493ec7edcSShteryana Shopova * Remove any multicast network addresses from an interface when an ifnet 79593ec7edcSShteryana Shopova * is going away. 796ec002feeSBruce M Simpson */ 79793ec7edcSShteryana Shopova static void 798ec002feeSBruce M Simpson if_purgemaddrs(struct ifnet *ifp) 799ec002feeSBruce M Simpson { 800ec002feeSBruce M Simpson struct ifmultiaddr *ifma; 801ec002feeSBruce M Simpson struct ifmultiaddr *next; 802ec002feeSBruce M Simpson 803ec002feeSBruce M Simpson IF_ADDR_LOCK(ifp); 804ec002feeSBruce M Simpson TAILQ_FOREACH_SAFE(ifma, &ifp->if_multiaddrs, ifma_link, next) 805ec002feeSBruce M Simpson if_delmulti_locked(ifp, ifma, 1); 806ec002feeSBruce M Simpson IF_ADDR_UNLOCK(ifp); 807ec002feeSBruce M Simpson } 808ec002feeSBruce M Simpson 809ec002feeSBruce M Simpson /* 810e0c14af9SMarko Zec * Detach an interface, removing it from the list of "active" interfaces. 811e0c14af9SMarko Zec * If vmove flag is set on entry to if_detach_internal(), perform only a 812e0c14af9SMarko Zec * limited subset of cleanup tasks, given that we are moving an ifnet from 813e0c14af9SMarko Zec * one vnet to another, where it must be fully operational. 814b1c53bc9SRobert Watson * 815b1c53bc9SRobert Watson * XXXRW: There are some significant questions about event ordering, and 816b1c53bc9SRobert Watson * how to prevent things from starting to use the interface during detach. 8176182fdbdSPeter Wemm */ 8186182fdbdSPeter Wemm void 81972fd1b6aSDag-Erling Smørgrav if_detach(struct ifnet *ifp) 8206182fdbdSPeter Wemm { 821e0c14af9SMarko Zec 822e0c14af9SMarko Zec if_detach_internal(ifp, 0); 823e0c14af9SMarko Zec } 824e0c14af9SMarko Zec 825e0c14af9SMarko Zec static void 826e0c14af9SMarko Zec if_detach_internal(struct ifnet *ifp, int vmove) 827e0c14af9SMarko Zec { 82845778b37SPeter Edwards struct ifaddr *ifa; 8295500d3beSWarner Losh struct radix_node_head *rnh; 830e0c14af9SMarko Zec int i, j; 83131b1bfe1SHajimu UMEMOTO struct domain *dp; 8323f35d515SPeter Pentchev struct ifnet *iter; 833457f48e6SGleb Smirnoff int found = 0; 834457f48e6SGleb Smirnoff 835457f48e6SGleb Smirnoff IFNET_WLOCK(); 836603724d3SBjoern A. Zeeb TAILQ_FOREACH(iter, &V_ifnet, if_link) 837457f48e6SGleb Smirnoff if (iter == ifp) { 838603724d3SBjoern A. Zeeb TAILQ_REMOVE(&V_ifnet, ifp, if_link); 839457f48e6SGleb Smirnoff found = 1; 840457f48e6SGleb Smirnoff break; 841457f48e6SGleb Smirnoff } 84221ca7b57SMarko Zec #ifdef VIMAGE 84321ca7b57SMarko Zec if (found) 844006e9db4SRobert Watson curvnet->vnet_ifcnt--; 84521ca7b57SMarko Zec #endif 846457f48e6SGleb Smirnoff IFNET_WUNLOCK(); 847e0c14af9SMarko Zec if (!found) { 848e0c14af9SMarko Zec if (vmove) 84958606037SBjoern A. Zeeb panic("%s: ifp=%p not on the ifnet tailq %p", 85058606037SBjoern A. Zeeb __func__, ifp, &V_ifnet); 851e0c14af9SMarko Zec else 852e0c14af9SMarko Zec return; /* XXX this should panic as well? */ 853e0c14af9SMarko Zec } 8546182fdbdSPeter Wemm 85568a3482fSGleb Smirnoff /* 85668a3482fSGleb Smirnoff * Remove/wait for pending events. 85768a3482fSGleb Smirnoff */ 85868a3482fSGleb Smirnoff taskqueue_drain(taskqueue_swi, &ifp->if_linktask); 85968a3482fSGleb Smirnoff 8606182fdbdSPeter Wemm /* 8616182fdbdSPeter Wemm * Remove routes and flush queues. 8626182fdbdSPeter Wemm */ 8636182fdbdSPeter Wemm if_down(ifp); 86402b199f1SMax Laier #ifdef ALTQ 86502b199f1SMax Laier if (ALTQ_IS_ENABLED(&ifp->if_snd)) 86602b199f1SMax Laier altq_disable(&ifp->if_snd); 86702b199f1SMax Laier if (ALTQ_IS_ATTACHED(&ifp->if_snd)) 86802b199f1SMax Laier altq_detach(&ifp->if_snd); 86902b199f1SMax Laier #endif 8706182fdbdSPeter Wemm 87145778b37SPeter Edwards if_purgeaddrs(ifp); 8726182fdbdSPeter Wemm 873b1c53bc9SRobert Watson #ifdef INET 874b1c53bc9SRobert Watson in_ifdetach(ifp); 875b1c53bc9SRobert Watson #endif 876b1c53bc9SRobert Watson 87733841545SHajimu UMEMOTO #ifdef INET6 87833841545SHajimu UMEMOTO /* 87933841545SHajimu UMEMOTO * Remove all IPv6 kernel structs related to ifp. This should be done 88033841545SHajimu UMEMOTO * before removing routing entries below, since IPv6 interface direct 88133841545SHajimu UMEMOTO * routes are expected to be removed by the IPv6-specific kernel API. 88233841545SHajimu UMEMOTO * Otherwise, the kernel will detect some inconsistency and bark it. 88333841545SHajimu UMEMOTO */ 88433841545SHajimu UMEMOTO in6_ifdetach(ifp); 88533841545SHajimu UMEMOTO #endif 886ec002feeSBruce M Simpson if_purgemaddrs(ifp); 887ec002feeSBruce M Simpson 888e0c14af9SMarko Zec if (!vmove) { 889f4247b59SLuigi Rizzo /* 890111c6b61SRobert Watson * Prevent further calls into the device driver via ifnet. 891111c6b61SRobert Watson */ 892111c6b61SRobert Watson if_dead(ifp); 893111c6b61SRobert Watson 894111c6b61SRobert Watson /* 8954a0d6638SRuslan Ermilov * Remove link ifaddr pointer and maybe decrement if_index. 896f4247b59SLuigi Rizzo * Clean up all addresses. 897f4247b59SLuigi Rizzo */ 8984a0d6638SRuslan Ermilov ifp->if_addr = NULL; 899f4247b59SLuigi Rizzo 900212bd869SHajimu UMEMOTO /* We can now free link ifaddr. */ 9013f35d515SPeter Pentchev if (!TAILQ_EMPTY(&ifp->if_addrhead)) { 902212bd869SHajimu UMEMOTO ifa = TAILQ_FIRST(&ifp->if_addrhead); 903212bd869SHajimu UMEMOTO TAILQ_REMOVE(&ifp->if_addrhead, ifa, ifa_link); 9041099f828SRobert Watson ifa_free(ifa); 9053f35d515SPeter Pentchev } 906e0c14af9SMarko Zec } 907212bd869SHajimu UMEMOTO 9085500d3beSWarner Losh /* 9095500d3beSWarner Losh * Delete all remaining routes using this interface 9105500d3beSWarner Losh * Unfortuneatly the only way to do this is to slog through 9115500d3beSWarner Losh * the entire routing table looking for routes which point 9125500d3beSWarner Losh * to this interface...oh well... 9135500d3beSWarner Losh */ 9145500d3beSWarner Losh for (i = 1; i <= AF_MAX; i++) { 9158b07e49aSJulian Elischer for (j = 0; j < rt_numfibs; j++) { 916c2c2a7c1SBjoern A. Zeeb rnh = rt_tables_get_rnh(j, i); 917c2c2a7c1SBjoern A. Zeeb if (rnh == NULL) 9185500d3beSWarner Losh continue; 919956b0b65SJeffrey Hsu RADIX_NODE_HEAD_LOCK(rnh); 9205500d3beSWarner Losh (void) rnh->rnh_walktree(rnh, if_rtdel, ifp); 921956b0b65SJeffrey Hsu RADIX_NODE_HEAD_UNLOCK(rnh); 9225500d3beSWarner Losh } 9238b07e49aSJulian Elischer } 9245500d3beSWarner Losh 9257b6edd04SRuslan Ermilov /* Announce that the interface is gone. */ 9267b6edd04SRuslan Ermilov rt_ifannouncemsg(ifp, IFAN_DEPARTURE); 92752023244SMax Laier EVENTHANDLER_INVOKE(ifnet_departure_event, ifp); 92821ca7b57SMarko Zec if (IS_DEFAULT_VNET(curvnet)) 929f3b90d48SAndrew Thompson devctl_notify("IFNET", ifp->if_xname, "DETACH", NULL); 9308623f9fdSMax Laier if_delgroups(ifp); 9317b6edd04SRuslan Ermilov 932d8c13659SBjoern A. Zeeb /* 933d8c13659SBjoern A. Zeeb * We cannot hold the lock over dom_ifdetach calls as they might 934d8c13659SBjoern A. Zeeb * sleep, for example trying to drain a callout, thus open up the 935d8c13659SBjoern A. Zeeb * theoretical race with re-attaching. 936d8c13659SBjoern A. Zeeb */ 937234a35c7SHajimu UMEMOTO IF_AFDATA_LOCK(ifp); 938d8c13659SBjoern A. Zeeb i = ifp->if_afdata_initialized; 939d8c13659SBjoern A. Zeeb ifp->if_afdata_initialized = 0; 940d8c13659SBjoern A. Zeeb IF_AFDATA_UNLOCK(ifp); 941d8c13659SBjoern A. Zeeb for (dp = domains; i > 0 && dp; dp = dp->dom_next) { 94231b1bfe1SHajimu UMEMOTO if (dp->dom_ifdetach && ifp->if_afdata[dp->dom_family]) 94331b1bfe1SHajimu UMEMOTO (*dp->dom_ifdetach)(ifp, 94431b1bfe1SHajimu UMEMOTO ifp->if_afdata[dp->dom_family]); 94531b1bfe1SHajimu UMEMOTO } 9465500d3beSWarner Losh } 9475500d3beSWarner Losh 948e0c14af9SMarko Zec #ifdef VIMAGE 949e0c14af9SMarko Zec /* 950e0c14af9SMarko Zec * if_vmove() performs a limited version of if_detach() in current 951e0c14af9SMarko Zec * vnet and if_attach()es the ifnet to the vnet specified as 2nd arg. 952e0c14af9SMarko Zec * An attempt is made to shrink if_index in current vnet, find an 953e0c14af9SMarko Zec * unused if_index in target vnet and calls if_grow() if necessary, 954e0c14af9SMarko Zec * and finally find an unused if_xname for the target vnet. 955e0c14af9SMarko Zec */ 956e0c14af9SMarko Zec void 957e0c14af9SMarko Zec if_vmove(struct ifnet *ifp, struct vnet *new_vnet) 958e0c14af9SMarko Zec { 95961f6986bSRobert Watson u_short idx; 960e0c14af9SMarko Zec 961e0c14af9SMarko Zec /* 962e0c14af9SMarko Zec * Detach from current vnet, but preserve LLADDR info, do not 963e0c14af9SMarko Zec * mark as dead etc. so that the ifnet can be reattached later. 964e0c14af9SMarko Zec */ 965e0c14af9SMarko Zec if_detach_internal(ifp, 1); 966e0c14af9SMarko Zec 967e0c14af9SMarko Zec /* 96877dfcdc4SRobert Watson * Unlink the ifnet from ifindex_table[] in current vnet, and shrink 96977dfcdc4SRobert Watson * the if_index for that vnet if possible. 97077dfcdc4SRobert Watson * 97177dfcdc4SRobert Watson * NOTE: IFNET_WLOCK/IFNET_WUNLOCK() are assumed to be unvirtualized, 97277dfcdc4SRobert Watson * or we'd lock on one vnet and unlock on another. 973e0c14af9SMarko Zec */ 974e0c14af9SMarko Zec IFNET_WLOCK(); 975ed2dabfcSRobert Watson ifindex_free_locked(ifp->if_index); 976d3c351c5SMarko Zec IFNET_WUNLOCK(); 977d3c351c5SMarko Zec 978d3c351c5SMarko Zec /* 979d3c351c5SMarko Zec * Perform interface-specific reassignment tasks, if provided by 980d3c351c5SMarko Zec * the driver. 981d3c351c5SMarko Zec */ 982d3c351c5SMarko Zec if (ifp->if_reassign != NULL) 983d3c351c5SMarko Zec ifp->if_reassign(ifp, new_vnet, NULL); 984e0c14af9SMarko Zec 985e0c14af9SMarko Zec /* 986e0c14af9SMarko Zec * Switch to the context of the target vnet. 987e0c14af9SMarko Zec */ 988e0c14af9SMarko Zec CURVNET_SET_QUIET(new_vnet); 989e0c14af9SMarko Zec 990d3c351c5SMarko Zec IFNET_WLOCK(); 991ed2dabfcSRobert Watson if (ifindex_alloc_locked(&idx) != 0) { 99261f6986bSRobert Watson IFNET_WUNLOCK(); 993e0c14af9SMarko Zec panic("if_index overflow"); 99461f6986bSRobert Watson } 99561f6986bSRobert Watson ifp->if_index = idx; 99677dfcdc4SRobert Watson ifnet_setbyindex_locked(ifp->if_index, ifp); 997e0c14af9SMarko Zec IFNET_WUNLOCK(); 998e0c14af9SMarko Zec 999e0c14af9SMarko Zec if_attach_internal(ifp, 1); 1000e0c14af9SMarko Zec 1001e0c14af9SMarko Zec CURVNET_RESTORE(); 1002e0c14af9SMarko Zec } 1003be31e5e7SBjoern A. Zeeb 1004be31e5e7SBjoern A. Zeeb /* 1005be31e5e7SBjoern A. Zeeb * Move an ifnet to or from another child prison/vnet, specified by the jail id. 1006be31e5e7SBjoern A. Zeeb */ 1007be31e5e7SBjoern A. Zeeb static int 1008be31e5e7SBjoern A. Zeeb if_vmove_loan(struct thread *td, struct ifnet *ifp, char *ifname, int jid) 1009be31e5e7SBjoern A. Zeeb { 1010be31e5e7SBjoern A. Zeeb struct prison *pr; 1011be31e5e7SBjoern A. Zeeb struct ifnet *difp; 1012be31e5e7SBjoern A. Zeeb 1013be31e5e7SBjoern A. Zeeb /* Try to find the prison within our visibility. */ 1014be31e5e7SBjoern A. Zeeb sx_slock(&allprison_lock); 1015be31e5e7SBjoern A. Zeeb pr = prison_find_child(td->td_ucred->cr_prison, jid); 1016be31e5e7SBjoern A. Zeeb sx_sunlock(&allprison_lock); 1017be31e5e7SBjoern A. Zeeb if (pr == NULL) 1018be31e5e7SBjoern A. Zeeb return (ENXIO); 1019be31e5e7SBjoern A. Zeeb prison_hold_locked(pr); 1020be31e5e7SBjoern A. Zeeb mtx_unlock(&pr->pr_mtx); 1021be31e5e7SBjoern A. Zeeb 1022be31e5e7SBjoern A. Zeeb /* Do not try to move the iface from and to the same prison. */ 1023be31e5e7SBjoern A. Zeeb if (pr->pr_vnet == ifp->if_vnet) { 1024be31e5e7SBjoern A. Zeeb prison_free(pr); 1025be31e5e7SBjoern A. Zeeb return (EEXIST); 1026be31e5e7SBjoern A. Zeeb } 1027be31e5e7SBjoern A. Zeeb 1028be31e5e7SBjoern A. Zeeb /* Make sure the named iface does not exists in the dst. prison/vnet. */ 1029be31e5e7SBjoern A. Zeeb /* XXX Lock interfaces to avoid races. */ 10309abb4862SMarko Zec CURVNET_SET_QUIET(pr->pr_vnet); 1031be31e5e7SBjoern A. Zeeb difp = ifunit(ifname); 1032be31e5e7SBjoern A. Zeeb CURVNET_RESTORE(); 1033be31e5e7SBjoern A. Zeeb if (difp != NULL) { 1034be31e5e7SBjoern A. Zeeb prison_free(pr); 1035be31e5e7SBjoern A. Zeeb return (EEXIST); 1036be31e5e7SBjoern A. Zeeb } 1037be31e5e7SBjoern A. Zeeb 1038be31e5e7SBjoern A. Zeeb /* Move the interface into the child jail/vnet. */ 1039be31e5e7SBjoern A. Zeeb if_vmove(ifp, pr->pr_vnet); 1040be31e5e7SBjoern A. Zeeb 1041be31e5e7SBjoern A. Zeeb /* Report the new if_xname back to the userland. */ 1042be31e5e7SBjoern A. Zeeb sprintf(ifname, "%s", ifp->if_xname); 1043be31e5e7SBjoern A. Zeeb 1044be31e5e7SBjoern A. Zeeb prison_free(pr); 1045be31e5e7SBjoern A. Zeeb return (0); 1046be31e5e7SBjoern A. Zeeb } 1047be31e5e7SBjoern A. Zeeb 1048be31e5e7SBjoern A. Zeeb static int 1049be31e5e7SBjoern A. Zeeb if_vmove_reclaim(struct thread *td, char *ifname, int jid) 1050be31e5e7SBjoern A. Zeeb { 1051be31e5e7SBjoern A. Zeeb struct prison *pr; 1052be31e5e7SBjoern A. Zeeb struct vnet *vnet_dst; 1053be31e5e7SBjoern A. Zeeb struct ifnet *ifp; 1054be31e5e7SBjoern A. Zeeb 1055be31e5e7SBjoern A. Zeeb /* Try to find the prison within our visibility. */ 1056be31e5e7SBjoern A. Zeeb sx_slock(&allprison_lock); 1057be31e5e7SBjoern A. Zeeb pr = prison_find_child(td->td_ucred->cr_prison, jid); 1058be31e5e7SBjoern A. Zeeb sx_sunlock(&allprison_lock); 1059be31e5e7SBjoern A. Zeeb if (pr == NULL) 1060be31e5e7SBjoern A. Zeeb return (ENXIO); 1061be31e5e7SBjoern A. Zeeb prison_hold_locked(pr); 1062be31e5e7SBjoern A. Zeeb mtx_unlock(&pr->pr_mtx); 1063be31e5e7SBjoern A. Zeeb 1064be31e5e7SBjoern A. Zeeb /* Make sure the named iface exists in the source prison/vnet. */ 1065be31e5e7SBjoern A. Zeeb CURVNET_SET(pr->pr_vnet); 1066be31e5e7SBjoern A. Zeeb ifp = ifunit(ifname); /* XXX Lock to avoid races. */ 1067be31e5e7SBjoern A. Zeeb if (ifp == NULL) { 1068be31e5e7SBjoern A. Zeeb CURVNET_RESTORE(); 1069be31e5e7SBjoern A. Zeeb prison_free(pr); 1070be31e5e7SBjoern A. Zeeb return (ENXIO); 1071be31e5e7SBjoern A. Zeeb } 1072be31e5e7SBjoern A. Zeeb 1073be31e5e7SBjoern A. Zeeb /* Do not try to move the iface from and to the same prison. */ 1074be31e5e7SBjoern A. Zeeb vnet_dst = TD_TO_VNET(td); 1075be31e5e7SBjoern A. Zeeb if (vnet_dst == ifp->if_vnet) { 1076be31e5e7SBjoern A. Zeeb CURVNET_RESTORE(); 1077be31e5e7SBjoern A. Zeeb prison_free(pr); 1078be31e5e7SBjoern A. Zeeb return (EEXIST); 1079be31e5e7SBjoern A. Zeeb } 1080be31e5e7SBjoern A. Zeeb 1081be31e5e7SBjoern A. Zeeb /* Get interface back from child jail/vnet. */ 1082be31e5e7SBjoern A. Zeeb if_vmove(ifp, vnet_dst); 1083be31e5e7SBjoern A. Zeeb CURVNET_RESTORE(); 1084be31e5e7SBjoern A. Zeeb 1085be31e5e7SBjoern A. Zeeb /* Report the new if_xname back to the userland. */ 1086be31e5e7SBjoern A. Zeeb sprintf(ifname, "%s", ifp->if_xname); 1087be31e5e7SBjoern A. Zeeb 1088be31e5e7SBjoern A. Zeeb prison_free(pr); 1089be31e5e7SBjoern A. Zeeb return (0); 1090be31e5e7SBjoern A. Zeeb } 1091e0c14af9SMarko Zec #endif /* VIMAGE */ 1092e0c14af9SMarko Zec 10935500d3beSWarner Losh /* 10940dad3f0eSMax Laier * Add a group to an interface 10950dad3f0eSMax Laier */ 10960dad3f0eSMax Laier int 10970dad3f0eSMax Laier if_addgroup(struct ifnet *ifp, const char *groupname) 10980dad3f0eSMax Laier { 10990dad3f0eSMax Laier struct ifg_list *ifgl; 11000dad3f0eSMax Laier struct ifg_group *ifg = NULL; 11010dad3f0eSMax Laier struct ifg_member *ifgm; 11020dad3f0eSMax Laier 11030dad3f0eSMax Laier if (groupname[0] && groupname[strlen(groupname) - 1] >= '0' && 11040dad3f0eSMax Laier groupname[strlen(groupname) - 1] <= '9') 11050dad3f0eSMax Laier return (EINVAL); 11060dad3f0eSMax Laier 11070dad3f0eSMax Laier IFNET_WLOCK(); 11080dad3f0eSMax Laier TAILQ_FOREACH(ifgl, &ifp->if_groups, ifgl_next) 11090dad3f0eSMax Laier if (!strcmp(ifgl->ifgl_group->ifg_group, groupname)) { 11100dad3f0eSMax Laier IFNET_WUNLOCK(); 11110dad3f0eSMax Laier return (EEXIST); 11120dad3f0eSMax Laier } 11130dad3f0eSMax Laier 11140dad3f0eSMax Laier if ((ifgl = (struct ifg_list *)malloc(sizeof(struct ifg_list), M_TEMP, 11150dad3f0eSMax Laier M_NOWAIT)) == NULL) { 11160dad3f0eSMax Laier IFNET_WUNLOCK(); 11170dad3f0eSMax Laier return (ENOMEM); 11180dad3f0eSMax Laier } 11190dad3f0eSMax Laier 11200dad3f0eSMax Laier if ((ifgm = (struct ifg_member *)malloc(sizeof(struct ifg_member), 11210dad3f0eSMax Laier M_TEMP, M_NOWAIT)) == NULL) { 11220dad3f0eSMax Laier free(ifgl, M_TEMP); 11230dad3f0eSMax Laier IFNET_WUNLOCK(); 11240dad3f0eSMax Laier return (ENOMEM); 11250dad3f0eSMax Laier } 11260dad3f0eSMax Laier 1127603724d3SBjoern A. Zeeb TAILQ_FOREACH(ifg, &V_ifg_head, ifg_next) 11280dad3f0eSMax Laier if (!strcmp(ifg->ifg_group, groupname)) 11290dad3f0eSMax Laier break; 11300dad3f0eSMax Laier 11310dad3f0eSMax Laier if (ifg == NULL) { 11320dad3f0eSMax Laier if ((ifg = (struct ifg_group *)malloc(sizeof(struct ifg_group), 11330dad3f0eSMax Laier M_TEMP, M_NOWAIT)) == NULL) { 11340dad3f0eSMax Laier free(ifgl, M_TEMP); 11350dad3f0eSMax Laier free(ifgm, M_TEMP); 11360dad3f0eSMax Laier IFNET_WUNLOCK(); 11370dad3f0eSMax Laier return (ENOMEM); 11380dad3f0eSMax Laier } 11390dad3f0eSMax Laier strlcpy(ifg->ifg_group, groupname, sizeof(ifg->ifg_group)); 11400dad3f0eSMax Laier ifg->ifg_refcnt = 0; 11410dad3f0eSMax Laier TAILQ_INIT(&ifg->ifg_members); 11420dad3f0eSMax Laier EVENTHANDLER_INVOKE(group_attach_event, ifg); 1143603724d3SBjoern A. Zeeb TAILQ_INSERT_TAIL(&V_ifg_head, ifg, ifg_next); 11440dad3f0eSMax Laier } 11450dad3f0eSMax Laier 11460dad3f0eSMax Laier ifg->ifg_refcnt++; 11470dad3f0eSMax Laier ifgl->ifgl_group = ifg; 11480dad3f0eSMax Laier ifgm->ifgm_ifp = ifp; 11490dad3f0eSMax Laier 11500dad3f0eSMax Laier IF_ADDR_LOCK(ifp); 11510dad3f0eSMax Laier TAILQ_INSERT_TAIL(&ifg->ifg_members, ifgm, ifgm_next); 11520dad3f0eSMax Laier TAILQ_INSERT_TAIL(&ifp->if_groups, ifgl, ifgl_next); 11530dad3f0eSMax Laier IF_ADDR_UNLOCK(ifp); 11540dad3f0eSMax Laier 11550dad3f0eSMax Laier IFNET_WUNLOCK(); 11560dad3f0eSMax Laier 11570dad3f0eSMax Laier EVENTHANDLER_INVOKE(group_change_event, groupname); 11580dad3f0eSMax Laier 11590dad3f0eSMax Laier return (0); 11600dad3f0eSMax Laier } 11610dad3f0eSMax Laier 11620dad3f0eSMax Laier /* 11630dad3f0eSMax Laier * Remove a group from an interface 11640dad3f0eSMax Laier */ 11650dad3f0eSMax Laier int 11660dad3f0eSMax Laier if_delgroup(struct ifnet *ifp, const char *groupname) 11670dad3f0eSMax Laier { 11680dad3f0eSMax Laier struct ifg_list *ifgl; 11690dad3f0eSMax Laier struct ifg_member *ifgm; 11700dad3f0eSMax Laier 11710dad3f0eSMax Laier IFNET_WLOCK(); 11720dad3f0eSMax Laier TAILQ_FOREACH(ifgl, &ifp->if_groups, ifgl_next) 11730dad3f0eSMax Laier if (!strcmp(ifgl->ifgl_group->ifg_group, groupname)) 11740dad3f0eSMax Laier break; 11750dad3f0eSMax Laier if (ifgl == NULL) { 11760dad3f0eSMax Laier IFNET_WUNLOCK(); 11770dad3f0eSMax Laier return (ENOENT); 11780dad3f0eSMax Laier } 11790dad3f0eSMax Laier 11800dad3f0eSMax Laier IF_ADDR_LOCK(ifp); 11810dad3f0eSMax Laier TAILQ_REMOVE(&ifp->if_groups, ifgl, ifgl_next); 11820dad3f0eSMax Laier IF_ADDR_UNLOCK(ifp); 11830dad3f0eSMax Laier 11840dad3f0eSMax Laier TAILQ_FOREACH(ifgm, &ifgl->ifgl_group->ifg_members, ifgm_next) 11850dad3f0eSMax Laier if (ifgm->ifgm_ifp == ifp) 11860dad3f0eSMax Laier break; 11870dad3f0eSMax Laier 11880dad3f0eSMax Laier if (ifgm != NULL) { 11890dad3f0eSMax Laier TAILQ_REMOVE(&ifgl->ifgl_group->ifg_members, ifgm, ifgm_next); 11900dad3f0eSMax Laier free(ifgm, M_TEMP); 11910dad3f0eSMax Laier } 11920dad3f0eSMax Laier 11930dad3f0eSMax Laier if (--ifgl->ifgl_group->ifg_refcnt == 0) { 1194603724d3SBjoern A. Zeeb TAILQ_REMOVE(&V_ifg_head, ifgl->ifgl_group, ifg_next); 11950dad3f0eSMax Laier EVENTHANDLER_INVOKE(group_detach_event, ifgl->ifgl_group); 11960dad3f0eSMax Laier free(ifgl->ifgl_group, M_TEMP); 11970dad3f0eSMax Laier } 11980dad3f0eSMax Laier IFNET_WUNLOCK(); 11990dad3f0eSMax Laier 12000dad3f0eSMax Laier free(ifgl, M_TEMP); 12010dad3f0eSMax Laier 12020dad3f0eSMax Laier EVENTHANDLER_INVOKE(group_change_event, groupname); 12030dad3f0eSMax Laier 12040dad3f0eSMax Laier return (0); 12050dad3f0eSMax Laier } 12060dad3f0eSMax Laier 12070dad3f0eSMax Laier /* 12088623f9fdSMax Laier * Remove an interface from all groups 12098623f9fdSMax Laier */ 12108623f9fdSMax Laier static void 12118623f9fdSMax Laier if_delgroups(struct ifnet *ifp) 12128623f9fdSMax Laier { 12138623f9fdSMax Laier struct ifg_list *ifgl; 12148623f9fdSMax Laier struct ifg_member *ifgm; 12158623f9fdSMax Laier char groupname[IFNAMSIZ]; 12168623f9fdSMax Laier 12178623f9fdSMax Laier IFNET_WLOCK(); 12188623f9fdSMax Laier while (!TAILQ_EMPTY(&ifp->if_groups)) { 12198623f9fdSMax Laier ifgl = TAILQ_FIRST(&ifp->if_groups); 12208623f9fdSMax Laier 12218623f9fdSMax Laier strlcpy(groupname, ifgl->ifgl_group->ifg_group, IFNAMSIZ); 12228623f9fdSMax Laier 12238623f9fdSMax Laier IF_ADDR_LOCK(ifp); 12248623f9fdSMax Laier TAILQ_REMOVE(&ifp->if_groups, ifgl, ifgl_next); 12258623f9fdSMax Laier IF_ADDR_UNLOCK(ifp); 12268623f9fdSMax Laier 12278623f9fdSMax Laier TAILQ_FOREACH(ifgm, &ifgl->ifgl_group->ifg_members, ifgm_next) 12288623f9fdSMax Laier if (ifgm->ifgm_ifp == ifp) 12298623f9fdSMax Laier break; 12308623f9fdSMax Laier 12318623f9fdSMax Laier if (ifgm != NULL) { 12328623f9fdSMax Laier TAILQ_REMOVE(&ifgl->ifgl_group->ifg_members, ifgm, 12338623f9fdSMax Laier ifgm_next); 12348623f9fdSMax Laier free(ifgm, M_TEMP); 12358623f9fdSMax Laier } 12368623f9fdSMax Laier 12378623f9fdSMax Laier if (--ifgl->ifgl_group->ifg_refcnt == 0) { 12388623f9fdSMax Laier TAILQ_REMOVE(&V_ifg_head, ifgl->ifgl_group, ifg_next); 12398623f9fdSMax Laier EVENTHANDLER_INVOKE(group_detach_event, 12408623f9fdSMax Laier ifgl->ifgl_group); 12418623f9fdSMax Laier free(ifgl->ifgl_group, M_TEMP); 12428623f9fdSMax Laier } 12438623f9fdSMax Laier IFNET_WUNLOCK(); 12448623f9fdSMax Laier 12458623f9fdSMax Laier free(ifgl, M_TEMP); 12468623f9fdSMax Laier 12478623f9fdSMax Laier EVENTHANDLER_INVOKE(group_change_event, groupname); 12488623f9fdSMax Laier 12498623f9fdSMax Laier IFNET_WLOCK(); 12508623f9fdSMax Laier } 12518623f9fdSMax Laier IFNET_WUNLOCK(); 12528623f9fdSMax Laier } 12538623f9fdSMax Laier 12548623f9fdSMax Laier /* 12550dad3f0eSMax Laier * Stores all groups from an interface in memory pointed 12560dad3f0eSMax Laier * to by data 12570dad3f0eSMax Laier */ 12580dad3f0eSMax Laier static int 12590dad3f0eSMax Laier if_getgroup(struct ifgroupreq *data, struct ifnet *ifp) 12600dad3f0eSMax Laier { 12610dad3f0eSMax Laier int len, error; 12620dad3f0eSMax Laier struct ifg_list *ifgl; 12630dad3f0eSMax Laier struct ifg_req ifgrq, *ifgp; 12640dad3f0eSMax Laier struct ifgroupreq *ifgr = data; 12650dad3f0eSMax Laier 12660dad3f0eSMax Laier if (ifgr->ifgr_len == 0) { 12670dad3f0eSMax Laier IF_ADDR_LOCK(ifp); 12680dad3f0eSMax Laier TAILQ_FOREACH(ifgl, &ifp->if_groups, ifgl_next) 12690dad3f0eSMax Laier ifgr->ifgr_len += sizeof(struct ifg_req); 12700dad3f0eSMax Laier IF_ADDR_UNLOCK(ifp); 12710dad3f0eSMax Laier return (0); 12720dad3f0eSMax Laier } 12730dad3f0eSMax Laier 12740dad3f0eSMax Laier len = ifgr->ifgr_len; 12750dad3f0eSMax Laier ifgp = ifgr->ifgr_groups; 12760dad3f0eSMax Laier /* XXX: wire */ 12770dad3f0eSMax Laier IF_ADDR_LOCK(ifp); 12780dad3f0eSMax Laier TAILQ_FOREACH(ifgl, &ifp->if_groups, ifgl_next) { 12790dad3f0eSMax Laier if (len < sizeof(ifgrq)) { 12800dad3f0eSMax Laier IF_ADDR_UNLOCK(ifp); 12810dad3f0eSMax Laier return (EINVAL); 12820dad3f0eSMax Laier } 12830dad3f0eSMax Laier bzero(&ifgrq, sizeof ifgrq); 12840dad3f0eSMax Laier strlcpy(ifgrq.ifgrq_group, ifgl->ifgl_group->ifg_group, 12850dad3f0eSMax Laier sizeof(ifgrq.ifgrq_group)); 12860dad3f0eSMax Laier if ((error = copyout(&ifgrq, ifgp, sizeof(struct ifg_req)))) { 12870dad3f0eSMax Laier IF_ADDR_UNLOCK(ifp); 12880dad3f0eSMax Laier return (error); 12890dad3f0eSMax Laier } 12900dad3f0eSMax Laier len -= sizeof(ifgrq); 12910dad3f0eSMax Laier ifgp++; 12920dad3f0eSMax Laier } 12930dad3f0eSMax Laier IF_ADDR_UNLOCK(ifp); 12940dad3f0eSMax Laier 12950dad3f0eSMax Laier return (0); 12960dad3f0eSMax Laier } 12970dad3f0eSMax Laier 12980dad3f0eSMax Laier /* 12990dad3f0eSMax Laier * Stores all members of a group in memory pointed to by data 13000dad3f0eSMax Laier */ 13010dad3f0eSMax Laier static int 13020dad3f0eSMax Laier if_getgroupmembers(struct ifgroupreq *data) 13030dad3f0eSMax Laier { 13040dad3f0eSMax Laier struct ifgroupreq *ifgr = data; 13050dad3f0eSMax Laier struct ifg_group *ifg; 13060dad3f0eSMax Laier struct ifg_member *ifgm; 13070dad3f0eSMax Laier struct ifg_req ifgrq, *ifgp; 13080dad3f0eSMax Laier int len, error; 13090dad3f0eSMax Laier 13100dad3f0eSMax Laier IFNET_RLOCK(); 1311603724d3SBjoern A. Zeeb TAILQ_FOREACH(ifg, &V_ifg_head, ifg_next) 13120dad3f0eSMax Laier if (!strcmp(ifg->ifg_group, ifgr->ifgr_name)) 13130dad3f0eSMax Laier break; 13140dad3f0eSMax Laier if (ifg == NULL) { 13150dad3f0eSMax Laier IFNET_RUNLOCK(); 13160dad3f0eSMax Laier return (ENOENT); 13170dad3f0eSMax Laier } 13180dad3f0eSMax Laier 13190dad3f0eSMax Laier if (ifgr->ifgr_len == 0) { 13200dad3f0eSMax Laier TAILQ_FOREACH(ifgm, &ifg->ifg_members, ifgm_next) 13210dad3f0eSMax Laier ifgr->ifgr_len += sizeof(ifgrq); 13220dad3f0eSMax Laier IFNET_RUNLOCK(); 13230dad3f0eSMax Laier return (0); 13240dad3f0eSMax Laier } 13250dad3f0eSMax Laier 13260dad3f0eSMax Laier len = ifgr->ifgr_len; 13270dad3f0eSMax Laier ifgp = ifgr->ifgr_groups; 13280dad3f0eSMax Laier TAILQ_FOREACH(ifgm, &ifg->ifg_members, ifgm_next) { 13290dad3f0eSMax Laier if (len < sizeof(ifgrq)) { 13300dad3f0eSMax Laier IFNET_RUNLOCK(); 13310dad3f0eSMax Laier return (EINVAL); 13320dad3f0eSMax Laier } 13330dad3f0eSMax Laier bzero(&ifgrq, sizeof ifgrq); 13340dad3f0eSMax Laier strlcpy(ifgrq.ifgrq_member, ifgm->ifgm_ifp->if_xname, 13350dad3f0eSMax Laier sizeof(ifgrq.ifgrq_member)); 13360dad3f0eSMax Laier if ((error = copyout(&ifgrq, ifgp, sizeof(struct ifg_req)))) { 13370dad3f0eSMax Laier IFNET_RUNLOCK(); 13380dad3f0eSMax Laier return (error); 13390dad3f0eSMax Laier } 13400dad3f0eSMax Laier len -= sizeof(ifgrq); 13410dad3f0eSMax Laier ifgp++; 13420dad3f0eSMax Laier } 13430dad3f0eSMax Laier IFNET_RUNLOCK(); 13440dad3f0eSMax Laier 13450dad3f0eSMax Laier return (0); 13460dad3f0eSMax Laier } 13470dad3f0eSMax Laier 13480dad3f0eSMax Laier /* 13495500d3beSWarner Losh * Delete Routes for a Network Interface 13505500d3beSWarner Losh * 13515500d3beSWarner Losh * Called for each routing entry via the rnh->rnh_walktree() call above 13525500d3beSWarner Losh * to delete all route entries referencing a detaching network interface. 13535500d3beSWarner Losh * 13545500d3beSWarner Losh * Arguments: 13555500d3beSWarner Losh * rn pointer to node in the routing table 13565500d3beSWarner Losh * arg argument passed to rnh->rnh_walktree() - detaching interface 13575500d3beSWarner Losh * 13585500d3beSWarner Losh * Returns: 13595500d3beSWarner Losh * 0 successful 13605500d3beSWarner Losh * errno failed - reason indicated 13615500d3beSWarner Losh * 13625500d3beSWarner Losh */ 13635500d3beSWarner Losh static int 136472fd1b6aSDag-Erling Smørgrav if_rtdel(struct radix_node *rn, void *arg) 13655500d3beSWarner Losh { 13665500d3beSWarner Losh struct rtentry *rt = (struct rtentry *)rn; 13675500d3beSWarner Losh struct ifnet *ifp = arg; 13685500d3beSWarner Losh int err; 13695500d3beSWarner Losh 13705500d3beSWarner Losh if (rt->rt_ifp == ifp) { 13715500d3beSWarner Losh 13725500d3beSWarner Losh /* 13735500d3beSWarner Losh * Protect (sorta) against walktree recursion problems 13745500d3beSWarner Losh * with cloned routes 13755500d3beSWarner Losh */ 13765500d3beSWarner Losh if ((rt->rt_flags & RTF_UP) == 0) 13775500d3beSWarner Losh return (0); 13785500d3beSWarner Losh 13798b07e49aSJulian Elischer err = rtrequest_fib(RTM_DELETE, rt_key(rt), rt->rt_gateway, 13806241d13aSKip Macy rt_mask(rt), rt->rt_flags|RTF_RNH_LOCKED, 13818b07e49aSJulian Elischer (struct rtentry **) NULL, rt->rt_fibnum); 13825500d3beSWarner Losh if (err) { 13835500d3beSWarner Losh log(LOG_WARNING, "if_rtdel: error %d\n", err); 13845500d3beSWarner Losh } 13855500d3beSWarner Losh } 13865500d3beSWarner Losh 13875500d3beSWarner Losh return (0); 13886182fdbdSPeter Wemm } 13896182fdbdSPeter Wemm 139040d8a302SBruce M Simpson /* 1391f9ef96caSRobert Watson * Wrapper functions for struct ifnet address list locking macros. These are 1392f9ef96caSRobert Watson * used by kernel modules to avoid encoding programming interface or binary 1393f9ef96caSRobert Watson * interface assumptions that may be violated when kernel-internal locking 1394f9ef96caSRobert Watson * approaches change. 1395f9ef96caSRobert Watson */ 1396f9ef96caSRobert Watson void 1397f9ef96caSRobert Watson if_addr_rlock(struct ifnet *ifp) 1398f9ef96caSRobert Watson { 1399f9ef96caSRobert Watson 1400f9ef96caSRobert Watson IF_ADDR_LOCK(ifp); 1401f9ef96caSRobert Watson } 1402f9ef96caSRobert Watson 1403f9ef96caSRobert Watson void 1404f9ef96caSRobert Watson if_addr_runlock(struct ifnet *ifp) 1405f9ef96caSRobert Watson { 1406f9ef96caSRobert Watson 1407f9ef96caSRobert Watson IF_ADDR_UNLOCK(ifp); 1408f9ef96caSRobert Watson } 1409f9ef96caSRobert Watson 1410f9ef96caSRobert Watson void 1411f9ef96caSRobert Watson if_maddr_rlock(struct ifnet *ifp) 1412f9ef96caSRobert Watson { 1413f9ef96caSRobert Watson 1414f9ef96caSRobert Watson IF_ADDR_LOCK(ifp); 1415f9ef96caSRobert Watson } 1416f9ef96caSRobert Watson 1417f9ef96caSRobert Watson void 1418f9ef96caSRobert Watson if_maddr_runlock(struct ifnet *ifp) 1419f9ef96caSRobert Watson { 1420f9ef96caSRobert Watson 1421f9ef96caSRobert Watson IF_ADDR_UNLOCK(ifp); 1422f9ef96caSRobert Watson } 1423f9ef96caSRobert Watson 1424f9ef96caSRobert Watson /* 14251099f828SRobert Watson * Reference count functions for ifaddrs. 14261099f828SRobert Watson */ 14271099f828SRobert Watson void 14281099f828SRobert Watson ifa_init(struct ifaddr *ifa) 14291099f828SRobert Watson { 14301099f828SRobert Watson 14311099f828SRobert Watson mtx_init(&ifa->ifa_mtx, "ifaddr", NULL, MTX_DEF); 14321099f828SRobert Watson refcount_init(&ifa->ifa_refcnt, 1); 14331099f828SRobert Watson } 14341099f828SRobert Watson 14351099f828SRobert Watson void 14361099f828SRobert Watson ifa_ref(struct ifaddr *ifa) 14371099f828SRobert Watson { 14381099f828SRobert Watson 14391099f828SRobert Watson refcount_acquire(&ifa->ifa_refcnt); 14401099f828SRobert Watson } 14411099f828SRobert Watson 14421099f828SRobert Watson void 14431099f828SRobert Watson ifa_free(struct ifaddr *ifa) 14441099f828SRobert Watson { 14451099f828SRobert Watson 14461099f828SRobert Watson if (refcount_release(&ifa->ifa_refcnt)) { 14471099f828SRobert Watson mtx_destroy(&ifa->ifa_mtx); 14481099f828SRobert Watson free(ifa, M_IFADDR); 14491099f828SRobert Watson } 14501099f828SRobert Watson } 14511099f828SRobert Watson 14529bb7d0f4SQing Li int 14539bb7d0f4SQing Li ifa_add_loopback_route(struct ifaddr *ifa, struct sockaddr *ia) 14549bb7d0f4SQing Li { 14559bb7d0f4SQing Li int error = 0; 14569bb7d0f4SQing Li struct rtentry *rt = NULL; 14579bb7d0f4SQing Li struct rt_addrinfo info; 14589bb7d0f4SQing Li static struct sockaddr_dl null_sdl = {sizeof(null_sdl), AF_LINK}; 14599bb7d0f4SQing Li 14609bb7d0f4SQing Li bzero(&info, sizeof(info)); 14619bb7d0f4SQing Li info.rti_ifp = V_loif; 14629bb7d0f4SQing Li info.rti_flags = ifa->ifa_flags | RTF_HOST | RTF_STATIC; 14639bb7d0f4SQing Li info.rti_info[RTAX_DST] = ia; 14649bb7d0f4SQing Li info.rti_info[RTAX_GATEWAY] = (struct sockaddr *)&null_sdl; 14659bb7d0f4SQing Li error = rtrequest1_fib(RTM_ADD, &info, &rt, 0); 14669bb7d0f4SQing Li 14679bb7d0f4SQing Li if (error == 0 && rt != NULL) { 14689bb7d0f4SQing Li RT_LOCK(rt); 14699bb7d0f4SQing Li ((struct sockaddr_dl *)rt->rt_gateway)->sdl_type = 147046e7f983SQing Li ifa->ifa_ifp->if_type; 14719bb7d0f4SQing Li ((struct sockaddr_dl *)rt->rt_gateway)->sdl_index = 147246e7f983SQing Li ifa->ifa_ifp->if_index; 14739bb7d0f4SQing Li RT_REMREF(rt); 14749bb7d0f4SQing Li RT_UNLOCK(rt); 14759bb7d0f4SQing Li } else if (error != 0) 14769bb7d0f4SQing Li log(LOG_INFO, "ifa_add_loopback_route: insertion failed\n"); 14779bb7d0f4SQing Li 14789bb7d0f4SQing Li return (error); 14799bb7d0f4SQing Li } 14809bb7d0f4SQing Li 14819bb7d0f4SQing Li int 14829bb7d0f4SQing Li ifa_del_loopback_route(struct ifaddr *ifa, struct sockaddr *ia) 14839bb7d0f4SQing Li { 14849bb7d0f4SQing Li int error = 0; 14859bb7d0f4SQing Li struct rt_addrinfo info; 14869bb7d0f4SQing Li struct sockaddr_dl null_sdl; 14879bb7d0f4SQing Li 14889bb7d0f4SQing Li bzero(&null_sdl, sizeof(null_sdl)); 14899bb7d0f4SQing Li null_sdl.sdl_len = sizeof(null_sdl); 14909bb7d0f4SQing Li null_sdl.sdl_family = AF_LINK; 14919bb7d0f4SQing Li null_sdl.sdl_type = ifa->ifa_ifp->if_type; 14929bb7d0f4SQing Li null_sdl.sdl_index = ifa->ifa_ifp->if_index; 14939bb7d0f4SQing Li bzero(&info, sizeof(info)); 14949bb7d0f4SQing Li info.rti_flags = ifa->ifa_flags | RTF_HOST | RTF_STATIC; 14959bb7d0f4SQing Li info.rti_info[RTAX_DST] = ia; 14969bb7d0f4SQing Li info.rti_info[RTAX_GATEWAY] = (struct sockaddr *)&null_sdl; 14979bb7d0f4SQing Li error = rtrequest1_fib(RTM_DELETE, &info, NULL, 0); 14989bb7d0f4SQing Li 14999bb7d0f4SQing Li if (error != 0) 15009bb7d0f4SQing Li log(LOG_INFO, "ifa_del_loopback_route: deletion failed\n"); 15019bb7d0f4SQing Li 15029bb7d0f4SQing Li return (error); 15039bb7d0f4SQing Li } 15049bb7d0f4SQing Li 15051099f828SRobert Watson /* 150640d8a302SBruce M Simpson * XXX: Because sockaddr_dl has deeper structure than the sockaddr 150740d8a302SBruce M Simpson * structs used to represent other address families, it is necessary 150840d8a302SBruce M Simpson * to perform a different comparison. 150940d8a302SBruce M Simpson */ 151040d8a302SBruce M Simpson 151140d8a302SBruce M Simpson #define sa_equal(a1, a2) \ 151240d8a302SBruce M Simpson (bcmp((a1), (a2), ((a1))->sa_len) == 0) 151340d8a302SBruce M Simpson 151440d8a302SBruce M Simpson #define sa_dl_equal(a1, a2) \ 151540d8a302SBruce M Simpson ((((struct sockaddr_dl *)(a1))->sdl_len == \ 151640d8a302SBruce M Simpson ((struct sockaddr_dl *)(a2))->sdl_len) && \ 151740d8a302SBruce M Simpson (bcmp(LLADDR((struct sockaddr_dl *)(a1)), \ 151840d8a302SBruce M Simpson LLADDR((struct sockaddr_dl *)(a2)), \ 151940d8a302SBruce M Simpson ((struct sockaddr_dl *)(a1))->sdl_alen) == 0)) 152019fc74fbSJeffrey Hsu 152130aad87dSBrooks Davis /* 1522df8bae1dSRodney W. Grimes * Locate an interface based on a complete address. 1523df8bae1dSRodney W. Grimes */ 1524df8bae1dSRodney W. Grimes /*ARGSUSED*/ 15258896f83aSRobert Watson static struct ifaddr * 15268c0fec80SRobert Watson ifa_ifwithaddr_internal(struct sockaddr *addr, int getref) 1527df8bae1dSRodney W. Grimes { 15280b59d917SJonathan Lemon struct ifnet *ifp; 15290b59d917SJonathan Lemon struct ifaddr *ifa; 1530df8bae1dSRodney W. Grimes 153177dfcdc4SRobert Watson IFNET_RLOCK_NOSLEEP(); 1532ab5ed8a5SRobert Watson TAILQ_FOREACH(ifp, &V_ifnet, if_link) { 1533ab5ed8a5SRobert Watson IF_ADDR_LOCK(ifp); 153437d40066SPoul-Henning Kamp TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) { 1535df8bae1dSRodney W. Grimes if (ifa->ifa_addr->sa_family != addr->sa_family) 1536df8bae1dSRodney W. Grimes continue; 1537ab5ed8a5SRobert Watson if (sa_equal(addr, ifa->ifa_addr)) { 15388c0fec80SRobert Watson if (getref) 15398c0fec80SRobert Watson ifa_ref(ifa); 1540ab5ed8a5SRobert Watson IF_ADDR_UNLOCK(ifp); 15410b59d917SJonathan Lemon goto done; 1542ab5ed8a5SRobert Watson } 154382cd038dSYoshinobu Inoue /* IP6 doesn't have broadcast */ 15440b59d917SJonathan Lemon if ((ifp->if_flags & IFF_BROADCAST) && 15450b59d917SJonathan Lemon ifa->ifa_broadaddr && 154682cd038dSYoshinobu Inoue ifa->ifa_broadaddr->sa_len != 0 && 1547ab5ed8a5SRobert Watson sa_equal(ifa->ifa_broadaddr, addr)) { 15488c0fec80SRobert Watson if (getref) 15498c0fec80SRobert Watson ifa_ref(ifa); 1550ab5ed8a5SRobert Watson IF_ADDR_UNLOCK(ifp); 15510b59d917SJonathan Lemon goto done; 15520b59d917SJonathan Lemon } 1553ab5ed8a5SRobert Watson } 1554ab5ed8a5SRobert Watson IF_ADDR_UNLOCK(ifp); 1555ab5ed8a5SRobert Watson } 15560b59d917SJonathan Lemon ifa = NULL; 15570b59d917SJonathan Lemon done: 155877dfcdc4SRobert Watson IFNET_RUNLOCK_NOSLEEP(); 1559df8bae1dSRodney W. Grimes return (ifa); 1560df8bae1dSRodney W. Grimes } 15610b59d917SJonathan Lemon 15628896f83aSRobert Watson struct ifaddr * 15638896f83aSRobert Watson ifa_ifwithaddr(struct sockaddr *addr) 15648896f83aSRobert Watson { 15658896f83aSRobert Watson 15668c0fec80SRobert Watson return (ifa_ifwithaddr_internal(addr, 1)); 15678896f83aSRobert Watson } 15688896f83aSRobert Watson 15698896f83aSRobert Watson int 15708896f83aSRobert Watson ifa_ifwithaddr_check(struct sockaddr *addr) 15718896f83aSRobert Watson { 15728896f83aSRobert Watson 15738c0fec80SRobert Watson return (ifa_ifwithaddr_internal(addr, 0) != NULL); 15748896f83aSRobert Watson } 15758896f83aSRobert Watson 1576df8bae1dSRodney W. Grimes /* 1577773725a2SAndre Oppermann * Locate an interface based on the broadcast address. 1578773725a2SAndre Oppermann */ 1579773725a2SAndre Oppermann /* ARGSUSED */ 1580773725a2SAndre Oppermann struct ifaddr * 1581773725a2SAndre Oppermann ifa_ifwithbroadaddr(struct sockaddr *addr) 1582773725a2SAndre Oppermann { 1583773725a2SAndre Oppermann struct ifnet *ifp; 1584773725a2SAndre Oppermann struct ifaddr *ifa; 1585773725a2SAndre Oppermann 158677dfcdc4SRobert Watson IFNET_RLOCK_NOSLEEP(); 1587ab5ed8a5SRobert Watson TAILQ_FOREACH(ifp, &V_ifnet, if_link) { 1588ab5ed8a5SRobert Watson IF_ADDR_LOCK(ifp); 1589773725a2SAndre Oppermann TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) { 1590773725a2SAndre Oppermann if (ifa->ifa_addr->sa_family != addr->sa_family) 1591773725a2SAndre Oppermann continue; 1592773725a2SAndre Oppermann if ((ifp->if_flags & IFF_BROADCAST) && 1593773725a2SAndre Oppermann ifa->ifa_broadaddr && 1594773725a2SAndre Oppermann ifa->ifa_broadaddr->sa_len != 0 && 1595ab5ed8a5SRobert Watson sa_equal(ifa->ifa_broadaddr, addr)) { 15968c0fec80SRobert Watson ifa_ref(ifa); 1597ab5ed8a5SRobert Watson IF_ADDR_UNLOCK(ifp); 1598773725a2SAndre Oppermann goto done; 1599773725a2SAndre Oppermann } 1600ab5ed8a5SRobert Watson } 1601ab5ed8a5SRobert Watson IF_ADDR_UNLOCK(ifp); 1602ab5ed8a5SRobert Watson } 1603773725a2SAndre Oppermann ifa = NULL; 1604773725a2SAndre Oppermann done: 160577dfcdc4SRobert Watson IFNET_RUNLOCK_NOSLEEP(); 1606773725a2SAndre Oppermann return (ifa); 1607773725a2SAndre Oppermann } 1608773725a2SAndre Oppermann 1609773725a2SAndre Oppermann /* 1610df8bae1dSRodney W. Grimes * Locate the point to point interface with a given destination address. 1611df8bae1dSRodney W. Grimes */ 1612df8bae1dSRodney W. Grimes /*ARGSUSED*/ 1613df8bae1dSRodney W. Grimes struct ifaddr * 161472fd1b6aSDag-Erling Smørgrav ifa_ifwithdstaddr(struct sockaddr *addr) 1615df8bae1dSRodney W. Grimes { 16160b59d917SJonathan Lemon struct ifnet *ifp; 16170b59d917SJonathan Lemon struct ifaddr *ifa; 1618df8bae1dSRodney W. Grimes 161977dfcdc4SRobert Watson IFNET_RLOCK_NOSLEEP(); 1620603724d3SBjoern A. Zeeb TAILQ_FOREACH(ifp, &V_ifnet, if_link) { 16210b59d917SJonathan Lemon if ((ifp->if_flags & IFF_POINTOPOINT) == 0) 16220b59d917SJonathan Lemon continue; 1623ab5ed8a5SRobert Watson IF_ADDR_LOCK(ifp); 162437d40066SPoul-Henning Kamp TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) { 1625df8bae1dSRodney W. Grimes if (ifa->ifa_addr->sa_family != addr->sa_family) 1626df8bae1dSRodney W. Grimes continue; 1627f0c04221SBjoern A. Zeeb if (ifa->ifa_dstaddr != NULL && 1628ab5ed8a5SRobert Watson sa_equal(addr, ifa->ifa_dstaddr)) { 16298c0fec80SRobert Watson ifa_ref(ifa); 1630ab5ed8a5SRobert Watson IF_ADDR_UNLOCK(ifp); 16310b59d917SJonathan Lemon goto done; 1632df8bae1dSRodney W. Grimes } 16330b59d917SJonathan Lemon } 1634ab5ed8a5SRobert Watson IF_ADDR_UNLOCK(ifp); 1635ab5ed8a5SRobert Watson } 16360b59d917SJonathan Lemon ifa = NULL; 16370b59d917SJonathan Lemon done: 163877dfcdc4SRobert Watson IFNET_RUNLOCK_NOSLEEP(); 16390b59d917SJonathan Lemon return (ifa); 1640df8bae1dSRodney W. Grimes } 1641df8bae1dSRodney W. Grimes 1642df8bae1dSRodney W. Grimes /* 1643df8bae1dSRodney W. Grimes * Find an interface on a specific network. If many, choice 1644df8bae1dSRodney W. Grimes * is most specific found. 1645df8bae1dSRodney W. Grimes */ 1646df8bae1dSRodney W. Grimes struct ifaddr * 16470ed6142bSQing Li ifa_ifwithnet(struct sockaddr *addr, int ignore_ptp) 1648df8bae1dSRodney W. Grimes { 164972fd1b6aSDag-Erling Smørgrav struct ifnet *ifp; 165072fd1b6aSDag-Erling Smørgrav struct ifaddr *ifa; 16518c0fec80SRobert Watson struct ifaddr *ifa_maybe = NULL; 1652df8bae1dSRodney W. Grimes u_int af = addr->sa_family; 1653df8bae1dSRodney W. Grimes char *addr_data = addr->sa_data, *cplim; 1654df8bae1dSRodney W. Grimes 16557e2a6151SJulian Elischer /* 16567e2a6151SJulian Elischer * AF_LINK addresses can be looked up directly by their index number, 16577e2a6151SJulian Elischer * so do that if we can. 16587e2a6151SJulian Elischer */ 1659df8bae1dSRodney W. Grimes if (af == AF_LINK) { 1660d1dd20beSSam Leffler struct sockaddr_dl *sdl = (struct sockaddr_dl *)addr; 1661603724d3SBjoern A. Zeeb if (sdl->sdl_index && sdl->sdl_index <= V_if_index) 1662f9132cebSJonathan Lemon return (ifaddr_byindex(sdl->sdl_index)); 1663df8bae1dSRodney W. Grimes } 16647e2a6151SJulian Elischer 16657e2a6151SJulian Elischer /* 16668c0fec80SRobert Watson * Scan though each interface, looking for ones that have addresses 16678c0fec80SRobert Watson * in this address family. Maintain a reference on ifa_maybe once 16688c0fec80SRobert Watson * we find one, as we release the IF_ADDR_LOCK() that kept it stable 16698c0fec80SRobert Watson * when we move onto the next interface. 16707e2a6151SJulian Elischer */ 167177dfcdc4SRobert Watson IFNET_RLOCK_NOSLEEP(); 1672603724d3SBjoern A. Zeeb TAILQ_FOREACH(ifp, &V_ifnet, if_link) { 1673ab5ed8a5SRobert Watson IF_ADDR_LOCK(ifp); 167437d40066SPoul-Henning Kamp TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) { 167572fd1b6aSDag-Erling Smørgrav char *cp, *cp2, *cp3; 1676df8bae1dSRodney W. Grimes 1677523a02aaSDavid Greenman if (ifa->ifa_addr->sa_family != af) 1678df8bae1dSRodney W. Grimes next: continue; 16790ed6142bSQing Li if (af == AF_INET && 16800ed6142bSQing Li ifp->if_flags & IFF_POINTOPOINT && !ignore_ptp) { 16817e2a6151SJulian Elischer /* 16827e2a6151SJulian Elischer * This is a bit broken as it doesn't 16837e2a6151SJulian Elischer * take into account that the remote end may 16847e2a6151SJulian Elischer * be a single node in the network we are 16857e2a6151SJulian Elischer * looking for. 16867e2a6151SJulian Elischer * The trouble is that we don't know the 16877e2a6151SJulian Elischer * netmask for the remote end. 16887e2a6151SJulian Elischer */ 1689f0c04221SBjoern A. Zeeb if (ifa->ifa_dstaddr != NULL && 1690ab5ed8a5SRobert Watson sa_equal(addr, ifa->ifa_dstaddr)) { 16918c0fec80SRobert Watson ifa_ref(ifa); 1692ab5ed8a5SRobert Watson IF_ADDR_UNLOCK(ifp); 16930b59d917SJonathan Lemon goto done; 1694ab5ed8a5SRobert Watson } 16953740e2adSDavid Greenman } else { 16967e2a6151SJulian Elischer /* 16977ed8f465SJulian Elischer * if we have a special address handler, 16987ed8f465SJulian Elischer * then use it instead of the generic one. 16997ed8f465SJulian Elischer */ 17007ed8f465SJulian Elischer if (ifa->ifa_claim_addr) { 1701ab5ed8a5SRobert Watson if ((*ifa->ifa_claim_addr)(ifa, addr)) { 17028c0fec80SRobert Watson ifa_ref(ifa); 1703ab5ed8a5SRobert Watson IF_ADDR_UNLOCK(ifp); 17040b59d917SJonathan Lemon goto done; 1705ab5ed8a5SRobert Watson } 17067ed8f465SJulian Elischer continue; 17077ed8f465SJulian Elischer } 17087ed8f465SJulian Elischer 17097ed8f465SJulian Elischer /* 17107e2a6151SJulian Elischer * Scan all the bits in the ifa's address. 17117e2a6151SJulian Elischer * If a bit dissagrees with what we are 17127e2a6151SJulian Elischer * looking for, mask it with the netmask 17137e2a6151SJulian Elischer * to see if it really matters. 17147e2a6151SJulian Elischer * (A byte at a time) 17157e2a6151SJulian Elischer */ 1716523a02aaSDavid Greenman if (ifa->ifa_netmask == 0) 1717523a02aaSDavid Greenman continue; 1718df8bae1dSRodney W. Grimes cp = addr_data; 1719df8bae1dSRodney W. Grimes cp2 = ifa->ifa_addr->sa_data; 1720df8bae1dSRodney W. Grimes cp3 = ifa->ifa_netmask->sa_data; 17217e2a6151SJulian Elischer cplim = ifa->ifa_netmask->sa_len 17227e2a6151SJulian Elischer + (char *)ifa->ifa_netmask; 1723df8bae1dSRodney W. Grimes while (cp3 < cplim) 1724df8bae1dSRodney W. Grimes if ((*cp++ ^ *cp2++) & *cp3++) 17257e2a6151SJulian Elischer goto next; /* next address! */ 17267e2a6151SJulian Elischer /* 17277e2a6151SJulian Elischer * If the netmask of what we just found 17287e2a6151SJulian Elischer * is more specific than what we had before 17297e2a6151SJulian Elischer * (if we had one) then remember the new one 17307e2a6151SJulian Elischer * before continuing to search 17317e2a6151SJulian Elischer * for an even better one. 17327e2a6151SJulian Elischer */ 17338c0fec80SRobert Watson if (ifa_maybe == NULL || 1734df8bae1dSRodney W. Grimes rn_refines((caddr_t)ifa->ifa_netmask, 17358c0fec80SRobert Watson (caddr_t)ifa_maybe->ifa_netmask)) { 17368c0fec80SRobert Watson if (ifa_maybe != NULL) 17378c0fec80SRobert Watson ifa_free(ifa_maybe); 1738df8bae1dSRodney W. Grimes ifa_maybe = ifa; 17398c0fec80SRobert Watson ifa_ref(ifa_maybe); 17408c0fec80SRobert Watson } 1741df8bae1dSRodney W. Grimes } 1742b2af64fdSDavid Greenman } 1743ab5ed8a5SRobert Watson IF_ADDR_UNLOCK(ifp); 1744b2af64fdSDavid Greenman } 17450b59d917SJonathan Lemon ifa = ifa_maybe; 17468c0fec80SRobert Watson ifa_maybe = NULL; 17470b59d917SJonathan Lemon done: 174877dfcdc4SRobert Watson IFNET_RUNLOCK_NOSLEEP(); 17498c0fec80SRobert Watson if (ifa_maybe != NULL) 17508c0fec80SRobert Watson ifa_free(ifa_maybe); 17510b59d917SJonathan Lemon return (ifa); 1752df8bae1dSRodney W. Grimes } 1753df8bae1dSRodney W. Grimes 1754df8bae1dSRodney W. Grimes /* 1755df8bae1dSRodney W. Grimes * Find an interface address specific to an interface best matching 1756df8bae1dSRodney W. Grimes * a given address. 1757df8bae1dSRodney W. Grimes */ 1758df8bae1dSRodney W. Grimes struct ifaddr * 175972fd1b6aSDag-Erling Smørgrav ifaof_ifpforaddr(struct sockaddr *addr, struct ifnet *ifp) 1760df8bae1dSRodney W. Grimes { 176172fd1b6aSDag-Erling Smørgrav struct ifaddr *ifa; 176272fd1b6aSDag-Erling Smørgrav char *cp, *cp2, *cp3; 176372fd1b6aSDag-Erling Smørgrav char *cplim; 17648c0fec80SRobert Watson struct ifaddr *ifa_maybe = NULL; 1765df8bae1dSRodney W. Grimes u_int af = addr->sa_family; 1766df8bae1dSRodney W. Grimes 1767df8bae1dSRodney W. Grimes if (af >= AF_MAX) 1768cd292f12SBjoern A. Zeeb return (NULL); 1769ab5ed8a5SRobert Watson IF_ADDR_LOCK(ifp); 177037d40066SPoul-Henning Kamp TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) { 1771df8bae1dSRodney W. Grimes if (ifa->ifa_addr->sa_family != af) 1772df8bae1dSRodney W. Grimes continue; 17738c0fec80SRobert Watson if (ifa_maybe == NULL) 1774df8bae1dSRodney W. Grimes ifa_maybe = ifa; 1775df8bae1dSRodney W. Grimes if (ifa->ifa_netmask == 0) { 1776d8d5b10eSRobert Watson if (sa_equal(addr, ifa->ifa_addr) || 1777d8d5b10eSRobert Watson (ifa->ifa_dstaddr && 1778d8d5b10eSRobert Watson sa_equal(addr, ifa->ifa_dstaddr))) 17792defe5cdSJonathan Lemon goto done; 1780df8bae1dSRodney W. Grimes continue; 1781df8bae1dSRodney W. Grimes } 1782b2af64fdSDavid Greenman if (ifp->if_flags & IFF_POINTOPOINT) { 1783d8d5b10eSRobert Watson if (sa_equal(addr, ifa->ifa_dstaddr)) 1784a8637146SJonathan Lemon goto done; 17853740e2adSDavid Greenman } else { 1786df8bae1dSRodney W. Grimes cp = addr->sa_data; 1787df8bae1dSRodney W. Grimes cp2 = ifa->ifa_addr->sa_data; 1788df8bae1dSRodney W. Grimes cp3 = ifa->ifa_netmask->sa_data; 1789df8bae1dSRodney W. Grimes cplim = ifa->ifa_netmask->sa_len + (char *)ifa->ifa_netmask; 1790df8bae1dSRodney W. Grimes for (; cp3 < cplim; cp3++) 1791df8bae1dSRodney W. Grimes if ((*cp++ ^ *cp2++) & *cp3) 1792df8bae1dSRodney W. Grimes break; 1793df8bae1dSRodney W. Grimes if (cp3 == cplim) 17942defe5cdSJonathan Lemon goto done; 1795df8bae1dSRodney W. Grimes } 1796b2af64fdSDavid Greenman } 1797f9132cebSJonathan Lemon ifa = ifa_maybe; 1798f9132cebSJonathan Lemon done: 17998c0fec80SRobert Watson if (ifa != NULL) 18008c0fec80SRobert Watson ifa_ref(ifa); 1801ab5ed8a5SRobert Watson IF_ADDR_UNLOCK(ifp); 1802f9132cebSJonathan Lemon return (ifa); 1803df8bae1dSRodney W. Grimes } 1804df8bae1dSRodney W. Grimes 18056e6b3f7cSQing Li #include <net/if_llatbl.h> 18066e6b3f7cSQing Li 1807df8bae1dSRodney W. Grimes /* 1808df8bae1dSRodney W. Grimes * Default action when installing a route with a Link Level gateway. 1809df8bae1dSRodney W. Grimes * Lookup an appropriate real ifa to point to. 1810df8bae1dSRodney W. Grimes * This should be moved to /sys/net/link.c eventually. 1811df8bae1dSRodney W. Grimes */ 18123bda9f9bSPoul-Henning Kamp static void 181372fd1b6aSDag-Erling Smørgrav link_rtrequest(int cmd, struct rtentry *rt, struct rt_addrinfo *info) 1814df8bae1dSRodney W. Grimes { 181572fd1b6aSDag-Erling Smørgrav struct ifaddr *ifa, *oifa; 1816df8bae1dSRodney W. Grimes struct sockaddr *dst; 1817df8bae1dSRodney W. Grimes struct ifnet *ifp; 1818df8bae1dSRodney W. Grimes 1819d1dd20beSSam Leffler RT_LOCK_ASSERT(rt); 1820d1dd20beSSam Leffler 1821df8bae1dSRodney W. Grimes if (cmd != RTM_ADD || ((ifa = rt->rt_ifa) == 0) || 1822df8bae1dSRodney W. Grimes ((ifp = ifa->ifa_ifp) == 0) || ((dst = rt_key(rt)) == 0)) 1823df8bae1dSRodney W. Grimes return; 18249448326fSPoul-Henning Kamp ifa = ifaof_ifpforaddr(dst, ifp); 18259448326fSPoul-Henning Kamp if (ifa) { 1826d1dd20beSSam Leffler oifa = rt->rt_ifa; 1827df8bae1dSRodney W. Grimes rt->rt_ifa = ifa; 18281099f828SRobert Watson ifa_free(oifa); 1829df8bae1dSRodney W. Grimes if (ifa->ifa_rtrequest && ifa->ifa_rtrequest != link_rtrequest) 18308071913dSRuslan Ermilov ifa->ifa_rtrequest(cmd, rt, info); 1831df8bae1dSRodney W. Grimes } 1832df8bae1dSRodney W. Grimes } 1833df8bae1dSRodney W. Grimes 1834df8bae1dSRodney W. Grimes /* 1835df8bae1dSRodney W. Grimes * Mark an interface down and notify protocols of 1836df8bae1dSRodney W. Grimes * the transition. 1837df8bae1dSRodney W. Grimes * NOTE: must be called at splnet or eqivalent. 1838df8bae1dSRodney W. Grimes */ 18398614fb12SMax Laier static void 184072fd1b6aSDag-Erling Smørgrav if_unroute(struct ifnet *ifp, int flag, int fam) 1841df8bae1dSRodney W. Grimes { 184272fd1b6aSDag-Erling Smørgrav struct ifaddr *ifa; 1843df8bae1dSRodney W. Grimes 1844292ee7beSRobert Watson KASSERT(flag == IFF_UP, ("if_unroute: flag != IFF_UP")); 1845292ee7beSRobert Watson 1846e8c2601dSPoul-Henning Kamp ifp->if_flags &= ~flag; 184798b9590eSPoul-Henning Kamp getmicrotime(&ifp->if_lastchange); 1848e8c2601dSPoul-Henning Kamp TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) 1849e8c2601dSPoul-Henning Kamp if (fam == PF_UNSPEC || (fam == ifa->ifa_addr->sa_family)) 1850df8bae1dSRodney W. Grimes pfctlinput(PRC_IFDOWN, ifa->ifa_addr); 1851db7f0b97SKip Macy ifp->if_qflush(ifp); 1852db7f0b97SKip Macy 1853a9771948SGleb Smirnoff if (ifp->if_carp) 185454bfbd51SWill Andrews (*carp_linkstate_p)(ifp); 1855df8bae1dSRodney W. Grimes rt_ifmsg(ifp); 1856df8bae1dSRodney W. Grimes } 1857df8bae1dSRodney W. Grimes 1858df8bae1dSRodney W. Grimes /* 1859df8bae1dSRodney W. Grimes * Mark an interface up and notify protocols of 1860df8bae1dSRodney W. Grimes * the transition. 1861df8bae1dSRodney W. Grimes * NOTE: must be called at splnet or eqivalent. 1862df8bae1dSRodney W. Grimes */ 18638614fb12SMax Laier static void 186472fd1b6aSDag-Erling Smørgrav if_route(struct ifnet *ifp, int flag, int fam) 1865df8bae1dSRodney W. Grimes { 186672fd1b6aSDag-Erling Smørgrav struct ifaddr *ifa; 1867df8bae1dSRodney W. Grimes 1868292ee7beSRobert Watson KASSERT(flag == IFF_UP, ("if_route: flag != IFF_UP")); 1869292ee7beSRobert Watson 1870e8c2601dSPoul-Henning Kamp ifp->if_flags |= flag; 187198b9590eSPoul-Henning Kamp getmicrotime(&ifp->if_lastchange); 1872e8c2601dSPoul-Henning Kamp TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) 1873e8c2601dSPoul-Henning Kamp if (fam == PF_UNSPEC || (fam == ifa->ifa_addr->sa_family)) 1874df8bae1dSRodney W. Grimes pfctlinput(PRC_IFUP, ifa->ifa_addr); 1875a9771948SGleb Smirnoff if (ifp->if_carp) 187654bfbd51SWill Andrews (*carp_linkstate_p)(ifp); 1877df8bae1dSRodney W. Grimes rt_ifmsg(ifp); 187882cd038dSYoshinobu Inoue #ifdef INET6 187982cd038dSYoshinobu Inoue in6_if_up(ifp); 188082cd038dSYoshinobu Inoue #endif 1881df8bae1dSRodney W. Grimes } 1882df8bae1dSRodney W. Grimes 1883a6fffd6cSBrooks Davis void (*vlan_link_state_p)(struct ifnet *); /* XXX: private from if_vlan */ 188475ee267cSGleb Smirnoff void (*vlan_trunk_cap_p)(struct ifnet *); /* XXX: private from if_vlan */ 1885e4cd31ddSJeff Roberson struct ifnet *(*vlan_trunkdev_p)(struct ifnet *); 1886e4cd31ddSJeff Roberson struct ifnet *(*vlan_devat_p)(struct ifnet *, uint16_t); 1887e4cd31ddSJeff Roberson int (*vlan_tag_p)(struct ifnet *, uint16_t *); 1888e4cd31ddSJeff Roberson int (*vlan_setcookie_p)(struct ifnet *, void *); 1889e4cd31ddSJeff Roberson void *(*vlan_cookie_p)(struct ifnet *); 189094f5c9cfSSam Leffler 189194f5c9cfSSam Leffler /* 189268a3482fSGleb Smirnoff * Handle a change in the interface link state. To avoid LORs 189368a3482fSGleb Smirnoff * between driver lock and upper layer locks, as well as possible 189468a3482fSGleb Smirnoff * recursions, we post event to taskqueue, and all job 189568a3482fSGleb Smirnoff * is done in static do_link_state_change(). 189694f5c9cfSSam Leffler */ 189794f5c9cfSSam Leffler void 189894f5c9cfSSam Leffler if_link_state_change(struct ifnet *ifp, int link_state) 189994f5c9cfSSam Leffler { 19004d96314fSGleb Smirnoff /* Return if state hasn't changed. */ 19014d96314fSGleb Smirnoff if (ifp->if_link_state == link_state) 19024d96314fSGleb Smirnoff return; 19034d96314fSGleb Smirnoff 190494f5c9cfSSam Leffler ifp->if_link_state = link_state; 19054d96314fSGleb Smirnoff 190668a3482fSGleb Smirnoff taskqueue_enqueue(taskqueue_swi, &ifp->if_linktask); 190768a3482fSGleb Smirnoff } 190868a3482fSGleb Smirnoff 190968a3482fSGleb Smirnoff static void 191068a3482fSGleb Smirnoff do_link_state_change(void *arg, int pending) 191168a3482fSGleb Smirnoff { 191268a3482fSGleb Smirnoff struct ifnet *ifp = (struct ifnet *)arg; 191368a3482fSGleb Smirnoff int link_state = ifp->if_link_state; 19148b615593SMarko Zec CURVNET_SET(ifp->if_vnet); 191568a3482fSGleb Smirnoff 19164d96314fSGleb Smirnoff /* Notify that the link state has changed. */ 191794f5c9cfSSam Leffler rt_ifmsg(ifp); 191875ee267cSGleb Smirnoff if (ifp->if_vlantrunk != NULL) 1919a6fffd6cSBrooks Davis (*vlan_link_state_p)(ifp); 19201c7899c7SGleb Smirnoff 19211c7899c7SGleb Smirnoff if ((ifp->if_type == IFT_ETHER || ifp->if_type == IFT_L2VLAN) && 19221c7899c7SGleb Smirnoff IFP2AC(ifp)->ac_netgraph != NULL) 19231c7899c7SGleb Smirnoff (*ng_ether_link_state_p)(ifp, link_state); 19244d96314fSGleb Smirnoff if (ifp->if_carp) 192554bfbd51SWill Andrews (*carp_linkstate_p)(ifp); 19268f867517SAndrew Thompson if (ifp->if_bridge) { 19278f867517SAndrew Thompson KASSERT(bstp_linkstate_p != NULL,("if_bridge bstp not loaded!")); 19288f867517SAndrew Thompson (*bstp_linkstate_p)(ifp, link_state); 19298f867517SAndrew Thompson } 193018242d3bSAndrew Thompson if (ifp->if_lagg) { 193118242d3bSAndrew Thompson KASSERT(lagg_linkstate_p != NULL,("if_lagg not loaded!")); 193218242d3bSAndrew Thompson (*lagg_linkstate_p)(ifp, link_state); 1933b47888ceSAndrew Thompson } 19348f867517SAndrew Thompson 193521ca7b57SMarko Zec if (IS_DEFAULT_VNET(curvnet)) 19369d80a330SBrooks Davis devctl_notify("IFNET", ifp->if_xname, 193721ca7b57SMarko Zec (link_state == LINK_STATE_UP) ? "LINK_UP" : "LINK_DOWN", 193821ca7b57SMarko Zec NULL); 193968a3482fSGleb Smirnoff if (pending > 1) 194068a3482fSGleb Smirnoff if_printf(ifp, "%d link states coalesced\n", pending); 19415515c2e7SGleb Smirnoff if (log_link_state_change) 19428b02df24SGleb Smirnoff log(LOG_NOTICE, "%s: link state changed to %s\n", ifp->if_xname, 19438b02df24SGleb Smirnoff (link_state == LINK_STATE_UP) ? "UP" : "DOWN" ); 1944e4cd31ddSJeff Roberson EVENTHANDLER_INVOKE(ifnet_link_event, ifp, ifp->if_link_state); 19458b615593SMarko Zec CURVNET_RESTORE(); 194694f5c9cfSSam Leffler } 194794f5c9cfSSam Leffler 1948df8bae1dSRodney W. Grimes /* 1949e8c2601dSPoul-Henning Kamp * Mark an interface down and notify protocols of 1950e8c2601dSPoul-Henning Kamp * the transition. 1951e8c2601dSPoul-Henning Kamp * NOTE: must be called at splnet or eqivalent. 1952e8c2601dSPoul-Henning Kamp */ 1953e8c2601dSPoul-Henning Kamp void 195472fd1b6aSDag-Erling Smørgrav if_down(struct ifnet *ifp) 1955e8c2601dSPoul-Henning Kamp { 1956e8c2601dSPoul-Henning Kamp 1957e8c2601dSPoul-Henning Kamp if_unroute(ifp, IFF_UP, AF_UNSPEC); 1958e8c2601dSPoul-Henning Kamp } 1959e8c2601dSPoul-Henning Kamp 1960e8c2601dSPoul-Henning Kamp /* 1961e8c2601dSPoul-Henning Kamp * Mark an interface up and notify protocols of 1962e8c2601dSPoul-Henning Kamp * the transition. 1963e8c2601dSPoul-Henning Kamp * NOTE: must be called at splnet or eqivalent. 1964e8c2601dSPoul-Henning Kamp */ 1965e8c2601dSPoul-Henning Kamp void 196672fd1b6aSDag-Erling Smørgrav if_up(struct ifnet *ifp) 1967e8c2601dSPoul-Henning Kamp { 1968e8c2601dSPoul-Henning Kamp 1969e8c2601dSPoul-Henning Kamp if_route(ifp, IFF_UP, AF_UNSPEC); 1970e8c2601dSPoul-Henning Kamp } 1971e8c2601dSPoul-Henning Kamp 1972e8c2601dSPoul-Henning Kamp /* 1973df8bae1dSRodney W. Grimes * Flush an interface queue. 1974df8bae1dSRodney W. Grimes */ 19757cc5b47fSKip Macy void 1976db7f0b97SKip Macy if_qflush(struct ifnet *ifp) 1977df8bae1dSRodney W. Grimes { 197872fd1b6aSDag-Erling Smørgrav struct mbuf *m, *n; 1979db7f0b97SKip Macy struct ifaltq *ifq; 1980df8bae1dSRodney W. Grimes 1981db7f0b97SKip Macy ifq = &ifp->if_snd; 19827b21048cSMax Laier IFQ_LOCK(ifq); 198302b199f1SMax Laier #ifdef ALTQ 198402b199f1SMax Laier if (ALTQ_IS_ENABLED(ifq)) 198502b199f1SMax Laier ALTQ_PURGE(ifq); 198602b199f1SMax Laier #endif 1987df8bae1dSRodney W. Grimes n = ifq->ifq_head; 19889448326fSPoul-Henning Kamp while ((m = n) != 0) { 1989df8bae1dSRodney W. Grimes n = m->m_act; 1990df8bae1dSRodney W. Grimes m_freem(m); 1991df8bae1dSRodney W. Grimes } 1992df8bae1dSRodney W. Grimes ifq->ifq_head = 0; 1993df8bae1dSRodney W. Grimes ifq->ifq_tail = 0; 1994df8bae1dSRodney W. Grimes ifq->ifq_len = 0; 19957b21048cSMax Laier IFQ_UNLOCK(ifq); 1996df8bae1dSRodney W. Grimes } 1997df8bae1dSRodney W. Grimes 1998df8bae1dSRodney W. Grimes /* 19996064c5d3SRobert Watson * Map interface name to interface structure pointer, with or without 20006064c5d3SRobert Watson * returning a reference. 2001df8bae1dSRodney W. Grimes */ 2002df8bae1dSRodney W. Grimes struct ifnet * 20036064c5d3SRobert Watson ifunit_ref(const char *name) 20046064c5d3SRobert Watson { 20056064c5d3SRobert Watson struct ifnet *ifp; 20066064c5d3SRobert Watson 200777dfcdc4SRobert Watson IFNET_RLOCK_NOSLEEP(); 20086064c5d3SRobert Watson TAILQ_FOREACH(ifp, &V_ifnet, if_link) { 20098bd015a1SRobert Watson if (strncmp(name, ifp->if_xname, IFNAMSIZ) == 0 && 20108bd015a1SRobert Watson !(ifp->if_flags & IFF_DYING)) 20116064c5d3SRobert Watson break; 20126064c5d3SRobert Watson } 20136064c5d3SRobert Watson if (ifp != NULL) 20146064c5d3SRobert Watson if_ref(ifp); 201577dfcdc4SRobert Watson IFNET_RUNLOCK_NOSLEEP(); 20166064c5d3SRobert Watson return (ifp); 20176064c5d3SRobert Watson } 20186064c5d3SRobert Watson 20196064c5d3SRobert Watson struct ifnet * 202030aad87dSBrooks Davis ifunit(const char *name) 2021df8bae1dSRodney W. Grimes { 20228b7805e4SBoris Popov struct ifnet *ifp; 2023df8bae1dSRodney W. Grimes 202477dfcdc4SRobert Watson IFNET_RLOCK_NOSLEEP(); 2025603724d3SBjoern A. Zeeb TAILQ_FOREACH(ifp, &V_ifnet, if_link) { 202636c19a57SBrooks Davis if (strncmp(name, ifp->if_xname, IFNAMSIZ) == 0) 2027df8bae1dSRodney W. Grimes break; 2028df8bae1dSRodney W. Grimes } 202977dfcdc4SRobert Watson IFNET_RUNLOCK_NOSLEEP(); 2030df8bae1dSRodney W. Grimes return (ifp); 2031df8bae1dSRodney W. Grimes } 2032df8bae1dSRodney W. Grimes 203382cd038dSYoshinobu Inoue /* 2034f13ad206SJonathan Lemon * Hardware specific interface ioctls. 2035df8bae1dSRodney W. Grimes */ 2036f13ad206SJonathan Lemon static int 2037f13ad206SJonathan Lemon ifhwioctl(u_long cmd, struct ifnet *ifp, caddr_t data, struct thread *td) 2038df8bae1dSRodney W. Grimes { 2039f13ad206SJonathan Lemon struct ifreq *ifr; 2040413dd0baSPoul-Henning Kamp struct ifstat *ifs; 2041f13ad206SJonathan Lemon int error = 0; 2042292ee7beSRobert Watson int new_flags, temp_flags; 204336c19a57SBrooks Davis size_t namelen, onamelen; 2044215940b3SXin LI size_t descrlen; 2045215940b3SXin LI char *descrbuf, *odescrbuf; 204636c19a57SBrooks Davis char new_name[IFNAMSIZ]; 204736c19a57SBrooks Davis struct ifaddr *ifa; 204836c19a57SBrooks Davis struct sockaddr_dl *sdl; 2049df8bae1dSRodney W. Grimes 2050df8bae1dSRodney W. Grimes ifr = (struct ifreq *)data; 205130aad87dSBrooks Davis switch (cmd) { 2052de593450SJonathan Lemon case SIOCGIFINDEX: 2053de593450SJonathan Lemon ifr->ifr_index = ifp->if_index; 2054de593450SJonathan Lemon break; 2055de593450SJonathan Lemon 2056df8bae1dSRodney W. Grimes case SIOCGIFFLAGS: 2057292ee7beSRobert Watson temp_flags = ifp->if_flags | ifp->if_drv_flags; 2058292ee7beSRobert Watson ifr->ifr_flags = temp_flags & 0xffff; 2059292ee7beSRobert Watson ifr->ifr_flagshigh = temp_flags >> 16; 2060df8bae1dSRodney W. Grimes break; 2061df8bae1dSRodney W. Grimes 2062016da741SJonathan Lemon case SIOCGIFCAP: 2063016da741SJonathan Lemon ifr->ifr_reqcap = ifp->if_capabilities; 2064016da741SJonathan Lemon ifr->ifr_curcap = ifp->if_capenable; 2065016da741SJonathan Lemon break; 2066016da741SJonathan Lemon 20678f293a63SRobert Watson #ifdef MAC 20688f293a63SRobert Watson case SIOCGIFMAC: 206930d239bcSRobert Watson error = mac_ifnet_ioctl_get(td->td_ucred, ifr, ifp); 20708f293a63SRobert Watson break; 20718f293a63SRobert Watson #endif 20728f293a63SRobert Watson 2073df8bae1dSRodney W. Grimes case SIOCGIFMETRIC: 2074df8bae1dSRodney W. Grimes ifr->ifr_metric = ifp->if_metric; 2075df8bae1dSRodney W. Grimes break; 2076df8bae1dSRodney W. Grimes 2077a7028af7SDavid Greenman case SIOCGIFMTU: 2078a7028af7SDavid Greenman ifr->ifr_mtu = ifp->if_mtu; 2079a7028af7SDavid Greenman break; 2080a7028af7SDavid Greenman 2081074c4a4eSGarrett Wollman case SIOCGIFPHYS: 2082074c4a4eSGarrett Wollman ifr->ifr_phys = ifp->if_physical; 2083074c4a4eSGarrett Wollman break; 2084074c4a4eSGarrett Wollman 2085215940b3SXin LI case SIOCGIFDESCR: 2086215940b3SXin LI error = 0; 2087215940b3SXin LI sx_slock(&ifdescr_sx); 208857d84848SXin LI if (ifp->if_description == NULL) 2089215940b3SXin LI error = ENOMSG; 209057d84848SXin LI else { 2091215940b3SXin LI /* space for terminating nul */ 2092215940b3SXin LI descrlen = strlen(ifp->if_description) + 1; 2093215940b3SXin LI if (ifr->ifr_buffer.length < descrlen) 209457d84848SXin LI ifr->ifr_buffer.buffer = NULL; 2095215940b3SXin LI else 2096215940b3SXin LI error = copyout(ifp->if_description, 2097215940b3SXin LI ifr->ifr_buffer.buffer, descrlen); 2098215940b3SXin LI ifr->ifr_buffer.length = descrlen; 2099215940b3SXin LI } 2100215940b3SXin LI sx_sunlock(&ifdescr_sx); 2101215940b3SXin LI break; 2102215940b3SXin LI 2103215940b3SXin LI case SIOCSIFDESCR: 2104215940b3SXin LI error = priv_check(td, PRIV_NET_SETIFDESCR); 2105215940b3SXin LI if (error) 2106215940b3SXin LI return (error); 2107215940b3SXin LI 2108215940b3SXin LI /* 2109215940b3SXin LI * Copy only (length-1) bytes to make sure that 2110215940b3SXin LI * if_description is always nul terminated. The 2111215940b3SXin LI * length parameter is supposed to count the 2112215940b3SXin LI * terminating nul in. 2113215940b3SXin LI */ 2114215940b3SXin LI if (ifr->ifr_buffer.length > ifdescr_maxlen) 2115215940b3SXin LI return (ENAMETOOLONG); 2116215940b3SXin LI else if (ifr->ifr_buffer.length == 0) 2117215940b3SXin LI descrbuf = NULL; 2118215940b3SXin LI else { 2119215940b3SXin LI descrbuf = malloc(ifr->ifr_buffer.length, M_IFDESCR, 2120215940b3SXin LI M_WAITOK | M_ZERO); 2121215940b3SXin LI error = copyin(ifr->ifr_buffer.buffer, descrbuf, 2122215940b3SXin LI ifr->ifr_buffer.length - 1); 2123215940b3SXin LI if (error) { 2124215940b3SXin LI free(descrbuf, M_IFDESCR); 2125215940b3SXin LI break; 2126215940b3SXin LI } 2127215940b3SXin LI } 2128215940b3SXin LI 2129215940b3SXin LI sx_xlock(&ifdescr_sx); 2130215940b3SXin LI odescrbuf = ifp->if_description; 2131215940b3SXin LI ifp->if_description = descrbuf; 2132215940b3SXin LI sx_xunlock(&ifdescr_sx); 2133215940b3SXin LI 2134215940b3SXin LI getmicrotime(&ifp->if_lastchange); 2135215940b3SXin LI free(odescrbuf, M_IFDESCR); 2136215940b3SXin LI break; 2137215940b3SXin LI 2138df8bae1dSRodney W. Grimes case SIOCSIFFLAGS: 2139acd3428bSRobert Watson error = priv_check(td, PRIV_NET_SETIFFLAGS); 21409448326fSPoul-Henning Kamp if (error) 2141df8bae1dSRodney W. Grimes return (error); 2142292ee7beSRobert Watson /* 2143292ee7beSRobert Watson * Currently, no driver owned flags pass the IFF_CANTCHANGE 2144292ee7beSRobert Watson * check, so we don't need special handling here yet. 2145292ee7beSRobert Watson */ 214662f76486SMaxim Sobolev new_flags = (ifr->ifr_flags & 0xffff) | 214762f76486SMaxim Sobolev (ifr->ifr_flagshigh << 16); 2148cf4b9371SPoul-Henning Kamp if (ifp->if_flags & IFF_SMART) { 2149cf4b9371SPoul-Henning Kamp /* Smart drivers twiddle their own routes */ 21502f55ead7SPoul-Henning Kamp } else if (ifp->if_flags & IFF_UP && 215162f76486SMaxim Sobolev (new_flags & IFF_UP) == 0) { 2152df8bae1dSRodney W. Grimes int s = splimp(); 2153df8bae1dSRodney W. Grimes if_down(ifp); 2154df8bae1dSRodney W. Grimes splx(s); 215562f76486SMaxim Sobolev } else if (new_flags & IFF_UP && 2156cf4b9371SPoul-Henning Kamp (ifp->if_flags & IFF_UP) == 0) { 2157df8bae1dSRodney W. Grimes int s = splimp(); 2158df8bae1dSRodney W. Grimes if_up(ifp); 2159df8bae1dSRodney W. Grimes splx(s); 2160df8bae1dSRodney W. Grimes } 21617aebc5e8SYaroslav Tykhiy /* See if permanently promiscuous mode bit is about to flip */ 21627aebc5e8SYaroslav Tykhiy if ((ifp->if_flags ^ new_flags) & IFF_PPROMISC) { 21637aebc5e8SYaroslav Tykhiy if (new_flags & IFF_PPROMISC) 21647aebc5e8SYaroslav Tykhiy ifp->if_flags |= IFF_PROMISC; 21657aebc5e8SYaroslav Tykhiy else if (ifp->if_pcount == 0) 21667aebc5e8SYaroslav Tykhiy ifp->if_flags &= ~IFF_PROMISC; 21677aebc5e8SYaroslav Tykhiy log(LOG_INFO, "%s: permanently promiscuous mode %s\n", 21687aebc5e8SYaroslav Tykhiy ifp->if_xname, 21697aebc5e8SYaroslav Tykhiy (new_flags & IFF_PPROMISC) ? "enabled" : "disabled"); 21707aebc5e8SYaroslav Tykhiy } 2171df8bae1dSRodney W. Grimes ifp->if_flags = (ifp->if_flags & IFF_CANTCHANGE) | 217262f76486SMaxim Sobolev (new_flags &~ IFF_CANTCHANGE); 217331302ebfSRobert Watson if (ifp->if_ioctl) { 2174df8bae1dSRodney W. Grimes (void) (*ifp->if_ioctl)(ifp, cmd, data); 217531302ebfSRobert Watson } 217698b9590eSPoul-Henning Kamp getmicrotime(&ifp->if_lastchange); 2177df8bae1dSRodney W. Grimes break; 2178df8bae1dSRodney W. Grimes 2179016da741SJonathan Lemon case SIOCSIFCAP: 2180acd3428bSRobert Watson error = priv_check(td, PRIV_NET_SETIFCAP); 2181016da741SJonathan Lemon if (error) 2182016da741SJonathan Lemon return (error); 2183efb4018bSYaroslav Tykhiy if (ifp->if_ioctl == NULL) 2184efb4018bSYaroslav Tykhiy return (EOPNOTSUPP); 2185016da741SJonathan Lemon if (ifr->ifr_reqcap & ~ifp->if_capabilities) 2186016da741SJonathan Lemon return (EINVAL); 2187efb4018bSYaroslav Tykhiy error = (*ifp->if_ioctl)(ifp, cmd, data); 2188efb4018bSYaroslav Tykhiy if (error == 0) 2189efb4018bSYaroslav Tykhiy getmicrotime(&ifp->if_lastchange); 2190016da741SJonathan Lemon break; 2191016da741SJonathan Lemon 21928f293a63SRobert Watson #ifdef MAC 21938f293a63SRobert Watson case SIOCSIFMAC: 219430d239bcSRobert Watson error = mac_ifnet_ioctl_set(td->td_ucred, ifr, ifp); 21958f293a63SRobert Watson break; 21968f293a63SRobert Watson #endif 21978f293a63SRobert Watson 219836c19a57SBrooks Davis case SIOCSIFNAME: 2199acd3428bSRobert Watson error = priv_check(td, PRIV_NET_SETIFNAME); 2200acd3428bSRobert Watson if (error) 220136c19a57SBrooks Davis return (error); 220236c19a57SBrooks Davis error = copyinstr(ifr->ifr_data, new_name, IFNAMSIZ, NULL); 2203bc1470f1SBrooks Davis if (error != 0) 220436c19a57SBrooks Davis return (error); 2205bc1470f1SBrooks Davis if (new_name[0] == '\0') 2206bc1470f1SBrooks Davis return (EINVAL); 220736c19a57SBrooks Davis if (ifunit(new_name) != NULL) 220836c19a57SBrooks Davis return (EEXIST); 220936c19a57SBrooks Davis 22105428776eSJohn Baldwin /* 22115428776eSJohn Baldwin * XXX: Locking. Nothing else seems to lock if_flags, 22125428776eSJohn Baldwin * and there are numerous other races with the 22135428776eSJohn Baldwin * ifunit() checks not being atomic with namespace 22145428776eSJohn Baldwin * changes (renames, vmoves, if_attach, etc). 22155428776eSJohn Baldwin */ 22165428776eSJohn Baldwin ifp->if_flags |= IFF_RENAMING; 22175428776eSJohn Baldwin 221836c19a57SBrooks Davis /* Announce the departure of the interface. */ 221936c19a57SBrooks Davis rt_ifannouncemsg(ifp, IFAN_DEPARTURE); 222052023244SMax Laier EVENTHANDLER_INVOKE(ifnet_departure_event, ifp); 222136c19a57SBrooks Davis 222271672bb6SBrooks Davis log(LOG_INFO, "%s: changing name to '%s'\n", 222371672bb6SBrooks Davis ifp->if_xname, new_name); 222471672bb6SBrooks Davis 222536c19a57SBrooks Davis strlcpy(ifp->if_xname, new_name, sizeof(ifp->if_xname)); 22264a0d6638SRuslan Ermilov ifa = ifp->if_addr; 222736c19a57SBrooks Davis IFA_LOCK(ifa); 222836c19a57SBrooks Davis sdl = (struct sockaddr_dl *)ifa->ifa_addr; 222936c19a57SBrooks Davis namelen = strlen(new_name); 223036c19a57SBrooks Davis onamelen = sdl->sdl_nlen; 223136c19a57SBrooks Davis /* 223236c19a57SBrooks Davis * Move the address if needed. This is safe because we 223336c19a57SBrooks Davis * allocate space for a name of length IFNAMSIZ when we 223436c19a57SBrooks Davis * create this in if_attach(). 223536c19a57SBrooks Davis */ 223636c19a57SBrooks Davis if (namelen != onamelen) { 223736c19a57SBrooks Davis bcopy(sdl->sdl_data + onamelen, 223836c19a57SBrooks Davis sdl->sdl_data + namelen, sdl->sdl_alen); 223936c19a57SBrooks Davis } 224036c19a57SBrooks Davis bcopy(new_name, sdl->sdl_data, namelen); 224136c19a57SBrooks Davis sdl->sdl_nlen = namelen; 224236c19a57SBrooks Davis sdl = (struct sockaddr_dl *)ifa->ifa_netmask; 224336c19a57SBrooks Davis bzero(sdl->sdl_data, onamelen); 224436c19a57SBrooks Davis while (namelen != 0) 224536c19a57SBrooks Davis sdl->sdl_data[--namelen] = 0xff; 224636c19a57SBrooks Davis IFA_UNLOCK(ifa); 224736c19a57SBrooks Davis 224825a4adceSMax Laier EVENTHANDLER_INVOKE(ifnet_arrival_event, ifp); 224936c19a57SBrooks Davis /* Announce the return of the interface. */ 225036c19a57SBrooks Davis rt_ifannouncemsg(ifp, IFAN_ARRIVAL); 22515428776eSJohn Baldwin 22525428776eSJohn Baldwin ifp->if_flags &= ~IFF_RENAMING; 225336c19a57SBrooks Davis break; 225436c19a57SBrooks Davis 2255679e1390SJamie Gritton #ifdef VIMAGE 2256679e1390SJamie Gritton case SIOCSIFVNET: 2257679e1390SJamie Gritton error = priv_check(td, PRIV_NET_SETIFVNET); 2258679e1390SJamie Gritton if (error) 2259679e1390SJamie Gritton return (error); 2260be31e5e7SBjoern A. Zeeb error = if_vmove_loan(td, ifp, ifr->ifr_name, ifr->ifr_jid); 2261679e1390SJamie Gritton break; 2262679e1390SJamie Gritton #endif 2263679e1390SJamie Gritton 2264df8bae1dSRodney W. Grimes case SIOCSIFMETRIC: 2265acd3428bSRobert Watson error = priv_check(td, PRIV_NET_SETIFMETRIC); 22669448326fSPoul-Henning Kamp if (error) 2267df8bae1dSRodney W. Grimes return (error); 2268df8bae1dSRodney W. Grimes ifp->if_metric = ifr->ifr_metric; 226998b9590eSPoul-Henning Kamp getmicrotime(&ifp->if_lastchange); 2270df8bae1dSRodney W. Grimes break; 2271df8bae1dSRodney W. Grimes 2272074c4a4eSGarrett Wollman case SIOCSIFPHYS: 2273acd3428bSRobert Watson error = priv_check(td, PRIV_NET_SETIFPHYS); 2274e39a0280SGary Palmer if (error) 2275913e410eSYaroslav Tykhiy return (error); 2276913e410eSYaroslav Tykhiy if (ifp->if_ioctl == NULL) 2277913e410eSYaroslav Tykhiy return (EOPNOTSUPP); 2278e39a0280SGary Palmer error = (*ifp->if_ioctl)(ifp, cmd, data); 2279e39a0280SGary Palmer if (error == 0) 228098b9590eSPoul-Henning Kamp getmicrotime(&ifp->if_lastchange); 2281913e410eSYaroslav Tykhiy break; 2282074c4a4eSGarrett Wollman 2283a7028af7SDavid Greenman case SIOCSIFMTU: 228482cd038dSYoshinobu Inoue { 228582cd038dSYoshinobu Inoue u_long oldmtu = ifp->if_mtu; 228682cd038dSYoshinobu Inoue 2287acd3428bSRobert Watson error = priv_check(td, PRIV_NET_SETIFMTU); 22889448326fSPoul-Henning Kamp if (error) 2289a7028af7SDavid Greenman return (error); 2290aab3beeeSBrian Somers if (ifr->ifr_mtu < IF_MINMTU || ifr->ifr_mtu > IF_MAXMTU) 229175ee03cbSDavid Greenman return (EINVAL); 2292f13ad206SJonathan Lemon if (ifp->if_ioctl == NULL) 2293f13ad206SJonathan Lemon return (EOPNOTSUPP); 2294e39a0280SGary Palmer error = (*ifp->if_ioctl)(ifp, cmd, data); 229548f71763SRuslan Ermilov if (error == 0) { 229698b9590eSPoul-Henning Kamp getmicrotime(&ifp->if_lastchange); 229748f71763SRuslan Ermilov rt_ifmsg(ifp); 229848f71763SRuslan Ermilov } 229982cd038dSYoshinobu Inoue /* 230082cd038dSYoshinobu Inoue * If the link MTU changed, do network layer specific procedure. 230182cd038dSYoshinobu Inoue */ 230282cd038dSYoshinobu Inoue if (ifp->if_mtu != oldmtu) { 230382cd038dSYoshinobu Inoue #ifdef INET6 230482cd038dSYoshinobu Inoue nd6_setmtu(ifp); 230582cd038dSYoshinobu Inoue #endif 230682cd038dSYoshinobu Inoue } 2307f13ad206SJonathan Lemon break; 230882cd038dSYoshinobu Inoue } 2309a7028af7SDavid Greenman 2310df8bae1dSRodney W. Grimes case SIOCADDMULTI: 2311df8bae1dSRodney W. Grimes case SIOCDELMULTI: 2312acd3428bSRobert Watson if (cmd == SIOCADDMULTI) 2313acd3428bSRobert Watson error = priv_check(td, PRIV_NET_ADDMULTI); 2314acd3428bSRobert Watson else 2315acd3428bSRobert Watson error = priv_check(td, PRIV_NET_DELMULTI); 23169448326fSPoul-Henning Kamp if (error) 2317df8bae1dSRodney W. Grimes return (error); 2318477180fbSGarrett Wollman 2319477180fbSGarrett Wollman /* Don't allow group membership on non-multicast interfaces. */ 2320477180fbSGarrett Wollman if ((ifp->if_flags & IFF_MULTICAST) == 0) 2321f13ad206SJonathan Lemon return (EOPNOTSUPP); 2322477180fbSGarrett Wollman 2323477180fbSGarrett Wollman /* Don't let users screw up protocols' entries. */ 2324477180fbSGarrett Wollman if (ifr->ifr_addr.sa_family != AF_LINK) 2325f13ad206SJonathan Lemon return (EINVAL); 2326477180fbSGarrett Wollman 2327477180fbSGarrett Wollman if (cmd == SIOCADDMULTI) { 2328477180fbSGarrett Wollman struct ifmultiaddr *ifma; 2329ec002feeSBruce M Simpson 2330ec002feeSBruce M Simpson /* 2331ec002feeSBruce M Simpson * Userland is only permitted to join groups once 2332ec002feeSBruce M Simpson * via the if_addmulti() KPI, because it cannot hold 2333ec002feeSBruce M Simpson * struct ifmultiaddr * between calls. It may also 2334ec002feeSBruce M Simpson * lose a race while we check if the membership 2335ec002feeSBruce M Simpson * already exists. 2336ec002feeSBruce M Simpson */ 2337ec002feeSBruce M Simpson IF_ADDR_LOCK(ifp); 2338ec002feeSBruce M Simpson ifma = if_findmulti(ifp, &ifr->ifr_addr); 2339ec002feeSBruce M Simpson IF_ADDR_UNLOCK(ifp); 2340ec002feeSBruce M Simpson if (ifma != NULL) 2341ec002feeSBruce M Simpson error = EADDRINUSE; 2342ec002feeSBruce M Simpson else 2343477180fbSGarrett Wollman error = if_addmulti(ifp, &ifr->ifr_addr, &ifma); 2344477180fbSGarrett Wollman } else { 2345477180fbSGarrett Wollman error = if_delmulti(ifp, &ifr->ifr_addr); 2346477180fbSGarrett Wollman } 2347e39a0280SGary Palmer if (error == 0) 234898b9590eSPoul-Henning Kamp getmicrotime(&ifp->if_lastchange); 2349f13ad206SJonathan Lemon break; 2350df8bae1dSRodney W. Grimes 235141b3e8e5SJun-ichiro itojun Hagino case SIOCSIFPHYADDR: 235241b3e8e5SJun-ichiro itojun Hagino case SIOCDIFPHYADDR: 235341b3e8e5SJun-ichiro itojun Hagino #ifdef INET6 235441b3e8e5SJun-ichiro itojun Hagino case SIOCSIFPHYADDR_IN6: 235541b3e8e5SJun-ichiro itojun Hagino #endif 235633841545SHajimu UMEMOTO case SIOCSLIFPHYADDR: 2357a912e453SPeter Wemm case SIOCSIFMEDIA: 2358d7189ec6SJoerg Wunsch case SIOCSIFGENERIC: 2359acd3428bSRobert Watson error = priv_check(td, PRIV_NET_HWIOCTL); 2360a912e453SPeter Wemm if (error) 2361a912e453SPeter Wemm return (error); 2362f13ad206SJonathan Lemon if (ifp->if_ioctl == NULL) 2363a912e453SPeter Wemm return (EOPNOTSUPP); 2364a912e453SPeter Wemm error = (*ifp->if_ioctl)(ifp, cmd, data); 2365a912e453SPeter Wemm if (error == 0) 236698b9590eSPoul-Henning Kamp getmicrotime(&ifp->if_lastchange); 2367f13ad206SJonathan Lemon break; 2368a912e453SPeter Wemm 2369413dd0baSPoul-Henning Kamp case SIOCGIFSTATUS: 2370413dd0baSPoul-Henning Kamp ifs = (struct ifstat *)data; 2371413dd0baSPoul-Henning Kamp ifs->ascii[0] = '\0'; 2372413dd0baSPoul-Henning Kamp 237333841545SHajimu UMEMOTO case SIOCGIFPSRCADDR: 237433841545SHajimu UMEMOTO case SIOCGIFPDSTADDR: 237533841545SHajimu UMEMOTO case SIOCGLIFPHYADDR: 2376a912e453SPeter Wemm case SIOCGIFMEDIA: 2377d7189ec6SJoerg Wunsch case SIOCGIFGENERIC: 2378913e410eSYaroslav Tykhiy if (ifp->if_ioctl == NULL) 2379a912e453SPeter Wemm return (EOPNOTSUPP); 2380f13ad206SJonathan Lemon error = (*ifp->if_ioctl)(ifp, cmd, data); 2381f13ad206SJonathan Lemon break; 2382a912e453SPeter Wemm 2383b106252cSBill Paul case SIOCSIFLLADDR: 2384acd3428bSRobert Watson error = priv_check(td, PRIV_NET_SETLLADDR); 2385b106252cSBill Paul if (error) 2386b106252cSBill Paul return (error); 2387f13ad206SJonathan Lemon error = if_setlladdr(ifp, 238866ce51ceSArchie Cobbs ifr->ifr_addr.sa_data, ifr->ifr_addr.sa_len); 2389ea4ca115SAndrew Thompson EVENTHANDLER_INVOKE(iflladdr_event, ifp); 2390f13ad206SJonathan Lemon break; 239166ce51ceSArchie Cobbs 23920dad3f0eSMax Laier case SIOCAIFGROUP: 23930dad3f0eSMax Laier { 23940dad3f0eSMax Laier struct ifgroupreq *ifgr = (struct ifgroupreq *)ifr; 23950dad3f0eSMax Laier 2396acd3428bSRobert Watson error = priv_check(td, PRIV_NET_ADDIFGROUP); 23970dad3f0eSMax Laier if (error) 23980dad3f0eSMax Laier return (error); 23990dad3f0eSMax Laier if ((error = if_addgroup(ifp, ifgr->ifgr_group))) 24000dad3f0eSMax Laier return (error); 24010dad3f0eSMax Laier break; 24020dad3f0eSMax Laier } 24030dad3f0eSMax Laier 24040dad3f0eSMax Laier case SIOCGIFGROUP: 24050dad3f0eSMax Laier if ((error = if_getgroup((struct ifgroupreq *)ifr, ifp))) 24060dad3f0eSMax Laier return (error); 24070dad3f0eSMax Laier break; 24080dad3f0eSMax Laier 24090dad3f0eSMax Laier case SIOCDIFGROUP: 24100dad3f0eSMax Laier { 24110dad3f0eSMax Laier struct ifgroupreq *ifgr = (struct ifgroupreq *)ifr; 24120dad3f0eSMax Laier 2413acd3428bSRobert Watson error = priv_check(td, PRIV_NET_DELIFGROUP); 24140dad3f0eSMax Laier if (error) 24150dad3f0eSMax Laier return (error); 24160dad3f0eSMax Laier if ((error = if_delgroup(ifp, ifgr->ifgr_group))) 24170dad3f0eSMax Laier return (error); 24180dad3f0eSMax Laier break; 24190dad3f0eSMax Laier } 24200dad3f0eSMax Laier 2421df8bae1dSRodney W. Grimes default: 2422f13ad206SJonathan Lemon error = ENOIOCTL; 2423f13ad206SJonathan Lemon break; 2424f13ad206SJonathan Lemon } 2425f13ad206SJonathan Lemon return (error); 2426f13ad206SJonathan Lemon } 2427f13ad206SJonathan Lemon 24289af74f3dSSergey Kandaurov #ifdef COMPAT_FREEBSD32 24299af74f3dSSergey Kandaurov struct ifconf32 { 24309af74f3dSSergey Kandaurov int32_t ifc_len; 24319af74f3dSSergey Kandaurov union { 24329af74f3dSSergey Kandaurov uint32_t ifcu_buf; 24339af74f3dSSergey Kandaurov uint32_t ifcu_req; 24349af74f3dSSergey Kandaurov } ifc_ifcu; 24359af74f3dSSergey Kandaurov }; 24369af74f3dSSergey Kandaurov #define SIOCGIFCONF32 _IOWR('i', 36, struct ifconf32) 24379af74f3dSSergey Kandaurov #endif 24389af74f3dSSergey Kandaurov 2439f13ad206SJonathan Lemon /* 2440f13ad206SJonathan Lemon * Interface ioctls. 2441f13ad206SJonathan Lemon */ 2442f13ad206SJonathan Lemon int 244372fd1b6aSDag-Erling Smørgrav ifioctl(struct socket *so, u_long cmd, caddr_t data, struct thread *td) 2444f13ad206SJonathan Lemon { 2445f13ad206SJonathan Lemon struct ifnet *ifp; 2446f13ad206SJonathan Lemon struct ifreq *ifr; 2447f13ad206SJonathan Lemon int error; 244862f76486SMaxim Sobolev int oif_flags; 2449f13ad206SJonathan Lemon 24501fb51a12SBjoern A. Zeeb CURVNET_SET(so->so_vnet); 2451f13ad206SJonathan Lemon switch (cmd) { 2452f13ad206SJonathan Lemon case SIOCGIFCONF: 2453f13ad206SJonathan Lemon case OSIOCGIFCONF: 24541fb51a12SBjoern A. Zeeb error = ifconf(cmd, data); 24551fb51a12SBjoern A. Zeeb CURVNET_RESTORE(); 24561fb51a12SBjoern A. Zeeb return (error); 24579af74f3dSSergey Kandaurov 24589af74f3dSSergey Kandaurov #ifdef COMPAT_FREEBSD32 24599af74f3dSSergey Kandaurov case SIOCGIFCONF32: 24609af74f3dSSergey Kandaurov { 24619af74f3dSSergey Kandaurov struct ifconf32 *ifc32; 24629af74f3dSSergey Kandaurov struct ifconf ifc; 24639af74f3dSSergey Kandaurov 24649af74f3dSSergey Kandaurov ifc32 = (struct ifconf32 *)data; 24659af74f3dSSergey Kandaurov ifc.ifc_len = ifc32->ifc_len; 24669af74f3dSSergey Kandaurov ifc.ifc_buf = PTRIN(ifc32->ifc_buf); 24679af74f3dSSergey Kandaurov 24681fb51a12SBjoern A. Zeeb error = ifconf(SIOCGIFCONF, (void *)&ifc); 24691fb51a12SBjoern A. Zeeb CURVNET_RESTORE(); 24701fb51a12SBjoern A. Zeeb return (error); 24719af74f3dSSergey Kandaurov } 24729af74f3dSSergey Kandaurov #endif 2473f13ad206SJonathan Lemon } 2474f13ad206SJonathan Lemon ifr = (struct ifreq *)data; 2475f13ad206SJonathan Lemon 2476f13ad206SJonathan Lemon switch (cmd) { 2477feb08d06SMarko Zec #ifdef VIMAGE 2478679e1390SJamie Gritton case SIOCSIFRVNET: 2479679e1390SJamie Gritton error = priv_check(td, PRIV_NET_SETIFVNET); 24801fb51a12SBjoern A. Zeeb if (error == 0) 24811fb51a12SBjoern A. Zeeb error = if_vmove_reclaim(td, ifr->ifr_name, 24821fb51a12SBjoern A. Zeeb ifr->ifr_jid); 24831fb51a12SBjoern A. Zeeb CURVNET_RESTORE(); 2484679e1390SJamie Gritton return (error); 2485feb08d06SMarko Zec #endif 2486f13ad206SJonathan Lemon case SIOCIFCREATE: 24876b7330e2SSam Leffler case SIOCIFCREATE2: 2488acd3428bSRobert Watson error = priv_check(td, PRIV_NET_IFCREATE); 24891fb51a12SBjoern A. Zeeb if (error == 0) 24901fb51a12SBjoern A. Zeeb error = if_clone_create(ifr->ifr_name, 24911fb51a12SBjoern A. Zeeb sizeof(ifr->ifr_name), 24921fb51a12SBjoern A. Zeeb cmd == SIOCIFCREATE2 ? ifr->ifr_data : NULL); 24931fb51a12SBjoern A. Zeeb CURVNET_RESTORE(); 24946b7330e2SSam Leffler return (error); 2495f13ad206SJonathan Lemon case SIOCIFDESTROY: 2496acd3428bSRobert Watson error = priv_check(td, PRIV_NET_IFDESTROY); 24971fb51a12SBjoern A. Zeeb if (error == 0) 24981fb51a12SBjoern A. Zeeb error = if_clone_destroy(ifr->ifr_name); 24991fb51a12SBjoern A. Zeeb CURVNET_RESTORE(); 2500f13ad206SJonathan Lemon return (error); 2501f13ad206SJonathan Lemon 2502f13ad206SJonathan Lemon case SIOCIFGCLONERS: 25031fb51a12SBjoern A. Zeeb error = if_clone_list((struct if_clonereq *)data); 25041fb51a12SBjoern A. Zeeb CURVNET_RESTORE(); 25051fb51a12SBjoern A. Zeeb return (error); 25060dad3f0eSMax Laier case SIOCGIFGMEMB: 25071fb51a12SBjoern A. Zeeb error = if_getgroupmembers((struct ifgroupreq *)data); 25081fb51a12SBjoern A. Zeeb CURVNET_RESTORE(); 25091fb51a12SBjoern A. Zeeb return (error); 2510f13ad206SJonathan Lemon } 2511f13ad206SJonathan Lemon 25126064c5d3SRobert Watson ifp = ifunit_ref(ifr->ifr_name); 25131fb51a12SBjoern A. Zeeb if (ifp == NULL) { 25141fb51a12SBjoern A. Zeeb CURVNET_RESTORE(); 2515f13ad206SJonathan Lemon return (ENXIO); 25161fb51a12SBjoern A. Zeeb } 2517f13ad206SJonathan Lemon 2518f13ad206SJonathan Lemon error = ifhwioctl(cmd, ifp, data, td); 25196064c5d3SRobert Watson if (error != ENOIOCTL) { 25206064c5d3SRobert Watson if_rele(ifp); 25211fb51a12SBjoern A. Zeeb CURVNET_RESTORE(); 2522f13ad206SJonathan Lemon return (error); 25236064c5d3SRobert Watson } 2524f13ad206SJonathan Lemon 252582cd038dSYoshinobu Inoue oif_flags = ifp->if_flags; 25266064c5d3SRobert Watson if (so->so_proto == NULL) { 25276064c5d3SRobert Watson if_rele(ifp); 25281fb51a12SBjoern A. Zeeb CURVNET_RESTORE(); 2529df8bae1dSRodney W. Grimes return (EOPNOTSUPP); 25306064c5d3SRobert Watson } 2531df8bae1dSRodney W. Grimes #ifndef COMPAT_43 253282cd038dSYoshinobu Inoue error = ((*so->so_proto->pr_usrreqs->pru_control)(so, cmd, 25332c37256eSGarrett Wollman data, 2534b40ce416SJulian Elischer ifp, td)); 2535bc3977f1SJamie Gritton if (error == EOPNOTSUPP && ifp != NULL && ifp->if_ioctl != NULL) 2536bc3977f1SJamie Gritton error = (*ifp->if_ioctl)(ifp, cmd, data); 2537df8bae1dSRodney W. Grimes #else 2538df8bae1dSRodney W. Grimes { 2539e40bae9aSRoman Divacky u_long ocmd = cmd; 2540df8bae1dSRodney W. Grimes 2541df8bae1dSRodney W. Grimes switch (cmd) { 2542df8bae1dSRodney W. Grimes 2543df8bae1dSRodney W. Grimes case SIOCSIFDSTADDR: 2544df8bae1dSRodney W. Grimes case SIOCSIFADDR: 2545df8bae1dSRodney W. Grimes case SIOCSIFBRDADDR: 2546df8bae1dSRodney W. Grimes case SIOCSIFNETMASK: 2547df8bae1dSRodney W. Grimes #if BYTE_ORDER != BIG_ENDIAN 2548df8bae1dSRodney W. Grimes if (ifr->ifr_addr.sa_family == 0 && 2549df8bae1dSRodney W. Grimes ifr->ifr_addr.sa_len < 16) { 2550df8bae1dSRodney W. Grimes ifr->ifr_addr.sa_family = ifr->ifr_addr.sa_len; 2551df8bae1dSRodney W. Grimes ifr->ifr_addr.sa_len = 16; 2552df8bae1dSRodney W. Grimes } 2553df8bae1dSRodney W. Grimes #else 2554df8bae1dSRodney W. Grimes if (ifr->ifr_addr.sa_len == 0) 2555df8bae1dSRodney W. Grimes ifr->ifr_addr.sa_len = 16; 2556df8bae1dSRodney W. Grimes #endif 2557df8bae1dSRodney W. Grimes break; 2558df8bae1dSRodney W. Grimes 2559df8bae1dSRodney W. Grimes case OSIOCGIFADDR: 2560df8bae1dSRodney W. Grimes cmd = SIOCGIFADDR; 2561df8bae1dSRodney W. Grimes break; 2562df8bae1dSRodney W. Grimes 2563df8bae1dSRodney W. Grimes case OSIOCGIFDSTADDR: 2564df8bae1dSRodney W. Grimes cmd = SIOCGIFDSTADDR; 2565df8bae1dSRodney W. Grimes break; 2566df8bae1dSRodney W. Grimes 2567df8bae1dSRodney W. Grimes case OSIOCGIFBRDADDR: 2568df8bae1dSRodney W. Grimes cmd = SIOCGIFBRDADDR; 2569df8bae1dSRodney W. Grimes break; 2570df8bae1dSRodney W. Grimes 2571df8bae1dSRodney W. Grimes case OSIOCGIFNETMASK: 2572df8bae1dSRodney W. Grimes cmd = SIOCGIFNETMASK; 2573df8bae1dSRodney W. Grimes } 25742c37256eSGarrett Wollman error = ((*so->so_proto->pr_usrreqs->pru_control)(so, 25752c37256eSGarrett Wollman cmd, 25762c37256eSGarrett Wollman data, 2577b40ce416SJulian Elischer ifp, td)); 2578bc3977f1SJamie Gritton if (error == EOPNOTSUPP && ifp != NULL && 2579bc3977f1SJamie Gritton ifp->if_ioctl != NULL) 2580bc3977f1SJamie Gritton error = (*ifp->if_ioctl)(ifp, cmd, data); 2581df8bae1dSRodney W. Grimes switch (ocmd) { 2582df8bae1dSRodney W. Grimes 2583df8bae1dSRodney W. Grimes case OSIOCGIFADDR: 2584df8bae1dSRodney W. Grimes case OSIOCGIFDSTADDR: 2585df8bae1dSRodney W. Grimes case OSIOCGIFBRDADDR: 2586df8bae1dSRodney W. Grimes case OSIOCGIFNETMASK: 2587df8bae1dSRodney W. Grimes *(u_short *)&ifr->ifr_addr = ifr->ifr_addr.sa_family; 258882cd038dSYoshinobu Inoue 258982cd038dSYoshinobu Inoue } 259082cd038dSYoshinobu Inoue } 259182cd038dSYoshinobu Inoue #endif /* COMPAT_43 */ 259282cd038dSYoshinobu Inoue 259382cd038dSYoshinobu Inoue if ((oif_flags ^ ifp->if_flags) & IFF_UP) { 259482cd038dSYoshinobu Inoue #ifdef INET6 259582cd038dSYoshinobu Inoue if (ifp->if_flags & IFF_UP) { 259682cd038dSYoshinobu Inoue int s = splimp(); 259782cd038dSYoshinobu Inoue in6_if_up(ifp); 259882cd038dSYoshinobu Inoue splx(s); 259982cd038dSYoshinobu Inoue } 260082cd038dSYoshinobu Inoue #endif 2601df8bae1dSRodney W. Grimes } 26026064c5d3SRobert Watson if_rele(ifp); 26031fb51a12SBjoern A. Zeeb CURVNET_RESTORE(); 2604df8bae1dSRodney W. Grimes return (error); 2605df8bae1dSRodney W. Grimes } 2606df8bae1dSRodney W. Grimes 2607df8bae1dSRodney W. Grimes /* 2608292ee7beSRobert Watson * The code common to handling reference counted flags, 26091a3b6859SYaroslav Tykhiy * e.g., in ifpromisc() and if_allmulti(). 2610b5c8bd59SYaroslav Tykhiy * The "pflag" argument can specify a permanent mode flag to check, 26111a3b6859SYaroslav Tykhiy * such as IFF_PPROMISC for promiscuous mode; should be 0 if none. 2612292ee7beSRobert Watson * 2613292ee7beSRobert Watson * Only to be used on stack-owned flags, not driver-owned flags. 26141a3b6859SYaroslav Tykhiy */ 26151a3b6859SYaroslav Tykhiy static int 26161a3b6859SYaroslav Tykhiy if_setflag(struct ifnet *ifp, int flag, int pflag, int *refcount, int onswitch) 26171a3b6859SYaroslav Tykhiy { 26181a3b6859SYaroslav Tykhiy struct ifreq ifr; 26191a3b6859SYaroslav Tykhiy int error; 26201a3b6859SYaroslav Tykhiy int oldflags, oldcount; 26211a3b6859SYaroslav Tykhiy 26221a3b6859SYaroslav Tykhiy /* Sanity checks to catch programming errors */ 2623b5c8bd59SYaroslav Tykhiy KASSERT((flag & (IFF_DRV_OACTIVE|IFF_DRV_RUNNING)) == 0, 2624b5c8bd59SYaroslav Tykhiy ("%s: setting driver-owned flag %d", __func__, flag)); 2625b5c8bd59SYaroslav Tykhiy 2626b5c8bd59SYaroslav Tykhiy if (onswitch) 2627b5c8bd59SYaroslav Tykhiy KASSERT(*refcount >= 0, 2628b5c8bd59SYaroslav Tykhiy ("%s: increment negative refcount %d for flag %d", 2629b5c8bd59SYaroslav Tykhiy __func__, *refcount, flag)); 2630b5c8bd59SYaroslav Tykhiy else 2631b5c8bd59SYaroslav Tykhiy KASSERT(*refcount > 0, 2632b5c8bd59SYaroslav Tykhiy ("%s: decrement non-positive refcount %d for flag %d", 2633b5c8bd59SYaroslav Tykhiy __func__, *refcount, flag)); 26341a3b6859SYaroslav Tykhiy 26351a3b6859SYaroslav Tykhiy /* In case this mode is permanent, just touch refcount */ 26361a3b6859SYaroslav Tykhiy if (ifp->if_flags & pflag) { 26371a3b6859SYaroslav Tykhiy *refcount += onswitch ? 1 : -1; 26381a3b6859SYaroslav Tykhiy return (0); 26391a3b6859SYaroslav Tykhiy } 26401a3b6859SYaroslav Tykhiy 26411a3b6859SYaroslav Tykhiy /* Save ifnet parameters for if_ioctl() may fail */ 26421a3b6859SYaroslav Tykhiy oldcount = *refcount; 26431a3b6859SYaroslav Tykhiy oldflags = ifp->if_flags; 26441a3b6859SYaroslav Tykhiy 26451a3b6859SYaroslav Tykhiy /* 26461a3b6859SYaroslav Tykhiy * See if we aren't the only and touching refcount is enough. 26471a3b6859SYaroslav Tykhiy * Actually toggle interface flag if we are the first or last. 26481a3b6859SYaroslav Tykhiy */ 26491a3b6859SYaroslav Tykhiy if (onswitch) { 26501a3b6859SYaroslav Tykhiy if ((*refcount)++) 26511a3b6859SYaroslav Tykhiy return (0); 26521a3b6859SYaroslav Tykhiy ifp->if_flags |= flag; 26531a3b6859SYaroslav Tykhiy } else { 26541a3b6859SYaroslav Tykhiy if (--(*refcount)) 26551a3b6859SYaroslav Tykhiy return (0); 26561a3b6859SYaroslav Tykhiy ifp->if_flags &= ~flag; 26571a3b6859SYaroslav Tykhiy } 26581a3b6859SYaroslav Tykhiy 26591a3b6859SYaroslav Tykhiy /* Call down the driver since we've changed interface flags */ 26601a3b6859SYaroslav Tykhiy if (ifp->if_ioctl == NULL) { 26611a3b6859SYaroslav Tykhiy error = EOPNOTSUPP; 26621a3b6859SYaroslav Tykhiy goto recover; 26631a3b6859SYaroslav Tykhiy } 26641a3b6859SYaroslav Tykhiy ifr.ifr_flags = ifp->if_flags & 0xffff; 26651a3b6859SYaroslav Tykhiy ifr.ifr_flagshigh = ifp->if_flags >> 16; 26661a3b6859SYaroslav Tykhiy error = (*ifp->if_ioctl)(ifp, SIOCSIFFLAGS, (caddr_t)&ifr); 26671a3b6859SYaroslav Tykhiy if (error) 26681a3b6859SYaroslav Tykhiy goto recover; 26691a3b6859SYaroslav Tykhiy /* Notify userland that interface flags have changed */ 26701a3b6859SYaroslav Tykhiy rt_ifmsg(ifp); 26711a3b6859SYaroslav Tykhiy return (0); 26721a3b6859SYaroslav Tykhiy 26731a3b6859SYaroslav Tykhiy recover: 26741a3b6859SYaroslav Tykhiy /* Recover after driver error */ 26751a3b6859SYaroslav Tykhiy *refcount = oldcount; 26761a3b6859SYaroslav Tykhiy ifp->if_flags = oldflags; 26771a3b6859SYaroslav Tykhiy return (error); 26781a3b6859SYaroslav Tykhiy } 26791a3b6859SYaroslav Tykhiy 26801a3b6859SYaroslav Tykhiy /* 2681963e4c2aSGarrett Wollman * Set/clear promiscuous mode on interface ifp based on the truth value 2682963e4c2aSGarrett Wollman * of pswitch. The calls are reference counted so that only the first 2683963e4c2aSGarrett Wollman * "on" request actually has an effect, as does the final "off" request. 2684963e4c2aSGarrett Wollman * Results are undefined if the "off" and "on" requests are not matched. 2685963e4c2aSGarrett Wollman */ 2686963e4c2aSGarrett Wollman int 268772fd1b6aSDag-Erling Smørgrav ifpromisc(struct ifnet *ifp, int pswitch) 2688963e4c2aSGarrett Wollman { 26894a26224cSGarrett Wollman int error; 26901a3b6859SYaroslav Tykhiy int oldflags = ifp->if_flags; 2691963e4c2aSGarrett Wollman 26921a3b6859SYaroslav Tykhiy error = if_setflag(ifp, IFF_PROMISC, IFF_PPROMISC, 26931a3b6859SYaroslav Tykhiy &ifp->if_pcount, pswitch); 26941a3b6859SYaroslav Tykhiy /* If promiscuous mode status has changed, log a message */ 26951a3b6859SYaroslav Tykhiy if (error == 0 && ((ifp->if_flags ^ oldflags) & IFF_PROMISC)) 26969bf40edeSBrooks Davis log(LOG_INFO, "%s: promiscuous mode %s\n", 26979bf40edeSBrooks Davis ifp->if_xname, 26984f3c11a6SBill Fenner (ifp->if_flags & IFF_PROMISC) ? "enabled" : "disabled"); 26991a3b6859SYaroslav Tykhiy return (error); 2700963e4c2aSGarrett Wollman } 2701963e4c2aSGarrett Wollman 2702963e4c2aSGarrett Wollman /* 2703df8bae1dSRodney W. Grimes * Return interface configuration 2704df8bae1dSRodney W. Grimes * of system. List may be used 2705df8bae1dSRodney W. Grimes * in later ioctl's (above) to get 2706df8bae1dSRodney W. Grimes * other information. 2707df8bae1dSRodney W. Grimes */ 2708df8bae1dSRodney W. Grimes /*ARGSUSED*/ 27093bda9f9bSPoul-Henning Kamp static int 271072fd1b6aSDag-Erling Smørgrav ifconf(u_long cmd, caddr_t data) 2711df8bae1dSRodney W. Grimes { 27120b59d917SJonathan Lemon struct ifconf *ifc = (struct ifconf *)data; 27130b59d917SJonathan Lemon struct ifnet *ifp; 27140b59d917SJonathan Lemon struct ifaddr *ifa; 27154dcf2bbbSBrooks Davis struct ifreq ifr; 27164dcf2bbbSBrooks Davis struct sbuf *sb; 27174dcf2bbbSBrooks Davis int error, full = 0, valid_len, max_len; 2718df8bae1dSRodney W. Grimes 27194dcf2bbbSBrooks Davis /* Limit initial buffer size to MAXPHYS to avoid DoS from userspace. */ 27204dcf2bbbSBrooks Davis max_len = MAXPHYS - 1; 27214dcf2bbbSBrooks Davis 2722b0b4b28bSXin LI /* Prevent hostile input from being able to crash the system */ 2723b0b4b28bSXin LI if (ifc->ifc_len <= 0) 2724b0b4b28bSXin LI return (EINVAL); 2725b0b4b28bSXin LI 27264dcf2bbbSBrooks Davis again: 27274dcf2bbbSBrooks Davis if (ifc->ifc_len <= max_len) { 27284dcf2bbbSBrooks Davis max_len = ifc->ifc_len; 27294dcf2bbbSBrooks Davis full = 1; 27304dcf2bbbSBrooks Davis } 27314dcf2bbbSBrooks Davis sb = sbuf_new(NULL, NULL, max_len + 1, SBUF_FIXEDLEN); 27324dcf2bbbSBrooks Davis max_len = 0; 27334dcf2bbbSBrooks Davis valid_len = 0; 27344dcf2bbbSBrooks Davis 273577dfcdc4SRobert Watson IFNET_RLOCK(); 2736603724d3SBjoern A. Zeeb TAILQ_FOREACH(ifp, &V_ifnet, if_link) { 27379bf40edeSBrooks Davis int addrs; 27382624cf89SGarrett Wollman 2739fbd24c5eSColin Percival /* 2740fbd24c5eSColin Percival * Zero the ifr_name buffer to make sure we don't 2741fbd24c5eSColin Percival * disclose the contents of the stack. 2742fbd24c5eSColin Percival */ 2743fbd24c5eSColin Percival memset(ifr.ifr_name, 0, sizeof(ifr.ifr_name)); 2744fbd24c5eSColin Percival 27459bf40edeSBrooks Davis if (strlcpy(ifr.ifr_name, ifp->if_xname, sizeof(ifr.ifr_name)) 274662313e4cSSam Leffler >= sizeof(ifr.ifr_name)) { 274762313e4cSSam Leffler sbuf_delete(sb); 274862313e4cSSam Leffler IFNET_RUNLOCK(); 27494dcf2bbbSBrooks Davis return (ENAMETOOLONG); 275062313e4cSSam Leffler } 27512624cf89SGarrett Wollman 275275c13541SPoul-Henning Kamp addrs = 0; 2753ab5ed8a5SRobert Watson IF_ADDR_LOCK(ifp); 27542defe5cdSJonathan Lemon TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) { 27552defe5cdSJonathan Lemon struct sockaddr *sa = ifa->ifa_addr; 27562defe5cdSJonathan Lemon 2757b89e82ddSJamie Gritton if (prison_if(curthread->td_ucred, sa) != 0) 275875c13541SPoul-Henning Kamp continue; 275975c13541SPoul-Henning Kamp addrs++; 2760df8bae1dSRodney W. Grimes #ifdef COMPAT_43 2761df8bae1dSRodney W. Grimes if (cmd == OSIOCGIFCONF) { 2762df8bae1dSRodney W. Grimes struct osockaddr *osa = 2763df8bae1dSRodney W. Grimes (struct osockaddr *)&ifr.ifr_addr; 2764df8bae1dSRodney W. Grimes ifr.ifr_addr = *sa; 2765df8bae1dSRodney W. Grimes osa->sa_family = sa->sa_family; 27664dcf2bbbSBrooks Davis sbuf_bcat(sb, &ifr, sizeof(ifr)); 27674dcf2bbbSBrooks Davis max_len += sizeof(ifr); 2768df8bae1dSRodney W. Grimes } else 2769df8bae1dSRodney W. Grimes #endif 2770df8bae1dSRodney W. Grimes if (sa->sa_len <= sizeof(*sa)) { 2771df8bae1dSRodney W. Grimes ifr.ifr_addr = *sa; 27724dcf2bbbSBrooks Davis sbuf_bcat(sb, &ifr, sizeof(ifr)); 27734dcf2bbbSBrooks Davis max_len += sizeof(ifr); 2774df8bae1dSRodney W. Grimes } else { 27754dcf2bbbSBrooks Davis sbuf_bcat(sb, &ifr, 27764dcf2bbbSBrooks Davis offsetof(struct ifreq, ifr_addr)); 27774dcf2bbbSBrooks Davis max_len += offsetof(struct ifreq, ifr_addr); 27784dcf2bbbSBrooks Davis sbuf_bcat(sb, sa, sa->sa_len); 27794dcf2bbbSBrooks Davis max_len += sa->sa_len; 2780df8bae1dSRodney W. Grimes } 27814dcf2bbbSBrooks Davis 27824d369413SMatthew D Fleming if (sbuf_error(sb) == 0) 27834dcf2bbbSBrooks Davis valid_len = sbuf_len(sb); 2784df8bae1dSRodney W. Grimes } 2785ab5ed8a5SRobert Watson IF_ADDR_UNLOCK(ifp); 27864dcf2bbbSBrooks Davis if (addrs == 0) { 278775c13541SPoul-Henning Kamp bzero((caddr_t)&ifr.ifr_addr, sizeof(ifr.ifr_addr)); 27884dcf2bbbSBrooks Davis sbuf_bcat(sb, &ifr, sizeof(ifr)); 27894dcf2bbbSBrooks Davis max_len += sizeof(ifr); 27904dcf2bbbSBrooks Davis 27914d369413SMatthew D Fleming if (sbuf_error(sb) == 0) 27924dcf2bbbSBrooks Davis valid_len = sbuf_len(sb); 279375c13541SPoul-Henning Kamp } 2794df8bae1dSRodney W. Grimes } 2795b30a244cSJeffrey Hsu IFNET_RUNLOCK(); 27964dcf2bbbSBrooks Davis 27974dcf2bbbSBrooks Davis /* 27984dcf2bbbSBrooks Davis * If we didn't allocate enough space (uncommon), try again. If 27994dcf2bbbSBrooks Davis * we have already allocated as much space as we are allowed, 28004dcf2bbbSBrooks Davis * return what we've got. 28014dcf2bbbSBrooks Davis */ 28024dcf2bbbSBrooks Davis if (valid_len != max_len && !full) { 28034dcf2bbbSBrooks Davis sbuf_delete(sb); 28044dcf2bbbSBrooks Davis goto again; 28054dcf2bbbSBrooks Davis } 28064dcf2bbbSBrooks Davis 28074dcf2bbbSBrooks Davis ifc->ifc_len = valid_len; 28085ed8cedcSBrian Feldman sbuf_finish(sb); 28094dcf2bbbSBrooks Davis error = copyout(sbuf_data(sb), ifc->ifc_req, ifc->ifc_len); 28104dcf2bbbSBrooks Davis sbuf_delete(sb); 2811df8bae1dSRodney W. Grimes return (error); 2812df8bae1dSRodney W. Grimes } 2813df8bae1dSRodney W. Grimes 28141158dfb7SGarrett Wollman /* 28158b25904eSGleb Smirnoff * Just like ifpromisc(), but for all-multicast-reception mode. 28161158dfb7SGarrett Wollman */ 28171158dfb7SGarrett Wollman int 281872fd1b6aSDag-Erling Smørgrav if_allmulti(struct ifnet *ifp, int onswitch) 28191158dfb7SGarrett Wollman { 28201158dfb7SGarrett Wollman 28211a3b6859SYaroslav Tykhiy return (if_setflag(ifp, IFF_ALLMULTI, 0, &ifp->if_amcount, onswitch)); 28221158dfb7SGarrett Wollman } 28231158dfb7SGarrett Wollman 28245896d124SBruce M Simpson struct ifmultiaddr * 2825c3b31afdSRobert Watson if_findmulti(struct ifnet *ifp, struct sockaddr *sa) 28261158dfb7SGarrett Wollman { 28271158dfb7SGarrett Wollman struct ifmultiaddr *ifma; 28281158dfb7SGarrett Wollman 2829c3b31afdSRobert Watson IF_ADDR_LOCK_ASSERT(ifp); 2830c3b31afdSRobert Watson 28316817526dSPoul-Henning Kamp TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) { 283240d8a302SBruce M Simpson if (sa->sa_family == AF_LINK) { 283340d8a302SBruce M Simpson if (sa_dl_equal(ifma->ifma_addr, sa)) 283440d8a302SBruce M Simpson break; 283540d8a302SBruce M Simpson } else { 2836c3b31afdSRobert Watson if (sa_equal(ifma->ifma_addr, sa)) 2837c3b31afdSRobert Watson break; 28381158dfb7SGarrett Wollman } 283940d8a302SBruce M Simpson } 2840c3b31afdSRobert Watson 2841c3b31afdSRobert Watson return ifma; 284257af7922SJulian Elischer } 28431158dfb7SGarrett Wollman 28441158dfb7SGarrett Wollman /* 2845c3b31afdSRobert Watson * Allocate a new ifmultiaddr and initialize based on passed arguments. We 2846c3b31afdSRobert Watson * make copies of passed sockaddrs. The ifmultiaddr will not be added to 2847c3b31afdSRobert Watson * the ifnet multicast address list here, so the caller must do that and 2848c3b31afdSRobert Watson * other setup work (such as notifying the device driver). The reference 2849c3b31afdSRobert Watson * count is initialized to 1. 28501158dfb7SGarrett Wollman */ 2851c3b31afdSRobert Watson static struct ifmultiaddr * 2852c3b31afdSRobert Watson if_allocmulti(struct ifnet *ifp, struct sockaddr *sa, struct sockaddr *llsa, 2853c3b31afdSRobert Watson int mflags) 2854c3b31afdSRobert Watson { 2855c3b31afdSRobert Watson struct ifmultiaddr *ifma; 2856c3b31afdSRobert Watson struct sockaddr *dupsa; 2857c3b31afdSRobert Watson 28581ede983cSDag-Erling Smørgrav ifma = malloc(sizeof *ifma, M_IFMADDR, mflags | 2859c3b31afdSRobert Watson M_ZERO); 2860c3b31afdSRobert Watson if (ifma == NULL) 2861c3b31afdSRobert Watson return (NULL); 2862c3b31afdSRobert Watson 28631ede983cSDag-Erling Smørgrav dupsa = malloc(sa->sa_len, M_IFMADDR, mflags); 2864c3b31afdSRobert Watson if (dupsa == NULL) { 28651ede983cSDag-Erling Smørgrav free(ifma, M_IFMADDR); 2866c3b31afdSRobert Watson return (NULL); 28671158dfb7SGarrett Wollman } 28681158dfb7SGarrett Wollman bcopy(sa, dupsa, sa->sa_len); 28691158dfb7SGarrett Wollman ifma->ifma_addr = dupsa; 2870c3b31afdSRobert Watson 28711158dfb7SGarrett Wollman ifma->ifma_ifp = ifp; 28721158dfb7SGarrett Wollman ifma->ifma_refcount = 1; 2873d4d22970SGleb Smirnoff ifma->ifma_protospec = NULL; 2874c3b31afdSRobert Watson 2875c3b31afdSRobert Watson if (llsa == NULL) { 2876c3b31afdSRobert Watson ifma->ifma_lladdr = NULL; 2877c3b31afdSRobert Watson return (ifma); 2878c3b31afdSRobert Watson } 2879c3b31afdSRobert Watson 28801ede983cSDag-Erling Smørgrav dupsa = malloc(llsa->sa_len, M_IFMADDR, mflags); 2881c3b31afdSRobert Watson if (dupsa == NULL) { 28821ede983cSDag-Erling Smørgrav free(ifma->ifma_addr, M_IFMADDR); 28831ede983cSDag-Erling Smørgrav free(ifma, M_IFMADDR); 2884c3b31afdSRobert Watson return (NULL); 2885c3b31afdSRobert Watson } 2886c3b31afdSRobert Watson bcopy(llsa, dupsa, llsa->sa_len); 2887c3b31afdSRobert Watson ifma->ifma_lladdr = dupsa; 2888c3b31afdSRobert Watson 2889c3b31afdSRobert Watson return (ifma); 2890c3b31afdSRobert Watson } 2891373f88edSGarrett Wollman 28921158dfb7SGarrett Wollman /* 2893c3b31afdSRobert Watson * if_freemulti: free ifmultiaddr structure and possibly attached related 2894c3b31afdSRobert Watson * addresses. The caller is responsible for implementing reference 2895c3b31afdSRobert Watson * counting, notifying the driver, handling routing messages, and releasing 2896c3b31afdSRobert Watson * any dependent link layer state. 28971158dfb7SGarrett Wollman */ 2898c3b31afdSRobert Watson static void 2899c3b31afdSRobert Watson if_freemulti(struct ifmultiaddr *ifma) 2900c3b31afdSRobert Watson { 2901c3b31afdSRobert Watson 2902ec002feeSBruce M Simpson KASSERT(ifma->ifma_refcount == 0, ("if_freemulti: refcount %d", 2903c3b31afdSRobert Watson ifma->ifma_refcount)); 2904c3b31afdSRobert Watson KASSERT(ifma->ifma_protospec == NULL, 2905c3b31afdSRobert Watson ("if_freemulti: protospec not NULL")); 2906c3b31afdSRobert Watson 2907c3b31afdSRobert Watson if (ifma->ifma_lladdr != NULL) 29081ede983cSDag-Erling Smørgrav free(ifma->ifma_lladdr, M_IFMADDR); 29091ede983cSDag-Erling Smørgrav free(ifma->ifma_addr, M_IFMADDR); 29101ede983cSDag-Erling Smørgrav free(ifma, M_IFMADDR); 2911c3b31afdSRobert Watson } 2912c3b31afdSRobert Watson 2913c3b31afdSRobert Watson /* 2914c3b31afdSRobert Watson * Register an additional multicast address with a network interface. 2915c3b31afdSRobert Watson * 2916c3b31afdSRobert Watson * - If the address is already present, bump the reference count on the 2917c3b31afdSRobert Watson * address and return. 2918c3b31afdSRobert Watson * - If the address is not link-layer, look up a link layer address. 2919c3b31afdSRobert Watson * - Allocate address structures for one or both addresses, and attach to the 2920c3b31afdSRobert Watson * multicast address list on the interface. If automatically adding a link 2921c3b31afdSRobert Watson * layer address, the protocol address will own a reference to the link 2922c3b31afdSRobert Watson * layer address, to be freed when it is freed. 2923c3b31afdSRobert Watson * - Notify the network device driver of an addition to the multicast address 2924c3b31afdSRobert Watson * list. 2925c3b31afdSRobert Watson * 2926c3b31afdSRobert Watson * 'sa' points to caller-owned memory with the desired multicast address. 2927c3b31afdSRobert Watson * 2928c3b31afdSRobert Watson * 'retifma' will be used to return a pointer to the resulting multicast 2929c3b31afdSRobert Watson * address reference, if desired. 2930c3b31afdSRobert Watson */ 2931c3b31afdSRobert Watson int 2932c3b31afdSRobert Watson if_addmulti(struct ifnet *ifp, struct sockaddr *sa, 2933c3b31afdSRobert Watson struct ifmultiaddr **retifma) 2934c3b31afdSRobert Watson { 2935c3b31afdSRobert Watson struct ifmultiaddr *ifma, *ll_ifma; 2936c3b31afdSRobert Watson struct sockaddr *llsa; 2937c3b31afdSRobert Watson int error; 2938c3b31afdSRobert Watson 2939c3b31afdSRobert Watson /* 2940c3b31afdSRobert Watson * If the address is already present, return a new reference to it; 2941c3b31afdSRobert Watson * otherwise, allocate storage and set up a new address. 2942c3b31afdSRobert Watson */ 2943c3b31afdSRobert Watson IF_ADDR_LOCK(ifp); 2944c3b31afdSRobert Watson ifma = if_findmulti(ifp, sa); 2945c3b31afdSRobert Watson if (ifma != NULL) { 2946c3b31afdSRobert Watson ifma->ifma_refcount++; 2947c3b31afdSRobert Watson if (retifma != NULL) 2948c3b31afdSRobert Watson *retifma = ifma; 2949c3b31afdSRobert Watson IF_ADDR_UNLOCK(ifp); 2950c3b31afdSRobert Watson return (0); 2951c3b31afdSRobert Watson } 2952c3b31afdSRobert Watson 2953c3b31afdSRobert Watson /* 2954c3b31afdSRobert Watson * The address isn't already present; resolve the protocol address 2955c3b31afdSRobert Watson * into a link layer address, and then look that up, bump its 2956c3b31afdSRobert Watson * refcount or allocate an ifma for that also. If 'llsa' was 2957c3b31afdSRobert Watson * returned, we will need to free it later. 2958c3b31afdSRobert Watson */ 2959c3b31afdSRobert Watson llsa = NULL; 2960c3b31afdSRobert Watson ll_ifma = NULL; 2961c3b31afdSRobert Watson if (ifp->if_resolvemulti != NULL) { 2962c3b31afdSRobert Watson error = ifp->if_resolvemulti(ifp, &llsa, sa); 2963c3b31afdSRobert Watson if (error) 2964c3b31afdSRobert Watson goto unlock_out; 2965c3b31afdSRobert Watson } 2966c3b31afdSRobert Watson 2967c3b31afdSRobert Watson /* 2968c3b31afdSRobert Watson * Allocate the new address. Don't hook it up yet, as we may also 2969c3b31afdSRobert Watson * need to allocate a link layer multicast address. 2970c3b31afdSRobert Watson */ 2971c3b31afdSRobert Watson ifma = if_allocmulti(ifp, sa, llsa, M_NOWAIT); 2972c3b31afdSRobert Watson if (ifma == NULL) { 2973c3b31afdSRobert Watson error = ENOMEM; 2974c3b31afdSRobert Watson goto free_llsa_out; 2975c3b31afdSRobert Watson } 2976c3b31afdSRobert Watson 2977c3b31afdSRobert Watson /* 2978c3b31afdSRobert Watson * If a link layer address is found, we'll need to see if it's 2979c3b31afdSRobert Watson * already present in the address list, or allocate is as well. 2980c3b31afdSRobert Watson * When this block finishes, the link layer address will be on the 2981c3b31afdSRobert Watson * list. 2982c3b31afdSRobert Watson */ 2983c3b31afdSRobert Watson if (llsa != NULL) { 2984c3b31afdSRobert Watson ll_ifma = if_findmulti(ifp, llsa); 2985c3b31afdSRobert Watson if (ll_ifma == NULL) { 2986c3b31afdSRobert Watson ll_ifma = if_allocmulti(ifp, llsa, NULL, M_NOWAIT); 2987c3b31afdSRobert Watson if (ll_ifma == NULL) { 2988ec002feeSBruce M Simpson --ifma->ifma_refcount; 2989c3b31afdSRobert Watson if_freemulti(ifma); 2990c3b31afdSRobert Watson error = ENOMEM; 2991c3b31afdSRobert Watson goto free_llsa_out; 2992c3b31afdSRobert Watson } 2993c3b31afdSRobert Watson TAILQ_INSERT_HEAD(&ifp->if_multiaddrs, ll_ifma, 2994c3b31afdSRobert Watson ifma_link); 2995c3b31afdSRobert Watson } else 2996c3b31afdSRobert Watson ll_ifma->ifma_refcount++; 2997ec002feeSBruce M Simpson ifma->ifma_llifma = ll_ifma; 2998c3b31afdSRobert Watson } 2999c3b31afdSRobert Watson 3000c3b31afdSRobert Watson /* 3001c3b31afdSRobert Watson * We now have a new multicast address, ifma, and possibly a new or 3002c3b31afdSRobert Watson * referenced link layer address. Add the primary address to the 3003c3b31afdSRobert Watson * ifnet address list. 3004c3b31afdSRobert Watson */ 30056817526dSPoul-Henning Kamp TAILQ_INSERT_HEAD(&ifp->if_multiaddrs, ifma, ifma_link); 3006c3b31afdSRobert Watson 300713990766SJonathan Mini if (retifma != NULL) 3008373f88edSGarrett Wollman *retifma = ifma; 30091158dfb7SGarrett Wollman 3010c3b31afdSRobert Watson /* 3011c3b31afdSRobert Watson * Must generate the message while holding the lock so that 'ifma' 3012c3b31afdSRobert Watson * pointer is still valid. 3013c3b31afdSRobert Watson */ 3014c3b31afdSRobert Watson rt_newmaddrmsg(RTM_NEWMADDR, ifma); 3015c3b31afdSRobert Watson IF_ADDR_UNLOCK(ifp); 3016c3b31afdSRobert Watson 30171158dfb7SGarrett Wollman /* 30181158dfb7SGarrett Wollman * We are certain we have added something, so call down to the 30191158dfb7SGarrett Wollman * interface to let them know about it. 30201158dfb7SGarrett Wollman */ 30212432c31cSRobert Watson if (ifp->if_ioctl != NULL) { 30221a3b6859SYaroslav Tykhiy (void) (*ifp->if_ioctl)(ifp, SIOCADDMULTI, 0); 30231a3b6859SYaroslav Tykhiy } 30241158dfb7SGarrett Wollman 3025c3b31afdSRobert Watson if (llsa != NULL) 30261ede983cSDag-Erling Smørgrav free(llsa, M_IFMADDR); 3027c3b31afdSRobert Watson 3028c3b31afdSRobert Watson return (0); 3029c3b31afdSRobert Watson 3030c3b31afdSRobert Watson free_llsa_out: 3031c3b31afdSRobert Watson if (llsa != NULL) 30321ede983cSDag-Erling Smørgrav free(llsa, M_IFMADDR); 3033c3b31afdSRobert Watson 3034c3b31afdSRobert Watson unlock_out: 3035c3b31afdSRobert Watson IF_ADDR_UNLOCK(ifp); 3036c3b31afdSRobert Watson return (error); 30371158dfb7SGarrett Wollman } 30381158dfb7SGarrett Wollman 30391158dfb7SGarrett Wollman /* 3040ec002feeSBruce M Simpson * Delete a multicast group membership by network-layer group address. 3041ec002feeSBruce M Simpson * 3042ec002feeSBruce M Simpson * Returns ENOENT if the entry could not be found. If ifp no longer 3043ec002feeSBruce M Simpson * exists, results are undefined. This entry point should only be used 3044ec002feeSBruce M Simpson * from subsystems which do appropriate locking to hold ifp for the 3045ec002feeSBruce M Simpson * duration of the call. 3046ec002feeSBruce M Simpson * Network-layer protocol domains must use if_delmulti_ifma(). 30471158dfb7SGarrett Wollman */ 30481158dfb7SGarrett Wollman int 304972fd1b6aSDag-Erling Smørgrav if_delmulti(struct ifnet *ifp, struct sockaddr *sa) 30501158dfb7SGarrett Wollman { 3051ec002feeSBruce M Simpson struct ifmultiaddr *ifma; 3052ec002feeSBruce M Simpson int lastref; 3053ec002feeSBruce M Simpson #ifdef INVARIANTS 3054ec002feeSBruce M Simpson struct ifnet *oifp; 3055ec002feeSBruce M Simpson 305677dfcdc4SRobert Watson IFNET_RLOCK_NOSLEEP(); 3057603724d3SBjoern A. Zeeb TAILQ_FOREACH(oifp, &V_ifnet, if_link) 3058ec002feeSBruce M Simpson if (ifp == oifp) 3059ec002feeSBruce M Simpson break; 3060ec002feeSBruce M Simpson if (ifp != oifp) 3061ec002feeSBruce M Simpson ifp = NULL; 306277dfcdc4SRobert Watson IFNET_RUNLOCK_NOSLEEP(); 3063ec002feeSBruce M Simpson 3064ec002feeSBruce M Simpson KASSERT(ifp != NULL, ("%s: ifnet went away", __func__)); 3065ec002feeSBruce M Simpson #endif 3066ec002feeSBruce M Simpson if (ifp == NULL) 3067ec002feeSBruce M Simpson return (ENOENT); 30681158dfb7SGarrett Wollman 3069c3b31afdSRobert Watson IF_ADDR_LOCK(ifp); 3070ec002feeSBruce M Simpson lastref = 0; 3071c3b31afdSRobert Watson ifma = if_findmulti(ifp, sa); 3072ec002feeSBruce M Simpson if (ifma != NULL) 3073ec002feeSBruce M Simpson lastref = if_delmulti_locked(ifp, ifma, 0); 3074c3b31afdSRobert Watson IF_ADDR_UNLOCK(ifp); 3075c3b31afdSRobert Watson 3076ec002feeSBruce M Simpson if (ifma == NULL) 3077ec002feeSBruce M Simpson return (ENOENT); 3078ec002feeSBruce M Simpson 3079ec002feeSBruce M Simpson if (lastref && ifp->if_ioctl != NULL) { 30801a3b6859SYaroslav Tykhiy (void)(*ifp->if_ioctl)(ifp, SIOCDELMULTI, 0); 308131302ebfSRobert Watson } 30821158dfb7SGarrett Wollman 3083ec002feeSBruce M Simpson return (0); 3084ec002feeSBruce M Simpson } 3085ec002feeSBruce M Simpson 3086ec002feeSBruce M Simpson /* 308793ec7edcSShteryana Shopova * Delete all multicast group membership for an interface. 308893ec7edcSShteryana Shopova * Should be used to quickly flush all multicast filters. 308993ec7edcSShteryana Shopova */ 309093ec7edcSShteryana Shopova void 309193ec7edcSShteryana Shopova if_delallmulti(struct ifnet *ifp) 309293ec7edcSShteryana Shopova { 309393ec7edcSShteryana Shopova struct ifmultiaddr *ifma; 309493ec7edcSShteryana Shopova struct ifmultiaddr *next; 309593ec7edcSShteryana Shopova 309693ec7edcSShteryana Shopova IF_ADDR_LOCK(ifp); 309793ec7edcSShteryana Shopova TAILQ_FOREACH_SAFE(ifma, &ifp->if_multiaddrs, ifma_link, next) 309893ec7edcSShteryana Shopova if_delmulti_locked(ifp, ifma, 0); 309993ec7edcSShteryana Shopova IF_ADDR_UNLOCK(ifp); 310093ec7edcSShteryana Shopova } 310193ec7edcSShteryana Shopova 310293ec7edcSShteryana Shopova /* 3103ec002feeSBruce M Simpson * Delete a multicast group membership by group membership pointer. 3104ec002feeSBruce M Simpson * Network-layer protocol domains must use this routine. 3105ec002feeSBruce M Simpson * 3106e5adda3dSRobert Watson * It is safe to call this routine if the ifp disappeared. 3107ec002feeSBruce M Simpson */ 3108ec002feeSBruce M Simpson void 3109ec002feeSBruce M Simpson if_delmulti_ifma(struct ifmultiaddr *ifma) 3110ec002feeSBruce M Simpson { 3111ec002feeSBruce M Simpson struct ifnet *ifp; 3112ec002feeSBruce M Simpson int lastref; 3113ec002feeSBruce M Simpson 3114ec002feeSBruce M Simpson ifp = ifma->ifma_ifp; 3115ec002feeSBruce M Simpson #ifdef DIAGNOSTIC 3116ec002feeSBruce M Simpson if (ifp == NULL) { 3117ec002feeSBruce M Simpson printf("%s: ifma_ifp seems to be detached\n", __func__); 3118ec002feeSBruce M Simpson } else { 3119ec002feeSBruce M Simpson struct ifnet *oifp; 3120ec002feeSBruce M Simpson 312177dfcdc4SRobert Watson IFNET_RLOCK_NOSLEEP(); 3122603724d3SBjoern A. Zeeb TAILQ_FOREACH(oifp, &V_ifnet, if_link) 3123ec002feeSBruce M Simpson if (ifp == oifp) 3124ec002feeSBruce M Simpson break; 3125ec002feeSBruce M Simpson if (ifp != oifp) { 3126ec002feeSBruce M Simpson printf("%s: ifnet %p disappeared\n", __func__, ifp); 3127ec002feeSBruce M Simpson ifp = NULL; 3128ec002feeSBruce M Simpson } 312977dfcdc4SRobert Watson IFNET_RUNLOCK_NOSLEEP(); 3130ec002feeSBruce M Simpson } 3131ec002feeSBruce M Simpson #endif 3132ec002feeSBruce M Simpson /* 3133ec002feeSBruce M Simpson * If and only if the ifnet instance exists: Acquire the address lock. 3134ec002feeSBruce M Simpson */ 3135ec002feeSBruce M Simpson if (ifp != NULL) 3136ec002feeSBruce M Simpson IF_ADDR_LOCK(ifp); 3137ec002feeSBruce M Simpson 3138ec002feeSBruce M Simpson lastref = if_delmulti_locked(ifp, ifma, 0); 3139ec002feeSBruce M Simpson 3140ec002feeSBruce M Simpson if (ifp != NULL) { 3141ec002feeSBruce M Simpson /* 3142ec002feeSBruce M Simpson * If and only if the ifnet instance exists: 3143ec002feeSBruce M Simpson * Release the address lock. 3144ec002feeSBruce M Simpson * If the group was left: update the hardware hash filter. 3145ec002feeSBruce M Simpson */ 3146ec002feeSBruce M Simpson IF_ADDR_UNLOCK(ifp); 3147ec002feeSBruce M Simpson if (lastref && ifp->if_ioctl != NULL) { 3148ec002feeSBruce M Simpson (void)(*ifp->if_ioctl)(ifp, SIOCDELMULTI, 0); 3149ec002feeSBruce M Simpson } 3150ec002feeSBruce M Simpson } 3151ec002feeSBruce M Simpson } 3152ec002feeSBruce M Simpson 3153ec002feeSBruce M Simpson /* 3154ec002feeSBruce M Simpson * Perform deletion of network-layer and/or link-layer multicast address. 3155ec002feeSBruce M Simpson * 3156ec002feeSBruce M Simpson * Return 0 if the reference count was decremented. 3157ec002feeSBruce M Simpson * Return 1 if the final reference was released, indicating that the 3158ec002feeSBruce M Simpson * hardware hash filter should be reprogrammed. 3159ec002feeSBruce M Simpson */ 3160ec002feeSBruce M Simpson static int 3161ec002feeSBruce M Simpson if_delmulti_locked(struct ifnet *ifp, struct ifmultiaddr *ifma, int detaching) 3162ec002feeSBruce M Simpson { 3163ec002feeSBruce M Simpson struct ifmultiaddr *ll_ifma; 3164ec002feeSBruce M Simpson 3165ec002feeSBruce M Simpson if (ifp != NULL && ifma->ifma_ifp != NULL) { 3166ec002feeSBruce M Simpson KASSERT(ifma->ifma_ifp == ifp, 3167ec002feeSBruce M Simpson ("%s: inconsistent ifp %p", __func__, ifp)); 3168ec002feeSBruce M Simpson IF_ADDR_LOCK_ASSERT(ifp); 3169ec002feeSBruce M Simpson } 3170ec002feeSBruce M Simpson 3171ec002feeSBruce M Simpson ifp = ifma->ifma_ifp; 3172ec002feeSBruce M Simpson 3173ec002feeSBruce M Simpson /* 3174ec002feeSBruce M Simpson * If the ifnet is detaching, null out references to ifnet, 3175ec002feeSBruce M Simpson * so that upper protocol layers will notice, and not attempt 317675ae0c01SBruce M Simpson * to obtain locks for an ifnet which no longer exists. The 317775ae0c01SBruce M Simpson * routing socket announcement must happen before the ifnet 317875ae0c01SBruce M Simpson * instance is detached from the system. 3179ec002feeSBruce M Simpson */ 3180ec002feeSBruce M Simpson if (detaching) { 3181ec002feeSBruce M Simpson #ifdef DIAGNOSTIC 3182ec002feeSBruce M Simpson printf("%s: detaching ifnet instance %p\n", __func__, ifp); 3183ec002feeSBruce M Simpson #endif 318475ae0c01SBruce M Simpson /* 318575ae0c01SBruce M Simpson * ifp may already be nulled out if we are being reentered 318675ae0c01SBruce M Simpson * to delete the ll_ifma. 318775ae0c01SBruce M Simpson */ 318875ae0c01SBruce M Simpson if (ifp != NULL) { 318975ae0c01SBruce M Simpson rt_newmaddrmsg(RTM_DELMADDR, ifma); 3190ec002feeSBruce M Simpson ifma->ifma_ifp = NULL; 3191ec002feeSBruce M Simpson } 319275ae0c01SBruce M Simpson } 3193ec002feeSBruce M Simpson 3194ec002feeSBruce M Simpson if (--ifma->ifma_refcount > 0) 31951158dfb7SGarrett Wollman return 0; 3196ec002feeSBruce M Simpson 3197ec002feeSBruce M Simpson /* 3198ec002feeSBruce M Simpson * If this ifma is a network-layer ifma, a link-layer ifma may 3199ec002feeSBruce M Simpson * have been associated with it. Release it first if so. 3200ec002feeSBruce M Simpson */ 3201ec002feeSBruce M Simpson ll_ifma = ifma->ifma_llifma; 3202ec002feeSBruce M Simpson if (ll_ifma != NULL) { 3203ec002feeSBruce M Simpson KASSERT(ifma->ifma_lladdr != NULL, 3204ec002feeSBruce M Simpson ("%s: llifma w/o lladdr", __func__)); 3205ec002feeSBruce M Simpson if (detaching) 3206ec002feeSBruce M Simpson ll_ifma->ifma_ifp = NULL; /* XXX */ 3207ec002feeSBruce M Simpson if (--ll_ifma->ifma_refcount == 0) { 3208ec002feeSBruce M Simpson if (ifp != NULL) { 3209ec002feeSBruce M Simpson TAILQ_REMOVE(&ifp->if_multiaddrs, ll_ifma, 3210ec002feeSBruce M Simpson ifma_link); 3211ec002feeSBruce M Simpson } 3212ec002feeSBruce M Simpson if_freemulti(ll_ifma); 3213ec002feeSBruce M Simpson } 3214ec002feeSBruce M Simpson } 3215ec002feeSBruce M Simpson 3216ec002feeSBruce M Simpson if (ifp != NULL) 3217ec002feeSBruce M Simpson TAILQ_REMOVE(&ifp->if_multiaddrs, ifma, ifma_link); 3218ec002feeSBruce M Simpson 3219ec002feeSBruce M Simpson if_freemulti(ifma); 3220ec002feeSBruce M Simpson 3221ec002feeSBruce M Simpson /* 3222ec002feeSBruce M Simpson * The last reference to this instance of struct ifmultiaddr 3223ec002feeSBruce M Simpson * was released; the hardware should be notified of this change. 3224ec002feeSBruce M Simpson */ 3225ec002feeSBruce M Simpson return 1; 32261158dfb7SGarrett Wollman } 32271158dfb7SGarrett Wollman 322866ce51ceSArchie Cobbs /* 322966ce51ceSArchie Cobbs * Set the link layer address on an interface. 323066ce51ceSArchie Cobbs * 323166ce51ceSArchie Cobbs * At this time we only support certain types of interfaces, 323266ce51ceSArchie Cobbs * and we don't allow the length of the address to change. 323366ce51ceSArchie Cobbs */ 323466ce51ceSArchie Cobbs int 323566ce51ceSArchie Cobbs if_setlladdr(struct ifnet *ifp, const u_char *lladdr, int len) 323666ce51ceSArchie Cobbs { 323766ce51ceSArchie Cobbs struct sockaddr_dl *sdl; 323866ce51ceSArchie Cobbs struct ifaddr *ifa; 3239d637e989SPeter Wemm struct ifreq ifr; 324066ce51ceSArchie Cobbs 32413baaf297SRobert Watson IF_ADDR_LOCK(ifp); 32424a0d6638SRuslan Ermilov ifa = ifp->if_addr; 32433baaf297SRobert Watson if (ifa == NULL) { 32443baaf297SRobert Watson IF_ADDR_UNLOCK(ifp); 324566ce51ceSArchie Cobbs return (EINVAL); 32463baaf297SRobert Watson } 32473baaf297SRobert Watson ifa_ref(ifa); 32483baaf297SRobert Watson IF_ADDR_UNLOCK(ifp); 324966ce51ceSArchie Cobbs sdl = (struct sockaddr_dl *)ifa->ifa_addr; 32503baaf297SRobert Watson if (sdl == NULL) { 32513baaf297SRobert Watson ifa_free(ifa); 325266ce51ceSArchie Cobbs return (EINVAL); 32533baaf297SRobert Watson } 32543baaf297SRobert Watson if (len != sdl->sdl_alen) { /* don't allow length to change */ 32553baaf297SRobert Watson ifa_free(ifa); 325666ce51ceSArchie Cobbs return (EINVAL); 32573baaf297SRobert Watson } 325866ce51ceSArchie Cobbs switch (ifp->if_type) { 3259d09ed26fSRuslan Ermilov case IFT_ETHER: 326066ce51ceSArchie Cobbs case IFT_FDDI: 326166ce51ceSArchie Cobbs case IFT_XETHER: 326266ce51ceSArchie Cobbs case IFT_ISO88025: 3263b7bffa71SYaroslav Tykhiy case IFT_L2VLAN: 32648f867517SAndrew Thompson case IFT_BRIDGE: 32656cdcc159SMax Khon case IFT_ARCNET: 3266b47888ceSAndrew Thompson case IFT_IEEE8023ADLAG: 3267a51f44a7SSam Leffler case IFT_IEEE80211: 326866ce51ceSArchie Cobbs bcopy(lladdr, LLADDR(sdl), len); 32693baaf297SRobert Watson ifa_free(ifa); 327066ce51ceSArchie Cobbs break; 327166ce51ceSArchie Cobbs default: 32723baaf297SRobert Watson ifa_free(ifa); 327366ce51ceSArchie Cobbs return (ENODEV); 327466ce51ceSArchie Cobbs } 32753baaf297SRobert Watson 327666ce51ceSArchie Cobbs /* 327766ce51ceSArchie Cobbs * If the interface is already up, we need 327866ce51ceSArchie Cobbs * to re-init it in order to reprogram its 327966ce51ceSArchie Cobbs * address filter. 328066ce51ceSArchie Cobbs */ 328166ce51ceSArchie Cobbs if ((ifp->if_flags & IFF_UP) != 0) { 32821a3b6859SYaroslav Tykhiy if (ifp->if_ioctl) { 328366ce51ceSArchie Cobbs ifp->if_flags &= ~IFF_UP; 328462f76486SMaxim Sobolev ifr.ifr_flags = ifp->if_flags & 0xffff; 328562f76486SMaxim Sobolev ifr.ifr_flagshigh = ifp->if_flags >> 16; 3286ee0a4f7eSSUZUKI Shinsuke (*ifp->if_ioctl)(ifp, SIOCSIFFLAGS, (caddr_t)&ifr); 328766ce51ceSArchie Cobbs ifp->if_flags |= IFF_UP; 328862f76486SMaxim Sobolev ifr.ifr_flags = ifp->if_flags & 0xffff; 328962f76486SMaxim Sobolev ifr.ifr_flagshigh = ifp->if_flags >> 16; 3290ee0a4f7eSSUZUKI Shinsuke (*ifp->if_ioctl)(ifp, SIOCSIFFLAGS, (caddr_t)&ifr); 32911a3b6859SYaroslav Tykhiy } 3292b2c08f43SLuigi Rizzo #ifdef INET 3293b2c08f43SLuigi Rizzo /* 3294b2c08f43SLuigi Rizzo * Also send gratuitous ARPs to notify other nodes about 3295b2c08f43SLuigi Rizzo * the address change. 3296b2c08f43SLuigi Rizzo */ 3297b2c08f43SLuigi Rizzo TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) { 32984b97d7afSYaroslav Tykhiy if (ifa->ifa_addr->sa_family == AF_INET) 3299c0933269SPeter Wemm arp_ifinit(ifp, ifa); 3300b2c08f43SLuigi Rizzo } 3301b2c08f43SLuigi Rizzo #endif 330266ce51ceSArchie Cobbs } 330366ce51ceSArchie Cobbs return (0); 330466ce51ceSArchie Cobbs } 330566ce51ceSArchie Cobbs 33069bf40edeSBrooks Davis /* 33079bf40edeSBrooks Davis * The name argument must be a pointer to storage which will last as 33089bf40edeSBrooks Davis * long as the interface does. For physical devices, the result of 33099bf40edeSBrooks Davis * device_get_name(dev) is a good choice and for pseudo-devices a 33109bf40edeSBrooks Davis * static string works well. 33119bf40edeSBrooks Davis */ 33129bf40edeSBrooks Davis void 33139bf40edeSBrooks Davis if_initname(struct ifnet *ifp, const char *name, int unit) 33149bf40edeSBrooks Davis { 33159bf40edeSBrooks Davis ifp->if_dname = name; 33169bf40edeSBrooks Davis ifp->if_dunit = unit; 33179bf40edeSBrooks Davis if (unit != IF_DUNIT_NONE) 33189bf40edeSBrooks Davis snprintf(ifp->if_xname, IFNAMSIZ, "%s%d", name, unit); 33199bf40edeSBrooks Davis else 33209bf40edeSBrooks Davis strlcpy(ifp->if_xname, name, IFNAMSIZ); 33219bf40edeSBrooks Davis } 33229bf40edeSBrooks Davis 3323fa882e87SBrooks Davis int 3324fa882e87SBrooks Davis if_printf(struct ifnet *ifp, const char * fmt, ...) 3325fa882e87SBrooks Davis { 3326fa882e87SBrooks Davis va_list ap; 3327fa882e87SBrooks Davis int retval; 3328fa882e87SBrooks Davis 33299bf40edeSBrooks Davis retval = printf("%s: ", ifp->if_xname); 3330fa882e87SBrooks Davis va_start(ap, fmt); 3331fa882e87SBrooks Davis retval += vprintf(fmt, ap); 3332fa882e87SBrooks Davis va_end(ap); 3333fa882e87SBrooks Davis return (retval); 3334fa882e87SBrooks Davis } 3335fa882e87SBrooks Davis 3336af5e59bfSRobert Watson void 3337af5e59bfSRobert Watson if_start(struct ifnet *ifp) 3338af5e59bfSRobert Watson { 3339af5e59bfSRobert Watson 3340af5e59bfSRobert Watson (*(ifp)->if_start)(ifp); 3341af5e59bfSRobert Watson } 3342af5e59bfSRobert Watson 3343db7f0b97SKip Macy /* 3344db7f0b97SKip Macy * Backwards compatibility interface for drivers 3345db7f0b97SKip Macy * that have not implemented it 3346db7f0b97SKip Macy */ 3347db7f0b97SKip Macy static int 3348db7f0b97SKip Macy if_transmit(struct ifnet *ifp, struct mbuf *m) 3349db7f0b97SKip Macy { 3350db7f0b97SKip Macy int error; 3351db7f0b97SKip Macy 3352db7f0b97SKip Macy IFQ_HANDOFF(ifp, m, error); 3353db7f0b97SKip Macy return (error); 3354db7f0b97SKip Macy } 3355db7f0b97SKip Macy 33560b762445SRobert Watson int 33570b762445SRobert Watson if_handoff(struct ifqueue *ifq, struct mbuf *m, struct ifnet *ifp, int adjust) 33580b762445SRobert Watson { 33590b762445SRobert Watson int active = 0; 33600b762445SRobert Watson 33610b762445SRobert Watson IF_LOCK(ifq); 33620b762445SRobert Watson if (_IF_QFULL(ifq)) { 33630b762445SRobert Watson _IF_DROP(ifq); 33640b762445SRobert Watson IF_UNLOCK(ifq); 33650b762445SRobert Watson m_freem(m); 33660b762445SRobert Watson return (0); 33670b762445SRobert Watson } 33680b762445SRobert Watson if (ifp != NULL) { 33690b762445SRobert Watson ifp->if_obytes += m->m_pkthdr.len + adjust; 33700b762445SRobert Watson if (m->m_flags & (M_BCAST|M_MCAST)) 33710b762445SRobert Watson ifp->if_omcasts++; 3372292ee7beSRobert Watson active = ifp->if_drv_flags & IFF_DRV_OACTIVE; 33730b762445SRobert Watson } 33740b762445SRobert Watson _IF_ENQUEUE(ifq, m); 33750b762445SRobert Watson IF_UNLOCK(ifq); 33760b762445SRobert Watson if (ifp != NULL && !active) 3377e5adda3dSRobert Watson (*(ifp)->if_start)(ifp); 33780b762445SRobert Watson return (1); 33790b762445SRobert Watson } 3380fc74a9f9SBrooks Davis 3381fc74a9f9SBrooks Davis void 3382fc74a9f9SBrooks Davis if_register_com_alloc(u_char type, 3383fc74a9f9SBrooks Davis if_com_alloc_t *a, if_com_free_t *f) 3384fc74a9f9SBrooks Davis { 3385fc74a9f9SBrooks Davis 3386fc74a9f9SBrooks Davis KASSERT(if_com_alloc[type] == NULL, 3387fc74a9f9SBrooks Davis ("if_register_com_alloc: %d already registered", type)); 3388fc74a9f9SBrooks Davis KASSERT(if_com_free[type] == NULL, 3389fc74a9f9SBrooks Davis ("if_register_com_alloc: %d free already registered", type)); 3390fc74a9f9SBrooks Davis 3391fc74a9f9SBrooks Davis if_com_alloc[type] = a; 3392fc74a9f9SBrooks Davis if_com_free[type] = f; 3393fc74a9f9SBrooks Davis } 3394fc74a9f9SBrooks Davis 3395fc74a9f9SBrooks Davis void 3396fc74a9f9SBrooks Davis if_deregister_com_alloc(u_char type) 3397fc74a9f9SBrooks Davis { 3398fc74a9f9SBrooks Davis 3399affcaf78SMax Khon KASSERT(if_com_alloc[type] != NULL, 3400fc74a9f9SBrooks Davis ("if_deregister_com_alloc: %d not registered", type)); 3401affcaf78SMax Khon KASSERT(if_com_free[type] != NULL, 3402fc74a9f9SBrooks Davis ("if_deregister_com_alloc: %d free not registered", type)); 3403fc74a9f9SBrooks Davis if_com_alloc[type] = NULL; 3404fc74a9f9SBrooks Davis if_com_free[type] = NULL; 3405fc74a9f9SBrooks Davis } 3406