[BACK]Return to if.c CVS log [TXT][DIR] Up to [cvs.NetBSD.org] / src / sys / net

Annotation of src/sys/net/if.c, Revision 1.408

1.408   ! ozaki-r     1: /*     $NetBSD: if.c,v 1.407 2017/12/07 01:23:53 ozaki-r Exp $ */
1.53      thorpej     2:
                      3: /*-
1.219     ad          4:  * Copyright (c) 1999, 2000, 2001, 2008 The NetBSD Foundation, Inc.
1.53      thorpej     5:  * All rights reserved.
                      6:  *
                      7:  * This code is derived from software contributed to The NetBSD Foundation
1.150     peter       8:  * by William Studenmund and Jason R. Thorpe.
1.53      thorpej     9:  *
                     10:  * Redistribution and use in source and binary forms, with or without
                     11:  * modification, are permitted provided that the following conditions
                     12:  * are met:
                     13:  * 1. Redistributions of source code must retain the above copyright
                     14:  *    notice, this list of conditions and the following disclaimer.
                     15:  * 2. Redistributions in binary form must reproduce the above copyright
                     16:  *    notice, this list of conditions and the following disclaimer in the
                     17:  *    documentation and/or other materials provided with the distribution.
                     18:  *
                     19:  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
                     20:  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
                     21:  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
                     22:  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
                     23:  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
                     24:  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
                     25:  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
                     26:  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
                     27:  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
                     28:  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
                     29:  * POSSIBILITY OF SUCH DAMAGE.
                     30:  */
1.49      itojun     31:
                     32: /*
                     33:  * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
                     34:  * All rights reserved.
1.154     perry      35:  *
1.49      itojun     36:  * Redistribution and use in source and binary forms, with or without
                     37:  * modification, are permitted provided that the following conditions
                     38:  * are met:
                     39:  * 1. Redistributions of source code must retain the above copyright
                     40:  *    notice, this list of conditions and the following disclaimer.
                     41:  * 2. Redistributions in binary form must reproduce the above copyright
                     42:  *    notice, this list of conditions and the following disclaimer in the
                     43:  *    documentation and/or other materials provided with the distribution.
                     44:  * 3. Neither the name of the project nor the names of its contributors
                     45:  *    may be used to endorse or promote products derived from this software
                     46:  *    without specific prior written permission.
1.154     perry      47:  *
1.49      itojun     48:  * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
                     49:  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
                     50:  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
                     51:  * ARE DISCLAIMED.  IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
                     52:  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
                     53:  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
                     54:  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
                     55:  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
                     56:  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
                     57:  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
                     58:  * SUCH DAMAGE.
                     59:  */
1.16      cgd        60:
1.1       cgd        61: /*
1.15      mycroft    62:  * Copyright (c) 1980, 1986, 1993
                     63:  *     The Regents of the University of California.  All rights reserved.
1.1       cgd        64:  *
                     65:  * Redistribution and use in source and binary forms, with or without
                     66:  * modification, are permitted provided that the following conditions
                     67:  * are met:
                     68:  * 1. Redistributions of source code must retain the above copyright
                     69:  *    notice, this list of conditions and the following disclaimer.
                     70:  * 2. Redistributions in binary form must reproduce the above copyright
                     71:  *    notice, this list of conditions and the following disclaimer in the
                     72:  *    documentation and/or other materials provided with the distribution.
1.126     agc        73:  * 3. Neither the name of the University nor the names of its contributors
1.1       cgd        74:  *    may be used to endorse or promote products derived from this software
                     75:  *    without specific prior written permission.
                     76:  *
                     77:  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
                     78:  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
                     79:  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
                     80:  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
                     81:  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
                     82:  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
                     83:  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
                     84:  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
                     85:  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
                     86:  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
                     87:  * SUCH DAMAGE.
                     88:  *
1.44      fvdl       89:  *     @(#)if.c        8.5 (Berkeley) 1/9/95
1.1       cgd        90:  */
1.99      lukem      91:
                     92: #include <sys/cdefs.h>
1.408   ! ozaki-r    93: __KERNEL_RCSID(0, "$NetBSD: if.c,v 1.407 2017/12/07 01:23:53 ozaki-r Exp $");
1.50      thorpej    94:
1.308     ozaki-r    95: #if defined(_KERNEL_OPT)
1.50      thorpej    96: #include "opt_inet.h"
1.352     knakahar   97: #include "opt_ipsec.h"
1.46      thorpej    98:
1.51      bouyer     99: #include "opt_atalk.h"
1.120     martin    100: #include "opt_natm.h"
1.288     ozaki-r   101: #include "opt_wlan.h"
1.308     ozaki-r   102: #include "opt_net_mpsafe.h"
                    103: #endif
1.1       cgd       104:
1.8       mycroft   105: #include <sys/param.h>
                    106: #include <sys/mbuf.h>
                    107: #include <sys/systm.h>
1.59      thorpej   108: #include <sys/callout.h>
1.15      mycroft   109: #include <sys/proc.h>
1.8       mycroft   110: #include <sys/socket.h>
                    111: #include <sys/socketvar.h>
1.56      thorpej   112: #include <sys/domain.h>
1.8       mycroft   113: #include <sys/protosw.h>
                    114: #include <sys/kernel.h>
                    115: #include <sys/ioctl.h>
1.133     jonathan  116: #include <sys/sysctl.h>
1.159     dyoung    117: #include <sys/syslog.h>
1.165     elad      118: #include <sys/kauth.h>
1.254     dyoung    119: #include <sys/kmem.h>
1.276     rmind     120: #include <sys/xcall.h>
1.323     ozaki-r   121: #include <sys/cpu.h>
                    122: #include <sys/intr.h>
1.1       cgd       123:
1.8       mycroft   124: #include <net/if.h>
                    125: #include <net/if_dl.h>
1.66      onoe      126: #include <net/if_ether.h>
1.124     dyoung    127: #include <net/if_media.h>
1.132     dyoung    128: #include <net80211/ieee80211.h>
                    129: #include <net80211/ieee80211_ioctl.h>
1.8       mycroft   130: #include <net/if_types.h>
1.53      thorpej   131: #include <net/route.h>
1.95      itojun    132: #include <net/netisr.h>
1.262     christos  133: #include <sys/module.h>
1.51      bouyer    134: #ifdef NETATALK
                    135: #include <netatalk/at_extern.h>
                    136: #include <netatalk/at.h>
                    137: #endif
1.143     itojun    138: #include <net/pfil.h>
1.278     he        139: #include <netinet/in.h>
1.276     rmind     140: #include <netinet/in_var.h>
1.352     knakahar  141: #include <netinet/ip_encap.h>
1.368     ozaki-r   142: #include <net/bpf.h>
1.1       cgd       143:
1.49      itojun    144: #ifdef INET6
1.72      thorpej   145: #include <netinet6/in6_var.h>
1.108     itojun    146: #include <netinet6/nd6.h>
1.49      itojun    147: #endif
1.117     thorpej   148:
1.288     ozaki-r   149: #include "ether.h"
                    150: #include "fddi.h"
                    151: #include "token.h"
                    152:
1.166     liamjfoy  153: #include "carp.h"
                    154: #if NCARP > 0
                    155: #include <netinet/ip_carp.h>
                    156: #endif
                    157:
1.186     christos  158: #include <compat/sys/sockio.h>
1.161     christos  159: #include <compat/sys/socket.h>
                    160:
1.117     thorpej   161: MALLOC_DEFINE(M_IFADDR, "ifaddr", "interface address");
                    162: MALLOC_DEFINE(M_IFMADDR, "ether_multi", "link-level multicast address");
1.49      itojun    163:
1.274     rmind     164: /*
                    165:  * Global list of interfaces.
                    166:  */
1.334     ozaki-r   167: /* DEPRECATED. Remove it once kvm(3) users disappeared */
1.274     rmind     168: struct ifnet_head              ifnet_list;
1.334     ozaki-r   169:
                    170: struct pslist_head             ifnet_pslist;
1.274     rmind     171: static ifnet_t **              ifindex2ifnet = NULL;
                    172: static u_int                   if_index = 1;
                    173: static size_t                  if_indexlim = 0;
                    174: static uint64_t                        index_gen;
1.334     ozaki-r   175: /* Mutex to protect the above objects. */
                    176: kmutex_t                       ifnet_mtx __cacheline_aligned;
1.385     ozaki-r   177: static struct psref_class      *ifnet_psref_class __read_mostly;
1.334     ozaki-r   178: static pserialize_t            ifnet_psz;
                    179:
1.292     christos  180: static kmutex_t                        if_clone_mtx;
1.274     rmind     181:
                    182: struct ifnet *lo0ifp;
1.1       cgd       183: int    ifqmaxlen = IFQ_MAXLEN;
1.104     matt      184:
1.357     ozaki-r   185: struct psref_class             *ifa_psref_class __read_mostly;
                    186:
1.362     ozaki-r   187: static int     if_delroute_matcher(struct rtentry *, void *);
1.53      thorpej   188:
1.379     knakahar  189: static bool if_is_unit(const char *);
1.163     thorpej   190: static struct if_clone *if_clone_lookup(const char *, int *);
1.63      thorpej   191:
1.163     thorpej   192: static LIST_HEAD(, if_clone) if_cloners = LIST_HEAD_INITIALIZER(if_cloners);
                    193: static int if_cloners_count;
1.63      thorpej   194:
1.265     rmind     195: /* Packet filtering hook for interfaces. */
1.369     christos  196: pfil_head_t *                  if_pfil __read_mostly;
1.143     itojun    197:
1.239     elad      198: static kauth_listener_t if_listener;
                    199:
1.273     pooka     200: static int doifioctl(struct socket *, u_long, void *, struct lwp *);
1.163     thorpej   201: static void if_detach_queues(struct ifnet *, struct ifqueue *);
1.234     dyoung    202: static void sysctl_sndq_setup(struct sysctllog **, const char *,
                    203:     struct ifaltq *);
1.294     ozaki-r   204: static void if_slowtimo(void *);
1.300     ozaki-r   205: static void if_free_sadl(struct ifnet *);
1.302     ozaki-r   206: static void if_attachdomain1(struct ifnet *);
                    207: static int ifconf(u_long, void *);
1.345     knakahar  208: static int if_transmit(struct ifnet *, struct mbuf *);
1.302     ozaki-r   209: static int if_clone_create(const char *);
                    210: static int if_clone_destroy(const char *);
1.324     ozaki-r   211: static void if_link_state_change_si(void *);
1.403     ozaki-r   212: static void if_up_locked(struct ifnet *);
                    213: static void _if_down(struct ifnet *);
                    214: static void if_down_deactivated(struct ifnet *);
1.95      itojun    215:
1.323     ozaki-r   216: struct if_percpuq {
                    217:        struct ifnet    *ipq_ifp;
                    218:        void            *ipq_si;
                    219:        struct percpu   *ipq_ifqs;      /* struct ifqueue */
                    220: };
                    221:
                    222: static struct mbuf *if_percpuq_dequeue(struct if_percpuq *);
                    223:
1.327     knakahar  224: static void if_percpuq_drops(void *, void *, struct cpu_info *);
                    225: static int sysctl_percpuq_drops_handler(SYSCTLFN_PROTO);
                    226: static void sysctl_percpuq_setup(struct sysctllog **, const char *,
                    227:     struct if_percpuq *);
                    228:
1.364     ozaki-r   229: struct if_deferred_start {
                    230:        struct ifnet    *ids_ifp;
                    231:        void            (*ids_if_start)(struct ifnet *);
                    232:        void            *ids_si;
                    233: };
                    234:
                    235: static void if_deferred_start_softint(void *);
                    236: static void if_deferred_start_common(struct ifnet *);
                    237: static void if_deferred_start_destroy(struct ifnet *);
                    238:
1.240     cegger    239: #if defined(INET) || defined(INET6)
1.276     rmind     240: static void sysctl_net_pktq_setup(struct sysctllog **, int);
1.240     cegger    241: #endif
1.237     pooka     242:
1.370     ozaki-r   243: static void if_sysctl_setup(struct sysctllog **);
                    244:
1.361     pgoyette  245: /*
                    246:  * Pointer to stub or real compat_cvtcmd() depending on presence of
                    247:  * the compat module
                    248:  */
                    249: u_long stub_compat_cvtcmd(u_long);
                    250: u_long (*vec_compat_cvtcmd)(u_long) = stub_compat_cvtcmd;
                    251:
                    252: /* Similarly, pointer to compat_ifioctl() if it is present */
                    253:
                    254: int (*vec_compat_ifioctl)(struct socket *, u_long, u_long, void *,
                    255:        struct lwp *) = NULL;
                    256:
                    257: /* The stub version of compat_cvtcmd() */
                    258: u_long stub_compat_cvtcmd(u_long cmd)
                    259: {
                    260:
                    261:        return cmd;
                    262: }
                    263:
1.239     elad      264: static int
                    265: if_listener_cb(kauth_cred_t cred, kauth_action_t action, void *cookie,
                    266:     void *arg0, void *arg1, void *arg2, void *arg3)
                    267: {
                    268:        int result;
                    269:        enum kauth_network_req req;
                    270:
                    271:        result = KAUTH_RESULT_DEFER;
                    272:        req = (enum kauth_network_req)arg1;
                    273:
                    274:        if (action != KAUTH_NETWORK_INTERFACE)
                    275:                return result;
                    276:
                    277:        if ((req == KAUTH_REQ_NETWORK_INTERFACE_GET) ||
                    278:            (req == KAUTH_REQ_NETWORK_INTERFACE_SET))
                    279:                result = KAUTH_RESULT_ALLOW;
                    280:
                    281:        return result;
                    282: }
                    283:
1.1       cgd       284: /*
                    285:  * Network interface utility routines.
                    286:  *
                    287:  * Routines with ifa_ifwith* names take sockaddr *'s as
                    288:  * parameters.
                    289:  */
1.4       andrew    290: void
1.163     thorpej   291: ifinit(void)
1.1       cgd       292: {
1.370     ozaki-r   293:
                    294:        if_sysctl_setup(NULL);
1.1       cgd       295:
1.392     ozaki-r   296: #if (defined(INET) || defined(INET6))
1.352     knakahar  297:        encapinit();
                    298: #endif
                    299:
1.239     elad      300:        if_listener = kauth_listen_scope(KAUTH_SCOPE_NETWORK,
                    301:            if_listener_cb, NULL);
1.273     pooka     302:
                    303:        /* interfaces are available, inform socket code */
                    304:        ifioctl = doifioctl;
1.227     yamt      305: }
                    306:
                    307: /*
                    308:  * XXX Initialization before configure().
                    309:  * XXX hack to get pfil_add_hook working in autoconf.
                    310:  */
                    311: void
                    312: ifinit1(void)
                    313: {
1.292     christos  314:        mutex_init(&if_clone_mtx, MUTEX_DEFAULT, IPL_NONE);
1.357     ozaki-r   315:
1.274     rmind     316:        TAILQ_INIT(&ifnet_list);
1.334     ozaki-r   317:        mutex_init(&ifnet_mtx, MUTEX_DEFAULT, IPL_NONE);
                    318:        ifnet_psz = pserialize_create();
                    319:        ifnet_psref_class = psref_class_create("ifnet", IPL_SOFTNET);
1.357     ozaki-r   320:        ifa_psref_class = psref_class_create("ifa", IPL_SOFTNET);
1.334     ozaki-r   321:        PSLIST_INIT(&ifnet_pslist);
1.357     ozaki-r   322:
1.274     rmind     323:        if_indexlim = 8;
                    324:
1.265     rmind     325:        if_pfil = pfil_head_create(PFIL_TYPE_IFNET, NULL);
                    326:        KASSERT(if_pfil != NULL);
1.286     ozaki-r   327:
1.288     ozaki-r   328: #if NETHER > 0 || NFDDI > 0 || defined(NETATALK) || NTOKEN > 0 || defined(WLAN)
1.286     ozaki-r   329:        etherinit();
1.288     ozaki-r   330: #endif
1.1       cgd       331: }
                    332:
1.274     rmind     333: ifnet_t *
1.226     christos  334: if_alloc(u_char type)
                    335: {
1.274     rmind     336:        return kmem_zalloc(sizeof(ifnet_t), KM_SLEEP);
1.226     christos  337: }
                    338:
                    339: void
1.274     rmind     340: if_free(ifnet_t *ifp)
1.251     dyoung    341: {
1.274     rmind     342:        kmem_free(ifp, sizeof(ifnet_t));
1.251     dyoung    343: }
                    344:
                    345: void
1.226     christos  346: if_initname(struct ifnet *ifp, const char *name, int unit)
                    347: {
                    348:        (void)snprintf(ifp->if_xname, sizeof(ifp->if_xname),
                    349:            "%s%d", name, unit);
                    350: }
                    351:
1.53      thorpej   352: /*
                    353:  * Null routines used while an interface is going away.  These routines
                    354:  * just return an error.
                    355:  */
                    356:
                    357: int
1.177     christos  358: if_nulloutput(struct ifnet *ifp, struct mbuf *m,
1.331     ozaki-r   359:     const struct sockaddr *so, const struct rtentry *rt)
1.53      thorpej   360: {
                    361:
1.185     dyoung    362:        return ENXIO;
1.53      thorpej   363: }
                    364:
                    365: void
1.177     christos  366: if_nullinput(struct ifnet *ifp, struct mbuf *m)
1.53      thorpej   367: {
                    368:
                    369:        /* Nothing. */
                    370: }
                    371:
                    372: void
1.177     christos  373: if_nullstart(struct ifnet *ifp)
1.53      thorpej   374: {
                    375:
                    376:        /* Nothing. */
                    377: }
                    378:
                    379: int
1.332     knakahar  380: if_nulltransmit(struct ifnet *ifp, struct mbuf *m)
                    381: {
                    382:
1.383     knakahar  383:        m_freem(m);
1.332     knakahar  384:        return ENXIO;
                    385: }
                    386:
                    387: int
1.183     christos  388: if_nullioctl(struct ifnet *ifp, u_long cmd, void *data)
1.53      thorpej   389: {
                    390:
1.185     dyoung    391:        return ENXIO;
1.53      thorpej   392: }
                    393:
                    394: int
1.177     christos  395: if_nullinit(struct ifnet *ifp)
1.53      thorpej   396: {
                    397:
1.185     dyoung    398:        return ENXIO;
1.53      thorpej   399: }
                    400:
                    401: void
1.177     christos  402: if_nullstop(struct ifnet *ifp, int disable)
1.75      thorpej   403: {
                    404:
                    405:        /* Nothing. */
                    406: }
                    407:
                    408: void
1.295     ozaki-r   409: if_nullslowtimo(struct ifnet *ifp)
1.53      thorpej   410: {
                    411:
                    412:        /* Nothing. */
                    413: }
                    414:
                    415: void
1.177     christos  416: if_nulldrain(struct ifnet *ifp)
1.53      thorpej   417: {
                    418:
                    419:        /* Nothing. */
                    420: }
                    421:
1.210     dyoung    422: void
1.231     dyoung    423: if_set_sadl(struct ifnet *ifp, const void *lla, u_char addrlen, bool factory)
1.210     dyoung    424: {
                    425:        struct ifaddr *ifa;
                    426:        struct sockaddr_dl *sdl;
                    427:
                    428:        ifp->if_addrlen = addrlen;
                    429:        if_alloc_sadl(ifp);
                    430:        ifa = ifp->if_dl;
                    431:        sdl = satosdl(ifa->ifa_addr);
                    432:
                    433:        (void)sockaddr_dl_setaddr(sdl, sdl->sdl_len, lla, ifp->if_addrlen);
1.231     dyoung    434:        if (factory) {
                    435:                ifp->if_hwdl = ifp->if_dl;
1.291     rmind     436:                ifaref(ifp->if_hwdl);
1.231     dyoung    437:        }
1.223     dyoung    438:        /* TBD routing socket */
1.210     dyoung    439: }
                    440:
1.211     dyoung    441: struct ifaddr *
                    442: if_dl_create(const struct ifnet *ifp, const struct sockaddr_dl **sdlp)
                    443: {
                    444:        unsigned socksize, ifasize;
                    445:        int addrlen, namelen;
                    446:        struct sockaddr_dl *mask, *sdl;
                    447:        struct ifaddr *ifa;
                    448:
                    449:        namelen = strlen(ifp->if_xname);
                    450:        addrlen = ifp->if_addrlen;
                    451:        socksize = roundup(sockaddr_dl_measure(namelen, addrlen), sizeof(long));
                    452:        ifasize = sizeof(*ifa) + 2 * socksize;
1.398     christos  453:        ifa = malloc(ifasize, M_IFADDR, M_WAITOK|M_ZERO);
1.211     dyoung    454:
                    455:        sdl = (struct sockaddr_dl *)(ifa + 1);
                    456:        mask = (struct sockaddr_dl *)(socksize + (char *)sdl);
                    457:
                    458:        sockaddr_dl_init(sdl, socksize, ifp->if_index, ifp->if_type,
                    459:            ifp->if_xname, namelen, NULL, addrlen);
1.398     christos  460:        mask->sdl_family = AF_LINK;
1.211     dyoung    461:        mask->sdl_len = sockaddr_dl_measure(namelen, 0);
                    462:        memset(&mask->sdl_data[0], 0xff, namelen);
                    463:        ifa->ifa_rtrequest = link_rtrequest;
                    464:        ifa->ifa_addr = (struct sockaddr *)sdl;
                    465:        ifa->ifa_netmask = (struct sockaddr *)mask;
1.357     ozaki-r   466:        ifa_psref_init(ifa);
1.211     dyoung    467:
                    468:        *sdlp = sdl;
                    469:
                    470:        return ifa;
                    471: }
                    472:
1.223     dyoung    473: static void
                    474: if_sadl_setrefs(struct ifnet *ifp, struct ifaddr *ifa)
                    475: {
                    476:        const struct sockaddr_dl *sdl;
1.316     ozaki-r   477:
1.223     dyoung    478:        ifp->if_dl = ifa;
1.291     rmind     479:        ifaref(ifa);
1.223     dyoung    480:        sdl = satosdl(ifa->ifa_addr);
                    481:        ifp->if_sadl = sdl;
                    482: }
                    483:
1.1       cgd       484: /*
1.81      thorpej   485:  * Allocate the link level name for the specified interface.  This
                    486:  * is an attachment helper.  It must be called after ifp->if_addrlen
                    487:  * is initialized, which may not be the case when if_attach() is
                    488:  * called.
                    489:  */
                    490: void
                    491: if_alloc_sadl(struct ifnet *ifp)
                    492: {
                    493:        struct ifaddr *ifa;
1.211     dyoung    494:        const struct sockaddr_dl *sdl;
1.84      thorpej   495:
                    496:        /*
                    497:         * If the interface already has a link name, release it
                    498:         * now.  This is useful for interfaces that can change
                    499:         * link types, and thus switch link names often.
                    500:         */
                    501:        if (ifp->if_sadl != NULL)
                    502:                if_free_sadl(ifp);
1.81      thorpej   503:
1.211     dyoung    504:        ifa = if_dl_create(ifp, &sdl);
1.195     dyoung    505:
1.207     dyoung    506:        ifa_insert(ifp, ifa);
1.223     dyoung    507:        if_sadl_setrefs(ifp, ifa);
                    508: }
                    509:
                    510: static void
                    511: if_deactivate_sadl(struct ifnet *ifp)
                    512: {
                    513:        struct ifaddr *ifa;
                    514:
                    515:        KASSERT(ifp->if_dl != NULL);
                    516:
                    517:        ifa = ifp->if_dl;
                    518:
                    519:        ifp->if_sadl = NULL;
                    520:
                    521:        ifp->if_dl = NULL;
1.291     rmind     522:        ifafree(ifa);
1.223     dyoung    523: }
                    524:
1.378     ozaki-r   525: static void
                    526: if_replace_sadl(struct ifnet *ifp, struct ifaddr *ifa)
                    527: {
                    528:        struct ifaddr *old;
                    529:
                    530:        KASSERT(ifp->if_dl != NULL);
                    531:
                    532:        old = ifp->if_dl;
                    533:
                    534:        ifaref(ifa);
                    535:        /* XXX Update if_dl and if_sadl atomically */
                    536:        ifp->if_dl = ifa;
                    537:        ifp->if_sadl = satosdl(ifa->ifa_addr);
                    538:
                    539:        ifafree(old);
                    540: }
                    541:
1.224     dyoung    542: void
1.357     ozaki-r   543: if_activate_sadl(struct ifnet *ifp, struct ifaddr *ifa0,
1.223     dyoung    544:     const struct sockaddr_dl *sdl)
                    545: {
1.357     ozaki-r   546:        int s, ss;
                    547:        struct ifaddr *ifa;
                    548:        int bound = curlwp_bind();
1.223     dyoung    549:
1.378     ozaki-r   550:        KASSERT(ifa_held(ifa0));
                    551:
1.373     ozaki-r   552:        s = splsoftnet();
1.223     dyoung    553:
1.378     ozaki-r   554:        if_replace_sadl(ifp, ifa0);
1.357     ozaki-r   555:
                    556:        ss = pserialize_read_enter();
                    557:        IFADDR_READER_FOREACH(ifa, ifp) {
                    558:                struct psref psref;
                    559:                ifa_acquire(ifa, &psref);
                    560:                pserialize_read_exit(ss);
                    561:
1.231     dyoung    562:                rtinit(ifa, RTM_LLINFO_UPD, 0);
1.354     ozaki-r   563:
1.357     ozaki-r   564:                ss = pserialize_read_enter();
                    565:                ifa_release(ifa, &psref);
                    566:        }
                    567:        pserialize_read_exit(ss);
                    568:
1.223     dyoung    569:        splx(s);
1.357     ozaki-r   570:        curlwp_bindx(bound);
1.81      thorpej   571: }
                    572:
                    573: /*
                    574:  * Free the link level name for the specified interface.  This is
1.300     ozaki-r   575:  * a detach helper.  This is called from if_detach().
1.81      thorpej   576:  */
1.300     ozaki-r   577: static void
1.81      thorpej   578: if_free_sadl(struct ifnet *ifp)
                    579: {
                    580:        struct ifaddr *ifa;
                    581:        int s;
                    582:
1.316     ozaki-r   583:        ifa = ifp->if_dl;
1.81      thorpej   584:        if (ifa == NULL) {
                    585:                KASSERT(ifp->if_sadl == NULL);
                    586:                return;
                    587:        }
                    588:
                    589:        KASSERT(ifp->if_sadl != NULL);
                    590:
1.373     ozaki-r   591:        s = splsoftnet();
1.81      thorpej   592:        rtinit(ifa, RTM_DELETE, 0);
1.207     dyoung    593:        ifa_remove(ifp, ifa);
1.223     dyoung    594:        if_deactivate_sadl(ifp);
1.231     dyoung    595:        if (ifp->if_hwdl == ifa) {
1.291     rmind     596:                ifafree(ifa);
1.231     dyoung    597:                ifp->if_hwdl = NULL;
                    598:        }
1.81      thorpej   599:        splx(s);
                    600: }
                    601:
1.274     rmind     602: static void
                    603: if_getindex(ifnet_t *ifp)
1.1       cgd       604: {
1.274     rmind     605:        bool hitlimit = false;
1.231     dyoung    606:
1.234     dyoung    607:        ifp->if_index_gen = index_gen++;
                    608:
1.102     atatat    609:        ifp->if_index = if_index;
1.274     rmind     610:        if (ifindex2ifnet == NULL) {
1.102     atatat    611:                if_index++;
1.274     rmind     612:                goto skip;
                    613:        }
                    614:        while (if_byindex(ifp->if_index)) {
                    615:                /*
                    616:                 * If we hit USHRT_MAX, we skip back to 0 since
                    617:                 * there are a number of places where the value
                    618:                 * of if_index or if_index itself is compared
                    619:                 * to or stored in an unsigned short.  By
                    620:                 * jumping back, we won't botch those assignments
                    621:                 * or comparisons.
                    622:                 */
                    623:                if (++if_index == 0) {
                    624:                        if_index = 1;
                    625:                } else if (if_index == USHRT_MAX) {
1.102     atatat    626:                        /*
1.274     rmind     627:                         * However, if we have to jump back to
                    628:                         * zero *twice* without finding an empty
                    629:                         * slot in ifindex2ifnet[], then there
                    630:                         * there are too many (>65535) interfaces.
1.102     atatat    631:                         */
1.274     rmind     632:                        if (hitlimit) {
                    633:                                panic("too many interfaces");
1.102     atatat    634:                        }
1.274     rmind     635:                        hitlimit = true;
                    636:                        if_index = 1;
1.102     atatat    637:                }
1.274     rmind     638:                ifp->if_index = if_index;
                    639:        }
                    640: skip:
1.49      itojun    641:        /*
1.316     ozaki-r   642:         * ifindex2ifnet is indexed by if_index. Since if_index will
                    643:         * grow dynamically, it should grow too.
1.49      itojun    644:         */
1.316     ozaki-r   645:        if (ifindex2ifnet == NULL || ifp->if_index >= if_indexlim) {
1.131     itojun    646:                size_t m, n, oldlim;
1.183     christos  647:                void *q;
1.154     perry     648:
1.131     itojun    649:                oldlim = if_indexlim;
1.53      thorpej   650:                while (ifp->if_index >= if_indexlim)
1.49      itojun    651:                        if_indexlim <<= 1;
                    652:
                    653:                /* grow ifindex2ifnet */
1.131     itojun    654:                m = oldlim * sizeof(struct ifnet *);
1.49      itojun    655:                n = if_indexlim * sizeof(struct ifnet *);
1.230     dyoung    656:                q = malloc(n, M_IFADDR, M_WAITOK|M_ZERO);
1.185     dyoung    657:                if (ifindex2ifnet != NULL) {
1.230     dyoung    658:                        memcpy(q, ifindex2ifnet, m);
                    659:                        free(ifindex2ifnet, M_IFADDR);
1.49      itojun    660:                }
                    661:                ifindex2ifnet = (struct ifnet **)q;
1.1       cgd       662:        }
1.274     rmind     663:        ifindex2ifnet[ifp->if_index] = ifp;
                    664: }
                    665:
                    666: /*
1.307     ozaki-r   667:  * Initialize an interface and assign an index for it.
                    668:  *
                    669:  * It must be called prior to a device specific attach routine
                    670:  * (e.g., ether_ifattach and ieee80211_ifattach) or if_alloc_sadl,
                    671:  * and be followed by if_register:
                    672:  *
                    673:  *     if_initialize(ifp);
                    674:  *     ether_ifattach(ifp, enaddr);
                    675:  *     if_register(ifp);
1.274     rmind     676:  */
1.396     msaitoh   677: int
1.307     ozaki-r   678: if_initialize(ifnet_t *ifp)
1.274     rmind     679: {
1.396     msaitoh   680:        int rv = 0;
                    681:
1.274     rmind     682:        KASSERT(if_indexlim > 0);
                    683:        TAILQ_INIT(&ifp->if_addrlist);
1.49      itojun    684:
1.1       cgd       685:        /*
1.81      thorpej   686:         * Link level name is allocated later by a separate call to
                    687:         * if_alloc_sadl().
1.1       cgd       688:         */
1.81      thorpej   689:
1.40      thorpej   690:        if (ifp->if_snd.ifq_maxlen == 0)
1.94      itojun    691:                ifp->if_snd.ifq_maxlen = ifqmaxlen;
1.234     dyoung    692:
1.42      is        693:        ifp->if_broadcastaddr = 0; /* reliably crash if used uninitialized */
1.57      thorpej   694:
                    695:        ifp->if_link_state = LINK_STATE_UNKNOWN;
1.325     roy       696:        ifp->if_link_queue = -1; /* all bits set, see link_state_change() */
1.57      thorpej   697:
1.89      thorpej   698:        ifp->if_capenable = 0;
1.97      thorpej   699:        ifp->if_csum_flags_tx = 0;
                    700:        ifp->if_csum_flags_rx = 0;
1.89      thorpej   701:
1.86      thorpej   702: #ifdef ALTQ
                    703:        ifp->if_snd.altq_type = 0;
                    704:        ifp->if_snd.altq_disc = NULL;
                    705:        ifp->if_snd.altq_flags &= ALTQF_CANTCHANGE;
                    706:        ifp->if_snd.altq_tbr  = NULL;
                    707:        ifp->if_snd.altq_ifp  = ifp;
                    708: #endif
                    709:
1.358     ozaki-r   710:        IFQ_LOCK_INIT(&ifp->if_snd);
1.285     ozaki-r   711:
1.265     rmind     712:        ifp->if_pfil = pfil_head_create(PFIL_TYPE_IFNET, ifp);
1.369     christos  713:        pfil_run_ifhooks(if_pfil, PFIL_IFNET_ATTACH, ifp);
1.87      thorpej   714:
1.318     ozaki-r   715:        IF_AFDATA_LOCK_INIT(ifp);
                    716:
1.347     knakahar  717:        if (if_is_link_state_changeable(ifp)) {
1.406     ozaki-r   718:                u_int flags = SOFTINT_NET;
                    719:                flags |= ISSET(ifp->if_extflags, IFEF_MPSAFE) ?
                    720:                    SOFTINT_MPSAFE : 0;
                    721:                ifp->if_link_si = softint_establish(flags,
1.346     knakahar  722:                    if_link_state_change_si, ifp);
1.396     msaitoh   723:                if (ifp->if_link_si == NULL) {
                    724:                        rv = ENOMEM;
                    725:                        goto fail;
                    726:                }
1.346     knakahar  727:        }
1.324     ozaki-r   728:
1.334     ozaki-r   729:        PSLIST_ENTRY_INIT(ifp, if_pslist_entry);
1.354     ozaki-r   730:        PSLIST_INIT(&ifp->if_addr_pslist);
1.334     ozaki-r   731:        psref_target_init(&ifp->if_psref, ifnet_psref_class);
1.336     ozaki-r   732:        ifp->if_ioctl_lock = mutex_obj_alloc(MUTEX_DEFAULT, IPL_NONE);
1.381     ozaki-r   733:        LIST_INIT(&ifp->if_multiaddrs);
1.334     ozaki-r   734:
                    735:        IFNET_LOCK();
1.307     ozaki-r   736:        if_getindex(ifp);
1.334     ozaki-r   737:        IFNET_UNLOCK();
1.396     msaitoh   738:
                    739:        return 0;
                    740:
                    741: fail:
                    742:        IF_AFDATA_LOCK_DESTROY(ifp);
                    743:
                    744:        pfil_run_ifhooks(if_pfil, PFIL_IFNET_DETACH, ifp);
                    745:        (void)pfil_head_destroy(ifp->if_pfil);
                    746:
                    747:        IFQ_LOCK_DESTROY(&ifp->if_snd);
                    748:
                    749:        return rv;
1.307     ozaki-r   750: }
                    751:
                    752: /*
                    753:  * Register an interface to the list of "active" interfaces.
                    754:  */
                    755: void
                    756: if_register(ifnet_t *ifp)
                    757: {
1.336     ozaki-r   758:        /*
                    759:         * If the driver has not supplied its own if_ioctl, then
                    760:         * supply the default.
                    761:         */
                    762:        if (ifp->if_ioctl == NULL)
                    763:                ifp->if_ioctl = ifioctl_common;
1.307     ozaki-r   764:
                    765:        sysctl_sndq_setup(&ifp->if_sysctl_log, ifp->if_xname, &ifp->if_snd);
                    766:
1.152     matt      767:        if (!STAILQ_EMPTY(&domains))
1.147     tron      768:                if_attachdomain1(ifp);
                    769:
1.107     itojun    770:        /* Announce the interface. */
                    771:        rt_ifannouncemsg(ifp, IFAN_ARRIVAL);
1.296     ozaki-r   772:
                    773:        if (ifp->if_slowtimo != NULL) {
1.405     ozaki-r   774:                int flags = ISSET(ifp->if_extflags, IFEF_MPSAFE) ?
                    775:                    CALLOUT_MPSAFE : 0;
1.297     ozaki-r   776:                ifp->if_slowtimo_ch =
                    777:                    kmem_zalloc(sizeof(*ifp->if_slowtimo_ch), KM_SLEEP);
1.405     ozaki-r   778:                callout_init(ifp->if_slowtimo_ch, flags);
1.297     ozaki-r   779:                callout_setfunc(ifp->if_slowtimo_ch, if_slowtimo, ifp);
1.296     ozaki-r   780:                if_slowtimo(ifp);
                    781:        }
1.307     ozaki-r   782:
1.332     knakahar  783:        if (ifp->if_transmit == NULL || ifp->if_transmit == if_nulltransmit)
                    784:                ifp->if_transmit = if_transmit;
                    785:
1.334     ozaki-r   786:        IFNET_LOCK();
1.307     ozaki-r   787:        TAILQ_INSERT_TAIL(&ifnet_list, ifp, if_list);
1.334     ozaki-r   788:        IFNET_WRITER_INSERT_TAIL(ifp);
                    789:        IFNET_UNLOCK();
1.307     ozaki-r   790: }
                    791:
                    792: /*
1.323     ozaki-r   793:  * The if_percpuq framework
                    794:  *
                    795:  * It allows network device drivers to execute the network stack
                    796:  * in softint (so called softint-based if_input). It utilizes
                    797:  * softint and percpu ifqueue. It doesn't distribute any packets
                    798:  * between CPUs, unlike pktqueue(9).
                    799:  *
                    800:  * Currently we support two options for device drivers to apply the framework:
                    801:  * - Use it implicitly with less changes
                    802:  *   - If you use if_attach in driver's _attach function and if_input in
                    803:  *     driver's Rx interrupt handler, a packet is queued and a softint handles
                    804:  *     the packet implicitly
                    805:  * - Use it explicitly in each driver (recommended)
                    806:  *   - You can use if_percpuq_* directly in your driver
                    807:  *   - In this case, you need to allocate struct if_percpuq in driver's softc
                    808:  *   - See wm(4) as a reference implementation
                    809:  */
                    810:
                    811: static void
                    812: if_percpuq_softint(void *arg)
                    813: {
                    814:        struct if_percpuq *ipq = arg;
                    815:        struct ifnet *ifp = ipq->ipq_ifp;
                    816:        struct mbuf *m;
                    817:
1.368     ozaki-r   818:        while ((m = if_percpuq_dequeue(ipq)) != NULL) {
                    819:                ifp->if_ipackets++;
                    820:                bpf_mtap(ifp, m);
                    821:
1.323     ozaki-r   822:                ifp->_if_input(ifp, m);
1.368     ozaki-r   823:        }
1.323     ozaki-r   824: }
                    825:
                    826: static void
                    827: if_percpuq_init_ifq(void *p, void *arg __unused, struct cpu_info *ci __unused)
                    828: {
                    829:        struct ifqueue *const ifq = p;
                    830:
                    831:        memset(ifq, 0, sizeof(*ifq));
                    832:        ifq->ifq_maxlen = IFQ_MAXLEN;
                    833: }
                    834:
                    835: struct if_percpuq *
                    836: if_percpuq_create(struct ifnet *ifp)
                    837: {
                    838:        struct if_percpuq *ipq;
                    839:
                    840:        ipq = kmem_zalloc(sizeof(*ipq), KM_SLEEP);
                    841:        ipq->ipq_ifp = ifp;
                    842:        ipq->ipq_si = softint_establish(SOFTINT_NET|SOFTINT_MPSAFE,
                    843:            if_percpuq_softint, ipq);
                    844:        ipq->ipq_ifqs = percpu_alloc(sizeof(struct ifqueue));
                    845:        percpu_foreach(ipq->ipq_ifqs, &if_percpuq_init_ifq, NULL);
                    846:
1.327     knakahar  847:        sysctl_percpuq_setup(&ifp->if_sysctl_log, ifp->if_xname, ipq);
                    848:
1.323     ozaki-r   849:        return ipq;
                    850: }
                    851:
                    852: static struct mbuf *
                    853: if_percpuq_dequeue(struct if_percpuq *ipq)
                    854: {
                    855:        struct mbuf *m;
                    856:        struct ifqueue *ifq;
                    857:        int s;
                    858:
                    859:        s = splnet();
                    860:        ifq = percpu_getref(ipq->ipq_ifqs);
                    861:        IF_DEQUEUE(ifq, m);
                    862:        percpu_putref(ipq->ipq_ifqs);
                    863:        splx(s);
                    864:
                    865:        return m;
                    866: }
                    867:
                    868: static void
                    869: if_percpuq_purge_ifq(void *p, void *arg __unused, struct cpu_info *ci __unused)
                    870: {
                    871:        struct ifqueue *const ifq = p;
                    872:
                    873:        IF_PURGE(ifq);
                    874: }
                    875:
                    876: void
                    877: if_percpuq_destroy(struct if_percpuq *ipq)
                    878: {
                    879:
                    880:        /* if_detach may already destroy it */
                    881:        if (ipq == NULL)
                    882:                return;
                    883:
                    884:        softint_disestablish(ipq->ipq_si);
                    885:        percpu_foreach(ipq->ipq_ifqs, &if_percpuq_purge_ifq, NULL);
                    886:        percpu_free(ipq->ipq_ifqs, sizeof(struct ifqueue));
1.363     ozaki-r   887:        kmem_free(ipq, sizeof(*ipq));
1.323     ozaki-r   888: }
                    889:
                    890: void
                    891: if_percpuq_enqueue(struct if_percpuq *ipq, struct mbuf *m)
                    892: {
                    893:        struct ifqueue *ifq;
                    894:        int s;
                    895:
                    896:        KASSERT(ipq != NULL);
                    897:
                    898:        s = splnet();
                    899:        ifq = percpu_getref(ipq->ipq_ifqs);
                    900:        if (IF_QFULL(ifq)) {
                    901:                IF_DROP(ifq);
1.326     ozaki-r   902:                percpu_putref(ipq->ipq_ifqs);
1.323     ozaki-r   903:                m_freem(m);
                    904:                goto out;
                    905:        }
                    906:        IF_ENQUEUE(ifq, m);
                    907:        percpu_putref(ipq->ipq_ifqs);
                    908:
                    909:        softint_schedule(ipq->ipq_si);
                    910: out:
                    911:        splx(s);
                    912: }
                    913:
1.327     knakahar  914: static void
                    915: if_percpuq_drops(void *p, void *arg, struct cpu_info *ci __unused)
                    916: {
                    917:        struct ifqueue *const ifq = p;
                    918:        int *sum = arg;
                    919:
                    920:        *sum += ifq->ifq_drops;
                    921: }
                    922:
                    923: static int
                    924: sysctl_percpuq_drops_handler(SYSCTLFN_ARGS)
                    925: {
                    926:        struct sysctlnode node;
                    927:        struct if_percpuq *ipq;
                    928:        int sum = 0;
                    929:        int error;
                    930:
                    931:        node = *rnode;
                    932:        ipq = node.sysctl_data;
                    933:
                    934:        percpu_foreach(ipq->ipq_ifqs, if_percpuq_drops, &sum);
                    935:
                    936:        node.sysctl_data = &sum;
                    937:        error = sysctl_lookup(SYSCTLFN_CALL(&node));
                    938:        if (error != 0 || newp == NULL)
                    939:                return error;
                    940:
                    941:        return 0;
                    942: }
                    943:
                    944: static void
                    945: sysctl_percpuq_setup(struct sysctllog **clog, const char* ifname,
                    946:     struct if_percpuq *ipq)
                    947: {
                    948:        const struct sysctlnode *cnode, *rnode;
                    949:
                    950:        if (sysctl_createv(clog, 0, NULL, &rnode,
                    951:                       CTLFLAG_PERMANENT,
                    952:                       CTLTYPE_NODE, "interfaces",
                    953:                       SYSCTL_DESCR("Per-interface controls"),
                    954:                       NULL, 0, NULL, 0,
                    955:                       CTL_NET, CTL_CREATE, CTL_EOL) != 0)
                    956:                goto bad;
                    957:
                    958:        if (sysctl_createv(clog, 0, &rnode, &rnode,
                    959:                       CTLFLAG_PERMANENT,
                    960:                       CTLTYPE_NODE, ifname,
                    961:                       SYSCTL_DESCR("Interface controls"),
                    962:                       NULL, 0, NULL, 0,
                    963:                       CTL_CREATE, CTL_EOL) != 0)
                    964:                goto bad;
                    965:
                    966:        if (sysctl_createv(clog, 0, &rnode, &rnode,
                    967:                       CTLFLAG_PERMANENT,
                    968:                       CTLTYPE_NODE, "rcvq",
                    969:                       SYSCTL_DESCR("Interface input queue controls"),
                    970:                       NULL, 0, NULL, 0,
                    971:                       CTL_CREATE, CTL_EOL) != 0)
                    972:                goto bad;
                    973:
                    974: #ifdef NOTYET
                    975:        /* XXX Should show each per-CPU queue length? */
                    976:        if (sysctl_createv(clog, 0, &rnode, &rnode,
                    977:                       CTLFLAG_PERMANENT,
                    978:                       CTLTYPE_INT, "len",
                    979:                       SYSCTL_DESCR("Current input queue length"),
                    980:                       sysctl_percpuq_len, 0, NULL, 0,
                    981:                       CTL_CREATE, CTL_EOL) != 0)
                    982:                goto bad;
                    983:
                    984:        if (sysctl_createv(clog, 0, &rnode, &cnode,
                    985:                       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
                    986:                       CTLTYPE_INT, "maxlen",
                    987:                       SYSCTL_DESCR("Maximum allowed input queue length"),
                    988:                       sysctl_percpuq_maxlen_handler, 0, (void *)ipq, 0,
                    989:                       CTL_CREATE, CTL_EOL) != 0)
                    990:                goto bad;
                    991: #endif
                    992:
                    993:        if (sysctl_createv(clog, 0, &rnode, &cnode,
                    994:                       CTLFLAG_PERMANENT,
                    995:                       CTLTYPE_INT, "drops",
                    996:                       SYSCTL_DESCR("Total packets dropped due to full input queue"),
                    997:                       sysctl_percpuq_drops_handler, 0, (void *)ipq, 0,
                    998:                       CTL_CREATE, CTL_EOL) != 0)
                    999:                goto bad;
                   1000:
                   1001:        return;
                   1002: bad:
                   1003:        printf("%s: could not attach sysctl nodes\n", ifname);
                   1004:        return;
                   1005: }
                   1006:
1.364     ozaki-r  1007: /*
                   1008:  * The deferred if_start framework
                   1009:  *
                   1010:  * The common APIs to defer if_start to softint when if_start is requested
                   1011:  * from a device driver running in hardware interrupt context.
                   1012:  */
                   1013: /*
                   1014:  * Call ifp->if_start (or equivalent) in a dedicated softint for
                   1015:  * deferred if_start.
                   1016:  */
                   1017: static void
                   1018: if_deferred_start_softint(void *arg)
                   1019: {
                   1020:        struct if_deferred_start *ids = arg;
                   1021:        struct ifnet *ifp = ids->ids_ifp;
                   1022:
                   1023:        ids->ids_if_start(ifp);
                   1024: }
                   1025:
                   1026: /*
                   1027:  * The default callback function for deferred if_start.
                   1028:  */
                   1029: static void
                   1030: if_deferred_start_common(struct ifnet *ifp)
                   1031: {
1.382     ozaki-r  1032:        int s;
1.364     ozaki-r  1033:
1.382     ozaki-r  1034:        s = splnet();
1.364     ozaki-r  1035:        if_start_lock(ifp);
1.382     ozaki-r  1036:        splx(s);
1.364     ozaki-r  1037: }
                   1038:
                   1039: static inline bool
                   1040: if_snd_is_used(struct ifnet *ifp)
                   1041: {
                   1042:
                   1043:        return ifp->if_transmit == NULL || ifp->if_transmit == if_nulltransmit ||
                   1044:            ALTQ_IS_ENABLED(&ifp->if_snd);
                   1045: }
                   1046:
                   1047: /*
                   1048:  * Schedule deferred if_start.
                   1049:  */
                   1050: void
                   1051: if_schedule_deferred_start(struct ifnet *ifp)
                   1052: {
                   1053:
                   1054:        KASSERT(ifp->if_deferred_start != NULL);
                   1055:
                   1056:        if (if_snd_is_used(ifp) && IFQ_IS_EMPTY(&ifp->if_snd))
                   1057:                return;
                   1058:
                   1059:        softint_schedule(ifp->if_deferred_start->ids_si);
                   1060: }
                   1061:
                   1062: /*
                   1063:  * Create an instance of deferred if_start. A driver should call the function
                   1064:  * only if the driver needs deferred if_start. Drivers can setup their own
                   1065:  * deferred if_start function via 2nd argument.
                   1066:  */
                   1067: void
                   1068: if_deferred_start_init(struct ifnet *ifp, void (*func)(struct ifnet *))
                   1069: {
                   1070:        struct if_deferred_start *ids;
                   1071:
                   1072:        ids = kmem_zalloc(sizeof(*ids), KM_SLEEP);
                   1073:        ids->ids_ifp = ifp;
                   1074:        ids->ids_si = softint_establish(SOFTINT_NET|SOFTINT_MPSAFE,
                   1075:            if_deferred_start_softint, ids);
                   1076:        if (func != NULL)
                   1077:                ids->ids_if_start = func;
                   1078:        else
                   1079:                ids->ids_if_start = if_deferred_start_common;
                   1080:
                   1081:        ifp->if_deferred_start = ids;
                   1082: }
                   1083:
                   1084: static void
                   1085: if_deferred_start_destroy(struct ifnet *ifp)
                   1086: {
                   1087:
                   1088:        if (ifp->if_deferred_start == NULL)
                   1089:                return;
                   1090:
                   1091:        softint_disestablish(ifp->if_deferred_start->ids_si);
                   1092:        kmem_free(ifp->if_deferred_start, sizeof(*ifp->if_deferred_start));
                   1093:        ifp->if_deferred_start = NULL;
                   1094: }
1.327     knakahar 1095:
1.323     ozaki-r  1096: /*
                   1097:  * The common interface input routine that is called by device drivers,
                   1098:  * which should be used only when the driver's rx handler already runs
                   1099:  * in softint.
                   1100:  */
                   1101: void
                   1102: if_input(struct ifnet *ifp, struct mbuf *m)
                   1103: {
                   1104:
                   1105:        KASSERT(ifp->if_percpuq == NULL);
                   1106:        KASSERT(!cpu_intr_p());
                   1107:
1.368     ozaki-r  1108:        ifp->if_ipackets++;
                   1109:        bpf_mtap(ifp, m);
                   1110:
1.323     ozaki-r  1111:        ifp->_if_input(ifp, m);
                   1112: }
                   1113:
                   1114: /*
                   1115:  * DEPRECATED. Use if_initialize and if_register instead.
1.307     ozaki-r  1116:  * See the above comment of if_initialize.
1.323     ozaki-r  1117:  *
                   1118:  * Note that it implicitly enables if_percpuq to make drivers easy to
1.333     skrll    1119:  * migrate softint-based if_input without much changes. If you don't
1.323     ozaki-r  1120:  * want to enable it, use if_initialize instead.
1.307     ozaki-r  1121:  */
1.396     msaitoh  1122: int
1.307     ozaki-r  1123: if_attach(ifnet_t *ifp)
                   1124: {
1.396     msaitoh  1125:        int rv;
                   1126:
                   1127:        rv = if_initialize(ifp);
                   1128:        if (rv != 0)
                   1129:                return rv;
1.323     ozaki-r  1130:
                   1131:        ifp->if_percpuq = if_percpuq_create(ifp);
1.307     ozaki-r  1132:        if_register(ifp);
1.396     msaitoh  1133:
                   1134:        return 0;
1.107     itojun   1135: }
                   1136:
                   1137: void
1.163     thorpej  1138: if_attachdomain(void)
1.107     itojun   1139: {
                   1140:        struct ifnet *ifp;
1.110     itojun   1141:        int s;
1.339     ozaki-r  1142:        int bound = curlwp_bind();
1.107     itojun   1143:
1.334     ozaki-r  1144:        s = pserialize_read_enter();
                   1145:        IFNET_READER_FOREACH(ifp) {
                   1146:                struct psref psref;
                   1147:                psref_acquire(&psref, &ifp->if_psref, ifnet_psref_class);
                   1148:                pserialize_read_exit(s);
1.107     itojun   1149:                if_attachdomain1(ifp);
1.334     ozaki-r  1150:                s = pserialize_read_enter();
                   1151:                psref_release(&psref, &ifp->if_psref, ifnet_psref_class);
                   1152:        }
                   1153:        pserialize_read_exit(s);
1.339     ozaki-r  1154:        curlwp_bindx(bound);
1.107     itojun   1155: }
                   1156:
1.302     ozaki-r  1157: static void
1.163     thorpej  1158: if_attachdomain1(struct ifnet *ifp)
1.107     itojun   1159: {
                   1160:        struct domain *dp;
1.109     itojun   1161:        int s;
                   1162:
1.373     ozaki-r  1163:        s = splsoftnet();
1.107     itojun   1164:
1.106     itojun   1165:        /* address family dependent data region */
                   1166:        memset(ifp->if_afdata, 0, sizeof(ifp->if_afdata));
1.152     matt     1167:        DOMAIN_FOREACH(dp) {
1.185     dyoung   1168:                if (dp->dom_ifattach != NULL)
1.106     itojun   1169:                        ifp->if_afdata[dp->dom_family] =
                   1170:                            (*dp->dom_ifattach)(ifp);
                   1171:        }
1.109     itojun   1172:
                   1173:        splx(s);
1.1       cgd      1174: }
1.53      thorpej  1175:
                   1176: /*
                   1177:  * Deactivate an interface.  This points all of the procedure
                   1178:  * handles at error stubs.  May be called from interrupt context.
                   1179:  */
                   1180: void
1.163     thorpej  1181: if_deactivate(struct ifnet *ifp)
1.53      thorpej  1182: {
                   1183:        int s;
                   1184:
1.373     ozaki-r  1185:        s = splsoftnet();
1.53      thorpej  1186:
                   1187:        ifp->if_output   = if_nulloutput;
1.323     ozaki-r  1188:        ifp->_if_input   = if_nullinput;
1.53      thorpej  1189:        ifp->if_start    = if_nullstart;
1.332     knakahar 1190:        ifp->if_transmit = if_nulltransmit;
1.53      thorpej  1191:        ifp->if_ioctl    = if_nullioctl;
1.75      thorpej  1192:        ifp->if_init     = if_nullinit;
                   1193:        ifp->if_stop     = if_nullstop;
1.295     ozaki-r  1194:        ifp->if_slowtimo = if_nullslowtimo;
1.53      thorpej  1195:        ifp->if_drain    = if_nulldrain;
                   1196:
                   1197:        /* No more packets may be enqueued. */
                   1198:        ifp->if_snd.ifq_maxlen = 0;
                   1199:
                   1200:        splx(s);
                   1201: }
                   1202:
1.348     ozaki-r  1203: bool
1.367     ozaki-r  1204: if_is_deactivated(const struct ifnet *ifp)
1.348     ozaki-r  1205: {
                   1206:
                   1207:        return ifp->if_output == if_nulloutput;
                   1208: }
                   1209:
1.206     dyoung   1210: void
1.218     dyoung   1211: if_purgeaddrs(struct ifnet *ifp, int family, void (*purgeaddr)(struct ifaddr *))
1.206     dyoung   1212: {
1.289     ozaki-r  1213:        struct ifaddr *ifa, *nifa;
1.357     ozaki-r  1214:        int s;
1.206     dyoung   1215:
1.357     ozaki-r  1216:        s = pserialize_read_enter();
1.354     ozaki-r  1217:        for (ifa = IFADDR_READER_FIRST(ifp); ifa; ifa = nifa) {
                   1218:                nifa = IFADDR_READER_NEXT(ifa);
1.206     dyoung   1219:                if (ifa->ifa_addr->sa_family != family)
                   1220:                        continue;
1.357     ozaki-r  1221:                pserialize_read_exit(s);
                   1222:
1.206     dyoung   1223:                (*purgeaddr)(ifa);
1.357     ozaki-r  1224:
                   1225:                s = pserialize_read_enter();
1.206     dyoung   1226:        }
1.357     ozaki-r  1227:        pserialize_read_exit(s);
1.206     dyoung   1228: }
                   1229:
1.349     ozaki-r  1230: #ifdef IFAREF_DEBUG
                   1231: static struct ifaddr **ifa_list;
                   1232: static int ifa_list_size;
                   1233:
                   1234: /* Depends on only one if_attach runs at once */
                   1235: static void
                   1236: if_build_ifa_list(struct ifnet *ifp)
                   1237: {
                   1238:        struct ifaddr *ifa;
                   1239:        int i;
                   1240:
                   1241:        KASSERT(ifa_list == NULL);
                   1242:        KASSERT(ifa_list_size == 0);
                   1243:
1.354     ozaki-r  1244:        IFADDR_READER_FOREACH(ifa, ifp)
1.349     ozaki-r  1245:                ifa_list_size++;
                   1246:
                   1247:        ifa_list = kmem_alloc(sizeof(*ifa) * ifa_list_size, KM_SLEEP);
                   1248:        i = 0;
1.354     ozaki-r  1249:        IFADDR_READER_FOREACH(ifa, ifp) {
1.349     ozaki-r  1250:                ifa_list[i++] = ifa;
                   1251:                ifaref(ifa);
                   1252:        }
                   1253: }
                   1254:
                   1255: static void
                   1256: if_check_and_free_ifa_list(struct ifnet *ifp)
                   1257: {
                   1258:        int i;
                   1259:        struct ifaddr *ifa;
                   1260:
                   1261:        if (ifa_list == NULL)
                   1262:                return;
                   1263:
                   1264:        for (i = 0; i < ifa_list_size; i++) {
                   1265:                char buf[64];
                   1266:
                   1267:                ifa = ifa_list[i];
                   1268:                sockaddr_format(ifa->ifa_addr, buf, sizeof(buf));
                   1269:                if (ifa->ifa_refcnt > 1) {
                   1270:                        log(LOG_WARNING,
                   1271:                            "ifa(%s) still referenced (refcnt=%d)\n",
                   1272:                            buf, ifa->ifa_refcnt - 1);
                   1273:                } else
                   1274:                        log(LOG_DEBUG,
                   1275:                            "ifa(%s) not referenced (refcnt=%d)\n",
                   1276:                            buf, ifa->ifa_refcnt - 1);
                   1277:                ifafree(ifa);
                   1278:        }
                   1279:
                   1280:        kmem_free(ifa_list, sizeof(*ifa) * ifa_list_size);
                   1281:        ifa_list = NULL;
                   1282:        ifa_list_size = 0;
                   1283: }
                   1284: #endif
                   1285:
1.53      thorpej  1286: /*
                   1287:  * Detach an interface from the list of "active" interfaces,
                   1288:  * freeing any resources as we go along.
                   1289:  *
                   1290:  * NOTE: This routine must be called with a valid thread context,
                   1291:  * as it may block.
                   1292:  */
                   1293: void
1.163     thorpej  1294: if_detach(struct ifnet *ifp)
1.53      thorpej  1295: {
1.56      thorpej  1296:        struct socket so;
1.178     dyoung   1297:        struct ifaddr *ifa;
1.53      thorpej  1298: #ifdef IFAREF_DEBUG
                   1299:        struct ifaddr *last_ifa = NULL;
                   1300: #endif
1.56      thorpej  1301:        struct domain *dp;
1.141     matt     1302:        const struct protosw *pr;
1.322     riastrad 1303:        int s, i, family, purged;
1.276     rmind    1304:        uint64_t xc;
1.53      thorpej  1305:
1.349     ozaki-r  1306: #ifdef IFAREF_DEBUG
                   1307:        if_build_ifa_list(ifp);
                   1308: #endif
1.56      thorpej  1309:        /*
                   1310:         * XXX It's kind of lame that we have to have the
                   1311:         * XXX socket structure...
                   1312:         */
                   1313:        memset(&so, 0, sizeof(so));
1.53      thorpej  1314:
1.88      thorpej  1315:        s = splnet();
1.53      thorpej  1316:
1.334     ozaki-r  1317:        sysctl_teardown(&ifp->if_sysctl_log);
1.336     ozaki-r  1318:        mutex_enter(ifp->if_ioctl_lock);
1.350     ozaki-r  1319:        if_deactivate(ifp);
1.336     ozaki-r  1320:        mutex_exit(ifp->if_ioctl_lock);
1.334     ozaki-r  1321:
                   1322:        IFNET_LOCK();
1.319     ozaki-r  1323:        ifindex2ifnet[ifp->if_index] = NULL;
                   1324:        TAILQ_REMOVE(&ifnet_list, ifp, if_list);
1.334     ozaki-r  1325:        IFNET_WRITER_REMOVE(ifp);
                   1326:        pserialize_perform(ifnet_psz);
                   1327:        IFNET_UNLOCK();
                   1328:
1.357     ozaki-r  1329:        /* Wait for all readers to drain before freeing.  */
                   1330:        psref_target_destroy(&ifp->if_psref, ifnet_psref_class);
                   1331:        PSLIST_ENTRY_DESTROY(ifp, if_pslist_entry);
                   1332:
1.350     ozaki-r  1333:        if (ifp->if_slowtimo != NULL && ifp->if_slowtimo_ch != NULL) {
1.305     martin   1334:                ifp->if_slowtimo = NULL;
1.297     ozaki-r  1335:                callout_halt(ifp->if_slowtimo_ch, NULL);
                   1336:                callout_destroy(ifp->if_slowtimo_ch);
                   1337:                kmem_free(ifp->if_slowtimo_ch, sizeof(*ifp->if_slowtimo_ch));
1.296     ozaki-r  1338:        }
1.364     ozaki-r  1339:        if_deferred_start_destroy(ifp);
1.296     ozaki-r  1340:
1.53      thorpej  1341:        /*
                   1342:         * Do an if_down() to give protocols a chance to do something.
                   1343:         */
1.403     ozaki-r  1344:        if_down_deactivated(ifp);
1.86      thorpej  1345:
                   1346: #ifdef ALTQ
                   1347:        if (ALTQ_IS_ENABLED(&ifp->if_snd))
                   1348:                altq_disable(&ifp->if_snd);
                   1349:        if (ALTQ_IS_ATTACHED(&ifp->if_snd))
                   1350:                altq_detach(&ifp->if_snd);
1.87      thorpej  1351: #endif
                   1352:
1.355     knakahar 1353:        mutex_obj_free(ifp->if_snd.ifq_lock);
1.285     ozaki-r  1354:
1.166     liamjfoy 1355: #if NCARP > 0
                   1356:        /* Remove the interface from any carp group it is a part of.  */
1.185     dyoung   1357:        if (ifp->if_carp != NULL && ifp->if_type != IFT_CARP)
1.166     liamjfoy 1358:                carp_ifdetach(ifp);
                   1359: #endif
                   1360:
1.401     ozaki-r  1361:        /* carp_ifdetach still uses the lock */
                   1362:        mutex_obj_free(ifp->if_ioctl_lock);
                   1363:        ifp->if_ioctl_lock = NULL;
                   1364:
1.53      thorpej  1365:        /*
                   1366:         * Rip all the addresses off the interface.  This should make
                   1367:         * all of the routes go away.
1.178     dyoung   1368:         *
                   1369:         * pr_usrreq calls can remove an arbitrary number of ifaddrs
                   1370:         * from the list, including our "cursor", ifa.  For safety,
                   1371:         * and to honor the TAILQ abstraction, I just restart the
                   1372:         * loop after each removal.  Note that the loop will exit
                   1373:         * when all of the remaining ifaddrs belong to the AF_LINK
                   1374:         * family.  I am counting on the historical fact that at
                   1375:         * least one pr_usrreq in each address domain removes at
                   1376:         * least one ifaddr.
1.53      thorpej  1377:         */
1.178     dyoung   1378: again:
1.357     ozaki-r  1379:        /*
                   1380:         * At this point, no other one tries to remove ifa in the list,
1.407     ozaki-r  1381:         * so we don't need to take a lock or psref.  Avoid using
                   1382:         * IFADDR_READER_FOREACH to pass over an inspection of contract
                   1383:         * violations of pserialize.
1.357     ozaki-r  1384:         */
1.407     ozaki-r  1385:        IFADDR_WRITER_FOREACH(ifa, ifp) {
1.56      thorpej  1386:                family = ifa->ifa_addr->sa_family;
1.53      thorpej  1387: #ifdef IFAREF_DEBUG
                   1388:                printf("if_detach: ifaddr %p, family %d, refcnt %d\n",
1.56      thorpej  1389:                    ifa, family, ifa->ifa_refcnt);
1.53      thorpej  1390:                if (last_ifa != NULL && ifa == last_ifa)
1.56      thorpej  1391:                        panic("if_detach: loop detected");
1.53      thorpej  1392:                last_ifa = ifa;
                   1393: #endif
1.178     dyoung   1394:                if (family == AF_LINK)
1.118     itojun   1395:                        continue;
                   1396:                dp = pffinddomain(family);
1.384     ozaki-r  1397:                KASSERTMSG(dp != NULL, "no domain for AF %d", family);
1.160     gdt      1398:                /*
                   1399:                 * XXX These PURGEIF calls are redundant with the
                   1400:                 * purge-all-families calls below, but are left in for
                   1401:                 * now both to make a smaller change, and to avoid
                   1402:                 * unplanned interactions with clearing of
                   1403:                 * ifp->if_addrlist.
                   1404:                 */
1.118     itojun   1405:                purged = 0;
1.322     riastrad 1406:                for (pr = dp->dom_protosw;
                   1407:                     pr < dp->dom_protoswNPROTOSW; pr++) {
1.118     itojun   1408:                        so.so_proto = pr;
1.275     rmind    1409:                        if (pr->pr_usrreqs) {
1.290     rtr      1410:                                (void) (*pr->pr_usrreqs->pr_purgeif)(&so, ifp);
1.118     itojun   1411:                                purged = 1;
1.53      thorpej  1412:                        }
1.118     itojun   1413:                }
                   1414:                if (purged == 0) {
                   1415:                        /*
                   1416:                         * XXX What's really the best thing to do
1.135     keihan   1417:                         * XXX here?  --thorpej@NetBSD.org
1.118     itojun   1418:                         */
                   1419:                        printf("if_detach: WARNING: AF %d not purged\n",
                   1420:                            family);
1.207     dyoung   1421:                        ifa_remove(ifp, ifa);
1.53      thorpej  1422:                }
1.178     dyoung   1423:                goto again;
1.53      thorpej  1424:        }
1.118     itojun   1425:
                   1426:        if_free_sadl(ifp);
1.53      thorpej  1427:
1.362     ozaki-r  1428:        /* Delete stray routes from the routing table. */
                   1429:        for (i = 0; i <= AF_MAX; i++)
                   1430:                rt_delete_matched_entries(i, if_delroute_matcher, ifp);
1.106     itojun   1431:
1.152     matt     1432:        DOMAIN_FOREACH(dp) {
1.185     dyoung   1433:                if (dp->dom_ifdetach != NULL && ifp->if_afdata[dp->dom_family])
1.260     christos 1434:                {
                   1435:                        void *p = ifp->if_afdata[dp->dom_family];
                   1436:                        if (p) {
                   1437:                                ifp->if_afdata[dp->dom_family] = NULL;
                   1438:                                (*dp->dom_ifdetach)(ifp, p);
                   1439:                        }
                   1440:                }
1.160     gdt      1441:
                   1442:                /*
                   1443:                 * One would expect multicast memberships (INET and
                   1444:                 * INET6) on UDP sockets to be purged by the PURGEIF
                   1445:                 * calls above, but if all addresses were removed from
                   1446:                 * the interface prior to destruction, the calls will
                   1447:                 * not be made (e.g. ppp, for which pppd(8) generally
                   1448:                 * removes addresses before destroying the interface).
                   1449:                 * Because there is no invariant that multicast
                   1450:                 * memberships only exist for interfaces with IPv4
                   1451:                 * addresses, we must call PURGEIF regardless of
                   1452:                 * addresses.  (Protocols which might store ifnet
                   1453:                 * pointers are marked with PR_PURGEIF.)
                   1454:                 */
1.185     dyoung   1455:                for (pr = dp->dom_protosw; pr < dp->dom_protoswNPROTOSW; pr++) {
1.160     gdt      1456:                        so.so_proto = pr;
1.275     rmind    1457:                        if (pr->pr_usrreqs && pr->pr_flags & PR_PURGEIF)
1.290     rtr      1458:                                (void)(*pr->pr_usrreqs->pr_purgeif)(&so, ifp);
1.160     gdt      1459:                }
1.53      thorpej  1460:        }
1.57      thorpej  1461:
1.369     christos 1462:        pfil_run_ifhooks(if_pfil, PFIL_IFNET_DETACH, ifp);
1.265     rmind    1463:        (void)pfil_head_destroy(ifp->if_pfil);
1.184     dyoung   1464:
1.57      thorpej  1465:        /* Announce that the interface is gone. */
                   1466:        rt_ifannouncemsg(ifp, IFAN_DEPARTURE);
1.93      itojun   1467:
1.320     ozaki-r  1468:        IF_AFDATA_LOCK_DESTROY(ifp);
                   1469:
1.347     knakahar 1470:        if (if_is_link_state_changeable(ifp)) {
1.346     knakahar 1471:                softint_disestablish(ifp->if_link_si);
                   1472:                ifp->if_link_si = NULL;
                   1473:        }
1.324     ozaki-r  1474:
1.95      itojun   1475:        /*
1.168     matt     1476:         * remove packets that came from ifp, from software interrupt queues.
1.95      itojun   1477:         */
1.168     matt     1478:        DOMAIN_FOREACH(dp) {
                   1479:                for (i = 0; i < __arraycount(dp->dom_ifqueues); i++) {
1.260     christos 1480:                        struct ifqueue *iq = dp->dom_ifqueues[i];
                   1481:                        if (iq == NULL)
1.168     matt     1482:                                break;
1.260     christos 1483:                        dp->dom_ifqueues[i] = NULL;
                   1484:                        if_detach_queues(ifp, iq);
1.168     matt     1485:                }
                   1486:        }
1.95      itojun   1487:
1.276     rmind    1488:        /*
                   1489:         * IP queues have to be processed separately: net-queue barrier
                   1490:         * ensures that the packets are dequeued while a cross-call will
                   1491:         * ensure that the interrupts have completed. FIXME: not quite..
                   1492:         */
1.278     he       1493: #ifdef INET
1.276     rmind    1494:        pktq_barrier(ip_pktq);
1.278     he       1495: #endif
1.281     rmind    1496: #ifdef INET6
1.293     pooka    1497:        if (in6_present)
                   1498:                pktq_barrier(ip6_pktq);
1.281     rmind    1499: #endif
1.276     rmind    1500:        xc = xc_broadcast(0, (xcfunc_t)nullop, NULL, NULL);
                   1501:        xc_wait(xc);
                   1502:
1.323     ozaki-r  1503:        if (ifp->if_percpuq != NULL) {
                   1504:                if_percpuq_destroy(ifp->if_percpuq);
                   1505:                ifp->if_percpuq = NULL;
                   1506:        }
                   1507:
1.53      thorpej  1508:        splx(s);
1.349     ozaki-r  1509:
                   1510: #ifdef IFAREF_DEBUG
                   1511:        if_check_and_free_ifa_list(ifp);
                   1512: #endif
1.95      itojun   1513: }
                   1514:
                   1515: static void
1.163     thorpej  1516: if_detach_queues(struct ifnet *ifp, struct ifqueue *q)
1.95      itojun   1517: {
                   1518:        struct mbuf *m, *prev, *next;
                   1519:
                   1520:        prev = NULL;
1.185     dyoung   1521:        for (m = q->ifq_head; m != NULL; m = next) {
1.274     rmind    1522:                KASSERT((m->m_flags & M_PKTHDR) != 0);
                   1523:
1.95      itojun   1524:                next = m->m_nextpkt;
1.338     ozaki-r  1525:                if (m->m_pkthdr.rcvif_index != ifp->if_index) {
1.96      itojun   1526:                        prev = m;
1.95      itojun   1527:                        continue;
1.96      itojun   1528:                }
1.95      itojun   1529:
1.185     dyoung   1530:                if (prev != NULL)
1.95      itojun   1531:                        prev->m_nextpkt = m->m_nextpkt;
                   1532:                else
                   1533:                        q->ifq_head = m->m_nextpkt;
                   1534:                if (q->ifq_tail == m)
                   1535:                        q->ifq_tail = prev;
                   1536:                q->ifq_len--;
                   1537:
                   1538:                m->m_nextpkt = NULL;
                   1539:                m_freem(m);
                   1540:                IF_DROP(q);
                   1541:        }
1.53      thorpej  1542: }
                   1543:
                   1544: /*
                   1545:  * Callback for a radix tree walk to delete all references to an
                   1546:  * ifnet.
                   1547:  */
1.163     thorpej  1548: static int
1.362     ozaki-r  1549: if_delroute_matcher(struct rtentry *rt, void *v)
1.53      thorpej  1550: {
1.55      itojun   1551:        struct ifnet *ifp = (struct ifnet *)v;
1.53      thorpej  1552:
1.362     ozaki-r  1553:        if (rt->rt_ifp == ifp)
                   1554:                return 1;
                   1555:        else
1.185     dyoung   1556:                return 0;
1.53      thorpej  1557: }
                   1558:
1.1       cgd      1559: /*
1.63      thorpej  1560:  * Create a clone network interface.
                   1561:  */
1.302     ozaki-r  1562: static int
1.163     thorpej  1563: if_clone_create(const char *name)
1.63      thorpej  1564: {
                   1565:        struct if_clone *ifc;
                   1566:        int unit;
1.336     ozaki-r  1567:        struct ifnet *ifp;
                   1568:        struct psref psref;
1.63      thorpej  1569:
1.372     ozaki-r  1570:        KASSERT(mutex_owned(&if_clone_mtx));
                   1571:
1.63      thorpej  1572:        ifc = if_clone_lookup(name, &unit);
                   1573:        if (ifc == NULL)
1.185     dyoung   1574:                return EINVAL;
1.63      thorpej  1575:
1.336     ozaki-r  1576:        ifp = if_get(name, &psref);
                   1577:        if (ifp != NULL) {
                   1578:                if_put(ifp, &psref);
1.185     dyoung   1579:                return EEXIST;
1.336     ozaki-r  1580:        }
1.63      thorpej  1581:
1.185     dyoung   1582:        return (*ifc->ifc_create)(ifc, unit);
1.63      thorpej  1583: }
                   1584:
                   1585: /*
                   1586:  * Destroy a clone network interface.
                   1587:  */
1.302     ozaki-r  1588: static int
1.163     thorpej  1589: if_clone_destroy(const char *name)
1.63      thorpej  1590: {
                   1591:        struct if_clone *ifc;
                   1592:        struct ifnet *ifp;
1.336     ozaki-r  1593:        struct psref psref;
1.63      thorpej  1594:
1.372     ozaki-r  1595:        KASSERT(mutex_owned(&if_clone_mtx));
                   1596:
1.63      thorpej  1597:        ifc = if_clone_lookup(name, NULL);
                   1598:        if (ifc == NULL)
1.185     dyoung   1599:                return EINVAL;
1.63      thorpej  1600:
1.336     ozaki-r  1601:        if (ifc->ifc_destroy == NULL)
                   1602:                return EOPNOTSUPP;
                   1603:
                   1604:        ifp = if_get(name, &psref);
1.63      thorpej  1605:        if (ifp == NULL)
1.185     dyoung   1606:                return ENXIO;
1.63      thorpej  1607:
1.336     ozaki-r  1608:        /* We have to disable ioctls here */
                   1609:        mutex_enter(ifp->if_ioctl_lock);
                   1610:        ifp->if_ioctl = if_nullioctl;
                   1611:        mutex_exit(ifp->if_ioctl_lock);
                   1612:
                   1613:        /*
                   1614:         * We cannot call ifc_destroy with holding ifp.
                   1615:         * Releasing ifp here is safe thanks to if_clone_mtx.
                   1616:         */
                   1617:        if_put(ifp, &psref);
1.63      thorpej  1618:
1.185     dyoung   1619:        return (*ifc->ifc_destroy)(ifp);
1.63      thorpej  1620: }
                   1621:
1.379     knakahar 1622: static bool
                   1623: if_is_unit(const char *name)
                   1624: {
                   1625:
                   1626:        while(*name != '\0') {
                   1627:                if (*name < '0' || *name > '9')
                   1628:                        return false;
                   1629:                name++;
                   1630:        }
                   1631:
                   1632:        return true;
                   1633: }
                   1634:
1.63      thorpej  1635: /*
                   1636:  * Look up a network interface cloner.
                   1637:  */
1.163     thorpej  1638: static struct if_clone *
                   1639: if_clone_lookup(const char *name, int *unitp)
1.63      thorpej  1640: {
                   1641:        struct if_clone *ifc;
                   1642:        const char *cp;
1.262     christos 1643:        char *dp, ifname[IFNAMSIZ + 3];
1.128     itojun   1644:        int unit;
1.63      thorpej  1645:
1.372     ozaki-r  1646:        KASSERT(mutex_owned(&if_clone_mtx));
                   1647:
1.262     christos 1648:        strcpy(ifname, "if_");
1.128     itojun   1649:        /* separate interface name from unit */
1.379     knakahar 1650:        /* TODO: search unit number from backward */
1.262     christos 1651:        for (dp = ifname + 3, cp = name; cp - name < IFNAMSIZ &&
1.379     knakahar 1652:            *cp && !if_is_unit(cp);)
1.262     christos 1653:                *dp++ = *cp++;
1.128     itojun   1654:
                   1655:        if (cp == name || cp - name == IFNAMSIZ || !*cp)
1.185     dyoung   1656:                return NULL;    /* No name or unit number */
1.262     christos 1657:        *dp++ = '\0';
1.128     itojun   1658:
1.262     christos 1659: again:
1.128     itojun   1660:        LIST_FOREACH(ifc, &if_cloners, ifc_list) {
1.262     christos 1661:                if (strcmp(ifname + 3, ifc->ifc_name) == 0)
1.128     itojun   1662:                        break;
1.63      thorpej  1663:        }
                   1664:
1.262     christos 1665:        if (ifc == NULL) {
1.375     christos 1666:                int error;
                   1667:                if (*ifname == '\0')
                   1668:                        return NULL;
                   1669:                mutex_exit(&if_clone_mtx);
                   1670:                error = module_autoload(ifname, MODULE_CLASS_DRIVER);
                   1671:                mutex_enter(&if_clone_mtx);
                   1672:                if (error)
1.262     christos 1673:                        return NULL;
                   1674:                *ifname = '\0';
                   1675:                goto again;
                   1676:        }
1.63      thorpej  1677:
1.128     itojun   1678:        unit = 0;
1.129     itojun   1679:        while (cp - name < IFNAMSIZ && *cp) {
1.245     christos 1680:                if (*cp < '0' || *cp > '9' || unit >= INT_MAX / 10) {
1.63      thorpej  1681:                        /* Bogus unit number. */
1.185     dyoung   1682:                        return NULL;
1.63      thorpej  1683:                }
1.128     itojun   1684:                unit = (unit * 10) + (*cp++ - '0');
1.63      thorpej  1685:        }
                   1686:
                   1687:        if (unitp != NULL)
1.128     itojun   1688:                *unitp = unit;
1.185     dyoung   1689:        return ifc;
1.63      thorpej  1690: }
                   1691:
                   1692: /*
                   1693:  * Register a network interface cloner.
                   1694:  */
                   1695: void
1.163     thorpej  1696: if_clone_attach(struct if_clone *ifc)
1.63      thorpej  1697: {
                   1698:
1.372     ozaki-r  1699:        mutex_enter(&if_clone_mtx);
1.63      thorpej  1700:        LIST_INSERT_HEAD(&if_cloners, ifc, ifc_list);
1.67      thorpej  1701:        if_cloners_count++;
1.372     ozaki-r  1702:        mutex_exit(&if_clone_mtx);
1.63      thorpej  1703: }
                   1704:
                   1705: /*
                   1706:  * Unregister a network interface cloner.
                   1707:  */
                   1708: void
1.163     thorpej  1709: if_clone_detach(struct if_clone *ifc)
1.63      thorpej  1710: {
                   1711:
1.377     christos 1712:        mutex_enter(&if_clone_mtx);
1.63      thorpej  1713:        LIST_REMOVE(ifc, ifc_list);
1.67      thorpej  1714:        if_cloners_count--;
1.377     christos 1715:        mutex_exit(&if_clone_mtx);
1.67      thorpej  1716: }
                   1717:
                   1718: /*
                   1719:  * Provide list of interface cloners to userspace.
                   1720:  */
1.315     martin   1721: int
                   1722: if_clone_list(int buf_count, char *buffer, int *total)
1.67      thorpej  1723: {
                   1724:        char outbuf[IFNAMSIZ], *dst;
                   1725:        struct if_clone *ifc;
                   1726:        int count, error = 0;
                   1727:
1.372     ozaki-r  1728:        mutex_enter(&if_clone_mtx);
1.315     martin   1729:        *total = if_cloners_count;
                   1730:        if ((dst = buffer) == NULL) {
1.67      thorpej  1731:                /* Just asking how many there are. */
1.372     ozaki-r  1732:                goto out;
1.67      thorpej  1733:        }
                   1734:
1.372     ozaki-r  1735:        if (buf_count < 0) {
                   1736:                error = EINVAL;
                   1737:                goto out;
                   1738:        }
1.67      thorpej  1739:
1.315     martin   1740:        count = (if_cloners_count < buf_count) ?
                   1741:            if_cloners_count : buf_count;
1.67      thorpej  1742:
                   1743:        for (ifc = LIST_FIRST(&if_cloners); ifc != NULL && count != 0;
                   1744:             ifc = LIST_NEXT(ifc, ifc_list), count--, dst += IFNAMSIZ) {
1.175     christos 1745:                (void)strncpy(outbuf, ifc->ifc_name, sizeof(outbuf));
1.372     ozaki-r  1746:                if (outbuf[sizeof(outbuf) - 1] != '\0') {
                   1747:                        error = ENAMETOOLONG;
                   1748:                        goto out;
                   1749:                }
1.172     christos 1750:                error = copyout(outbuf, dst, sizeof(outbuf));
1.185     dyoung   1751:                if (error != 0)
1.67      thorpej  1752:                        break;
                   1753:        }
                   1754:
1.372     ozaki-r  1755: out:
                   1756:        mutex_exit(&if_clone_mtx);
1.185     dyoung   1757:        return error;
1.63      thorpej  1758: }
                   1759:
1.207     dyoung   1760: void
1.357     ozaki-r  1761: ifa_psref_init(struct ifaddr *ifa)
                   1762: {
                   1763:
                   1764:        psref_target_init(&ifa->ifa_psref, ifa_psref_class);
                   1765: }
                   1766:
                   1767: void
1.291     rmind    1768: ifaref(struct ifaddr *ifa)
                   1769: {
1.366     ozaki-r  1770:        KASSERT(!ISSET(ifa->ifa_flags, IFA_DESTROYING));
1.291     rmind    1771:        ifa->ifa_refcnt++;
                   1772: }
                   1773:
                   1774: void
                   1775: ifafree(struct ifaddr *ifa)
                   1776: {
                   1777:        KASSERT(ifa != NULL);
                   1778:        KASSERT(ifa->ifa_refcnt > 0);
                   1779:
                   1780:        if (--ifa->ifa_refcnt == 0) {
                   1781:                free(ifa, M_IFADDR);
                   1782:        }
                   1783: }
                   1784:
1.366     ozaki-r  1785: bool
                   1786: ifa_is_destroying(struct ifaddr *ifa)
                   1787: {
                   1788:
                   1789:        return ISSET(ifa->ifa_flags, IFA_DESTROYING);
                   1790: }
                   1791:
1.291     rmind    1792: void
1.207     dyoung   1793: ifa_insert(struct ifnet *ifp, struct ifaddr *ifa)
                   1794: {
1.357     ozaki-r  1795:
1.207     dyoung   1796:        ifa->ifa_ifp = ifp;
1.357     ozaki-r  1797:
                   1798:        IFNET_LOCK();
1.208     dyoung   1799:        TAILQ_INSERT_TAIL(&ifp->if_addrlist, ifa, ifa_list);
1.354     ozaki-r  1800:        IFADDR_ENTRY_INIT(ifa);
                   1801:        IFADDR_WRITER_INSERT_TAIL(ifp, ifa);
1.357     ozaki-r  1802:        IFNET_UNLOCK();
                   1803:
1.291     rmind    1804:        ifaref(ifa);
1.207     dyoung   1805: }
                   1806:
                   1807: void
                   1808: ifa_remove(struct ifnet *ifp, struct ifaddr *ifa)
                   1809: {
1.357     ozaki-r  1810:
1.207     dyoung   1811:        KASSERT(ifa->ifa_ifp == ifp);
1.357     ozaki-r  1812:
                   1813:        IFNET_LOCK();
1.207     dyoung   1814:        TAILQ_REMOVE(&ifp->if_addrlist, ifa, ifa_list);
1.354     ozaki-r  1815:        IFADDR_WRITER_REMOVE(ifa);
1.359     ozaki-r  1816: #ifdef NET_MPSAFE
1.357     ozaki-r  1817:        pserialize_perform(ifnet_psz);
                   1818: #endif
                   1819:        IFNET_UNLOCK();
                   1820:
1.359     ozaki-r  1821: #ifdef NET_MPSAFE
1.357     ozaki-r  1822:        psref_target_destroy(&ifa->ifa_psref, ifa_psref_class);
                   1823: #endif
1.360     ozaki-r  1824:        IFADDR_ENTRY_DESTROY(ifa);
1.291     rmind    1825:        ifafree(ifa);
1.207     dyoung   1826: }
                   1827:
1.357     ozaki-r  1828: void
                   1829: ifa_acquire(struct ifaddr *ifa, struct psref *psref)
                   1830: {
                   1831:
                   1832:        psref_acquire(psref, &ifa->ifa_psref, ifa_psref_class);
                   1833: }
                   1834:
                   1835: void
                   1836: ifa_release(struct ifaddr *ifa, struct psref *psref)
                   1837: {
                   1838:
                   1839:        if (ifa == NULL)
                   1840:                return;
                   1841:
                   1842:        psref_release(psref, &ifa->ifa_psref, ifa_psref_class);
                   1843: }
                   1844:
                   1845: bool
                   1846: ifa_held(struct ifaddr *ifa)
                   1847: {
                   1848:
                   1849:        return psref_held(&ifa->ifa_psref, ifa_psref_class);
                   1850: }
                   1851:
1.194     dyoung   1852: static inline int
                   1853: equal(const struct sockaddr *sa1, const struct sockaddr *sa2)
                   1854: {
                   1855:        return sockaddr_cmp(sa1, sa2) == 0;
                   1856: }
                   1857:
1.63      thorpej  1858: /*
1.1       cgd      1859:  * Locate an interface based on a complete address.
                   1860:  */
                   1861: /*ARGSUSED*/
                   1862: struct ifaddr *
1.163     thorpej  1863: ifa_ifwithaddr(const struct sockaddr *addr)
1.1       cgd      1864: {
1.61      augustss 1865:        struct ifnet *ifp;
                   1866:        struct ifaddr *ifa;
1.1       cgd      1867:
1.334     ozaki-r  1868:        IFNET_READER_FOREACH(ifp) {
1.348     ozaki-r  1869:                if (if_is_deactivated(ifp))
1.1       cgd      1870:                        continue;
1.354     ozaki-r  1871:                IFADDR_READER_FOREACH(ifa, ifp) {
1.53      thorpej  1872:                        if (ifa->ifa_addr->sa_family != addr->sa_family)
                   1873:                                continue;
                   1874:                        if (equal(addr, ifa->ifa_addr))
1.185     dyoung   1875:                                return ifa;
1.53      thorpej  1876:                        if ((ifp->if_flags & IFF_BROADCAST) &&
                   1877:                            ifa->ifa_broadaddr &&
                   1878:                            /* IP6 doesn't have broadcast */
                   1879:                            ifa->ifa_broadaddr->sa_len != 0 &&
                   1880:                            equal(ifa->ifa_broadaddr, addr))
1.185     dyoung   1881:                                return ifa;
1.53      thorpej  1882:                }
1.1       cgd      1883:        }
1.357     ozaki-r  1884:        return NULL;
                   1885: }
                   1886:
                   1887: struct ifaddr *
                   1888: ifa_ifwithaddr_psref(const struct sockaddr *addr, struct psref *psref)
                   1889: {
                   1890:        struct ifaddr *ifa;
                   1891:        int s = pserialize_read_enter();
                   1892:
                   1893:        ifa = ifa_ifwithaddr(addr);
                   1894:        if (ifa != NULL)
                   1895:                ifa_acquire(ifa, psref);
1.334     ozaki-r  1896:        pserialize_read_exit(s);
1.357     ozaki-r  1897:
                   1898:        return ifa;
1.1       cgd      1899: }
1.49      itojun   1900:
1.1       cgd      1901: /*
                   1902:  * Locate the point to point interface with a given destination address.
                   1903:  */
                   1904: /*ARGSUSED*/
                   1905: struct ifaddr *
1.163     thorpej  1906: ifa_ifwithdstaddr(const struct sockaddr *addr)
1.1       cgd      1907: {
1.61      augustss 1908:        struct ifnet *ifp;
                   1909:        struct ifaddr *ifa;
1.1       cgd      1910:
1.334     ozaki-r  1911:        IFNET_READER_FOREACH(ifp) {
1.348     ozaki-r  1912:                if (if_is_deactivated(ifp))
1.53      thorpej  1913:                        continue;
1.185     dyoung   1914:                if ((ifp->if_flags & IFF_POINTOPOINT) == 0)
                   1915:                        continue;
1.354     ozaki-r  1916:                IFADDR_READER_FOREACH(ifa, ifp) {
1.185     dyoung   1917:                        if (ifa->ifa_addr->sa_family != addr->sa_family ||
                   1918:                            ifa->ifa_dstaddr == NULL)
                   1919:                                continue;
                   1920:                        if (equal(addr, ifa->ifa_dstaddr))
                   1921:                                return ifa;
1.53      thorpej  1922:                }
1.1       cgd      1923:        }
1.357     ozaki-r  1924:
                   1925:        return NULL;
                   1926: }
                   1927:
                   1928: struct ifaddr *
                   1929: ifa_ifwithdstaddr_psref(const struct sockaddr *addr, struct psref *psref)
                   1930: {
                   1931:        struct ifaddr *ifa;
                   1932:        int s;
                   1933:
                   1934:        s = pserialize_read_enter();
                   1935:        ifa = ifa_ifwithdstaddr(addr);
                   1936:        if (ifa != NULL)
                   1937:                ifa_acquire(ifa, psref);
1.334     ozaki-r  1938:        pserialize_read_exit(s);
1.357     ozaki-r  1939:
                   1940:        return ifa;
1.1       cgd      1941: }
                   1942:
                   1943: /*
                   1944:  * Find an interface on a specific network.  If many, choice
1.15      mycroft  1945:  * is most specific found.
1.1       cgd      1946:  */
                   1947: struct ifaddr *
1.163     thorpej  1948: ifa_ifwithnet(const struct sockaddr *addr)
1.1       cgd      1949: {
1.61      augustss 1950:        struct ifnet *ifp;
1.357     ozaki-r  1951:        struct ifaddr *ifa, *ifa_maybe = NULL;
1.140     matt     1952:        const struct sockaddr_dl *sdl;
1.1       cgd      1953:        u_int af = addr->sa_family;
1.171     pooka    1954:        const char *addr_data = addr->sa_data, *cplim;
1.1       cgd      1955:
                   1956:        if (af == AF_LINK) {
1.195     dyoung   1957:                sdl = satocsdl(addr);
1.137     itojun   1958:                if (sdl->sdl_index && sdl->sdl_index < if_indexlim &&
                   1959:                    ifindex2ifnet[sdl->sdl_index] &&
1.348     ozaki-r  1960:                    !if_is_deactivated(ifindex2ifnet[sdl->sdl_index])) {
1.316     ozaki-r  1961:                        return ifindex2ifnet[sdl->sdl_index]->if_dl;
                   1962:                }
1.1       cgd      1963:        }
1.51      bouyer   1964: #ifdef NETATALK
                   1965:        if (af == AF_APPLETALK) {
1.140     matt     1966:                const struct sockaddr_at *sat, *sat2;
1.158     christos 1967:                sat = (const struct sockaddr_at *)addr;
1.334     ozaki-r  1968:                IFNET_READER_FOREACH(ifp) {
1.348     ozaki-r  1969:                        if (if_is_deactivated(ifp))
1.53      thorpej  1970:                                continue;
1.158     christos 1971:                        ifa = at_ifawithnet((const struct sockaddr_at *)addr, ifp);
1.62      bouyer   1972:                        if (ifa == NULL)
                   1973:                                continue;
                   1974:                        sat2 = (struct sockaddr_at *)ifa->ifa_addr;
                   1975:                        if (sat2->sat_addr.s_net == sat->sat_addr.s_net)
1.185     dyoung   1976:                                return ifa; /* exact match */
1.62      bouyer   1977:                        if (ifa_maybe == NULL) {
1.112     wiz      1978:                                /* else keep the if with the right range */
1.62      bouyer   1979:                                ifa_maybe = ifa;
                   1980:                        }
1.51      bouyer   1981:                }
1.185     dyoung   1982:                return ifa_maybe;
1.51      bouyer   1983:        }
                   1984: #endif
1.334     ozaki-r  1985:        IFNET_READER_FOREACH(ifp) {
1.348     ozaki-r  1986:                if (if_is_deactivated(ifp))
1.53      thorpej  1987:                        continue;
1.354     ozaki-r  1988:                IFADDR_READER_FOREACH(ifa, ifp) {
1.171     pooka    1989:                        const char *cp, *cp2, *cp3;
1.15      mycroft  1990:
                   1991:                        if (ifa->ifa_addr->sa_family != af ||
1.185     dyoung   1992:                            ifa->ifa_netmask == NULL)
1.53      thorpej  1993:  next:                         continue;
1.15      mycroft  1994:                        cp = addr_data;
                   1995:                        cp2 = ifa->ifa_addr->sa_data;
                   1996:                        cp3 = ifa->ifa_netmask->sa_data;
1.171     pooka    1997:                        cplim = (const char *)ifa->ifa_netmask +
1.53      thorpej  1998:                            ifa->ifa_netmask->sa_len;
                   1999:                        while (cp3 < cplim) {
                   2000:                                if ((*cp++ ^ *cp2++) & *cp3++) {
                   2001:                                        /* want to continue for() loop */
1.32      mrg      2002:                                        goto next;
1.53      thorpej  2003:                                }
                   2004:                        }
1.185     dyoung   2005:                        if (ifa_maybe == NULL ||
1.329     ozaki-r  2006:                            rt_refines(ifa->ifa_netmask,
                   2007:                                       ifa_maybe->ifa_netmask))
1.15      mycroft  2008:                                ifa_maybe = ifa;
                   2009:                }
1.53      thorpej  2010:        }
1.357     ozaki-r  2011:        return ifa_maybe;
                   2012: }
                   2013:
                   2014: struct ifaddr *
                   2015: ifa_ifwithnet_psref(const struct sockaddr *addr, struct psref *psref)
                   2016: {
                   2017:        struct ifaddr *ifa;
                   2018:        int s;
                   2019:
                   2020:        s = pserialize_read_enter();
                   2021:        ifa = ifa_ifwithnet(addr);
                   2022:        if (ifa != NULL)
                   2023:                ifa_acquire(ifa, psref);
1.334     ozaki-r  2024:        pserialize_read_exit(s);
1.357     ozaki-r  2025:
                   2026:        return ifa;
1.26      mrg      2027: }
1.53      thorpej  2028:
1.26      mrg      2029: /*
                   2030:  * Find the interface of the addresss.
                   2031:  */
                   2032: struct ifaddr *
1.163     thorpej  2033: ifa_ifwithladdr(const struct sockaddr *addr)
1.26      mrg      2034: {
                   2035:        struct ifaddr *ia;
                   2036:
1.53      thorpej  2037:        if ((ia = ifa_ifwithaddr(addr)) || (ia = ifa_ifwithdstaddr(addr)) ||
                   2038:            (ia = ifa_ifwithnet(addr)))
1.185     dyoung   2039:                return ia;
                   2040:        return NULL;
1.1       cgd      2041: }
                   2042:
1.357     ozaki-r  2043: struct ifaddr *
                   2044: ifa_ifwithladdr_psref(const struct sockaddr *addr, struct psref *psref)
                   2045: {
                   2046:        struct ifaddr *ifa;
                   2047:        int s;
                   2048:
                   2049:        s = pserialize_read_enter();
                   2050:        ifa = ifa_ifwithladdr(addr);
                   2051:        if (ifa != NULL)
                   2052:                ifa_acquire(ifa, psref);
                   2053:        pserialize_read_exit(s);
                   2054:
                   2055:        return ifa;
                   2056: }
                   2057:
1.1       cgd      2058: /*
                   2059:  * Find an interface using a specific address family
                   2060:  */
                   2061: struct ifaddr *
1.163     thorpej  2062: ifa_ifwithaf(int af)
1.1       cgd      2063: {
1.61      augustss 2064:        struct ifnet *ifp;
1.334     ozaki-r  2065:        struct ifaddr *ifa = NULL;
                   2066:        int s;
1.1       cgd      2067:
1.334     ozaki-r  2068:        s = pserialize_read_enter();
                   2069:        IFNET_READER_FOREACH(ifp) {
1.348     ozaki-r  2070:                if (if_is_deactivated(ifp))
1.53      thorpej  2071:                        continue;
1.354     ozaki-r  2072:                IFADDR_READER_FOREACH(ifa, ifp) {
1.21      mycroft  2073:                        if (ifa->ifa_addr->sa_family == af)
1.334     ozaki-r  2074:                                goto out;
1.53      thorpej  2075:                }
                   2076:        }
1.334     ozaki-r  2077: out:
                   2078:        pserialize_read_exit(s);
                   2079:        return ifa;
1.1       cgd      2080: }
                   2081:
                   2082: /*
                   2083:  * Find an interface address specific to an interface best matching
                   2084:  * a given address.
                   2085:  */
                   2086: struct ifaddr *
1.163     thorpej  2087: ifaof_ifpforaddr(const struct sockaddr *addr, struct ifnet *ifp)
1.1       cgd      2088: {
1.61      augustss 2089:        struct ifaddr *ifa;
1.140     matt     2090:        const char *cp, *cp2, *cp3;
                   2091:        const char *cplim;
1.1       cgd      2092:        struct ifaddr *ifa_maybe = 0;
                   2093:        u_int af = addr->sa_family;
                   2094:
1.348     ozaki-r  2095:        if (if_is_deactivated(ifp))
1.185     dyoung   2096:                return NULL;
1.53      thorpej  2097:
1.1       cgd      2098:        if (af >= AF_MAX)
1.185     dyoung   2099:                return NULL;
1.53      thorpej  2100:
1.354     ozaki-r  2101:        IFADDR_READER_FOREACH(ifa, ifp) {
1.1       cgd      2102:                if (ifa->ifa_addr->sa_family != af)
                   2103:                        continue;
                   2104:                ifa_maybe = ifa;
1.185     dyoung   2105:                if (ifa->ifa_netmask == NULL) {
1.1       cgd      2106:                        if (equal(addr, ifa->ifa_addr) ||
1.53      thorpej  2107:                            (ifa->ifa_dstaddr &&
                   2108:                             equal(addr, ifa->ifa_dstaddr)))
1.185     dyoung   2109:                                return ifa;
1.1       cgd      2110:                        continue;
                   2111:                }
                   2112:                cp = addr->sa_data;
                   2113:                cp2 = ifa->ifa_addr->sa_data;
                   2114:                cp3 = ifa->ifa_netmask->sa_data;
                   2115:                cplim = ifa->ifa_netmask->sa_len + (char *)ifa->ifa_netmask;
1.53      thorpej  2116:                for (; cp3 < cplim; cp3++) {
1.1       cgd      2117:                        if ((*cp++ ^ *cp2++) & *cp3)
                   2118:                                break;
1.53      thorpej  2119:                }
1.1       cgd      2120:                if (cp3 == cplim)
1.185     dyoung   2121:                        return ifa;
1.1       cgd      2122:        }
1.185     dyoung   2123:        return ifa_maybe;
1.1       cgd      2124: }
1.9       mycroft  2125:
1.357     ozaki-r  2126: struct ifaddr *
                   2127: ifaof_ifpforaddr_psref(const struct sockaddr *addr, struct ifnet *ifp,
                   2128:     struct psref *psref)
                   2129: {
                   2130:        struct ifaddr *ifa;
                   2131:        int s;
                   2132:
                   2133:        s = pserialize_read_enter();
                   2134:        ifa = ifaof_ifpforaddr(addr, ifp);
                   2135:        if (ifa != NULL)
                   2136:                ifa_acquire(ifa, psref);
                   2137:        pserialize_read_exit(s);
                   2138:
                   2139:        return ifa;
                   2140: }
                   2141:
1.1       cgd      2142: /*
                   2143:  * Default action when installing a route with a Link Level gateway.
                   2144:  * Lookup an appropriate real ifa to point to.
                   2145:  * This should be moved to /sys/net/link.c eventually.
                   2146:  */
1.15      mycroft  2147: void
1.228     dyoung   2148: link_rtrequest(int cmd, struct rtentry *rt, const struct rt_addrinfo *info)
1.1       cgd      2149: {
1.61      augustss 2150:        struct ifaddr *ifa;
1.194     dyoung   2151:        const struct sockaddr *dst;
1.15      mycroft  2152:        struct ifnet *ifp;
1.357     ozaki-r  2153:        struct psref psref;
1.1       cgd      2154:
1.225     dyoung   2155:        if (cmd != RTM_ADD || (ifa = rt->rt_ifa) == NULL ||
                   2156:            (ifp = ifa->ifa_ifp) == NULL || (dst = rt_getkey(rt)) == NULL)
1.1       cgd      2157:                return;
1.357     ozaki-r  2158:        if ((ifa = ifaof_ifpforaddr_psref(dst, ifp, &psref)) != NULL) {
1.176     dyoung   2159:                rt_replace_ifa(rt, ifa);
1.1       cgd      2160:                if (ifa->ifa_rtrequest && ifa->ifa_rtrequest != link_rtrequest)
1.82      itojun   2161:                        ifa->ifa_rtrequest(cmd, rt, info);
1.357     ozaki-r  2162:                ifa_release(ifa, &psref);
1.1       cgd      2163:        }
                   2164: }
                   2165:
                   2166: /*
1.325     roy      2167:  * bitmask macros to manage a densely packed link_state change queue.
                   2168:  * Because we need to store LINK_STATE_UNKNOWN(0), LINK_STATE_DOWN(1) and
                   2169:  * LINK_STATE_UP(2) we need 2 bits for each state change.
                   2170:  * As a state change to store is 0, treat all bits set as an unset item.
                   2171:  */
                   2172: #define LQ_ITEM_BITS           2
                   2173: #define LQ_ITEM_MASK           ((1 << LQ_ITEM_BITS) - 1)
                   2174: #define LQ_MASK(i)             (LQ_ITEM_MASK << (i) * LQ_ITEM_BITS)
                   2175: #define LINK_STATE_UNSET       LQ_ITEM_MASK
                   2176: #define LQ_ITEM(q, i)          (((q) & LQ_MASK((i))) >> (i) * LQ_ITEM_BITS)
                   2177: #define LQ_STORE(q, i, v)                                                    \
                   2178:        do {                                                                  \
                   2179:                (q) &= ~LQ_MASK((i));                                         \
                   2180:                (q) |= (v) << (i) * LQ_ITEM_BITS;                             \
                   2181:        } while (0 /* CONSTCOND */)
                   2182: #define LQ_MAX(q)              ((sizeof((q)) * NBBY) / LQ_ITEM_BITS)
                   2183: #define LQ_POP(q, v)                                                         \
                   2184:        do {                                                                  \
                   2185:                (v) = LQ_ITEM((q), 0);                                        \
                   2186:                (q) >>= LQ_ITEM_BITS;                                         \
                   2187:                (q) |= LINK_STATE_UNSET << (LQ_MAX((q)) - 1) * LQ_ITEM_BITS;  \
                   2188:        } while (0 /* CONSTCOND */)
                   2189: #define LQ_PUSH(q, v)                                                        \
                   2190:        do {                                                                  \
                   2191:                (q) >>= LQ_ITEM_BITS;                                         \
                   2192:                (q) |= (v) << (LQ_MAX((q)) - 1) * LQ_ITEM_BITS;               \
                   2193:        } while (0 /* CONSTCOND */)
                   2194: #define LQ_FIND_UNSET(q, i)                                                  \
                   2195:        for ((i) = 0; i < LQ_MAX((q)); (i)++) {                               \
                   2196:                if (LQ_ITEM((q), (i)) == LINK_STATE_UNSET)                    \
                   2197:                        break;                                                \
                   2198:        }
1.406     ozaki-r  2199:
                   2200: /*
                   2201:  * XXX reusing (ifp)->if_snd->ifq_lock rather than having another spin mutex
                   2202:  * for each ifnet.  It doesn't matter because:
                   2203:  * - if IFEF_MPSAFE is enabled, if_snd isn't used and lock contentions on
                   2204:  *   ifq_lock don't happen
                   2205:  * - if IFEF_MPSAFE is disabled, there is no lock contention on ifq_lock
                   2206:  *   because if_snd, if_link_state_change and if_link_state_change_softint
                   2207:  *   are all called with KERNEL_LOCK
                   2208:  */
                   2209: #define IF_LINK_STATE_CHANGE_LOCK(ifp)         \
                   2210:        mutex_enter((ifp)->if_snd.ifq_lock)
                   2211: #define IF_LINK_STATE_CHANGE_UNLOCK(ifp)       \
                   2212:        mutex_exit((ifp)->if_snd.ifq_lock)
                   2213:
1.325     roy      2214: /*
                   2215:  * Handle a change in the interface link state and
                   2216:  * queue notifications.
1.159     dyoung   2217:  */
                   2218: void
                   2219: if_link_state_change(struct ifnet *ifp, int link_state)
                   2220: {
1.406     ozaki-r  2221:        int idx;
1.263     roy      2222:
1.347     knakahar 2223:        KASSERTMSG(if_is_link_state_changeable(ifp),
1.346     knakahar 2224:            "%s: IFEF_NO_LINK_STATE_CHANGE must not be set, but if_extflags=0x%x",
                   2225:            ifp->if_xname, ifp->if_extflags);
                   2226:
1.325     roy      2227:        /* Ensure change is to a valid state */
                   2228:        switch (link_state) {
                   2229:        case LINK_STATE_UNKNOWN:        /* FALLTHROUGH */
                   2230:        case LINK_STATE_DOWN:           /* FALLTHROUGH */
                   2231:        case LINK_STATE_UP:
                   2232:                break;
                   2233:        default:
                   2234: #ifdef DEBUG
                   2235:                printf("%s: invalid link state %d\n",
                   2236:                    ifp->if_xname, link_state);
                   2237: #endif
1.185     dyoung   2238:                return;
1.264     roy      2239:        }
1.263     roy      2240:
1.406     ozaki-r  2241:        IF_LINK_STATE_CHANGE_LOCK(ifp);
1.325     roy      2242:
                   2243:        /* Find the last unset event in the queue. */
                   2244:        LQ_FIND_UNSET(ifp->if_link_queue, idx);
                   2245:
                   2246:        /*
                   2247:         * Ensure link_state doesn't match the last event in the queue.
                   2248:         * ifp->if_link_state is not checked and set here because
                   2249:         * that would present an inconsistent picture to the system.
                   2250:         */
                   2251:        if (idx != 0 &&
                   2252:            LQ_ITEM(ifp->if_link_queue, idx - 1) == (uint8_t)link_state)
                   2253:                goto out;
                   2254:
                   2255:        /* Handle queue overflow. */
                   2256:        if (idx == LQ_MAX(ifp->if_link_queue)) {
                   2257:                uint8_t lost;
                   2258:
                   2259:                /*
                   2260:                 * The DOWN state must be protected from being pushed off
                   2261:                 * the queue to ensure that userland will always be
                   2262:                 * in a sane state.
                   2263:                 * Because DOWN is protected, there is no need to protect
                   2264:                 * UNKNOWN.
                   2265:                 * It should be invalid to change from any other state to
                   2266:                 * UNKNOWN anyway ...
                   2267:                 */
                   2268:                lost = LQ_ITEM(ifp->if_link_queue, 0);
                   2269:                LQ_PUSH(ifp->if_link_queue, (uint8_t)link_state);
                   2270:                if (lost == LINK_STATE_DOWN) {
                   2271:                        lost = LQ_ITEM(ifp->if_link_queue, 0);
                   2272:                        LQ_STORE(ifp->if_link_queue, 0, LINK_STATE_DOWN);
                   2273:                }
                   2274:                printf("%s: lost link state change %s\n",
                   2275:                    ifp->if_xname,
                   2276:                    lost == LINK_STATE_UP ? "UP" :
                   2277:                    lost == LINK_STATE_DOWN ? "DOWN" :
                   2278:                    "UNKNOWN");
                   2279:        } else
                   2280:                LQ_STORE(ifp->if_link_queue, idx, (uint8_t)link_state);
                   2281:
1.324     ozaki-r  2282:        softint_schedule(ifp->if_link_si);
                   2283:
1.325     roy      2284: out:
1.406     ozaki-r  2285:        IF_LINK_STATE_CHANGE_UNLOCK(ifp);
1.324     ozaki-r  2286: }
                   2287:
1.325     roy      2288: /*
                   2289:  * Handle interface link state change notifications.
                   2290:  */
1.393     ozaki-r  2291: void
                   2292: if_link_state_change_softint(struct ifnet *ifp, int link_state)
1.324     ozaki-r  2293: {
                   2294:        struct domain *dp;
1.393     ozaki-r  2295:        int s = splnet();
1.406     ozaki-r  2296:        bool notify;
1.393     ozaki-r  2297:
                   2298:        KASSERT(!cpu_intr_p());
1.324     ozaki-r  2299:
1.406     ozaki-r  2300:        IF_LINK_STATE_CHANGE_LOCK(ifp);
                   2301:
1.325     roy      2302:        /* Ensure the change is still valid. */
1.393     ozaki-r  2303:        if (ifp->if_link_state == link_state) {
1.406     ozaki-r  2304:                IF_LINK_STATE_CHANGE_UNLOCK(ifp);
1.325     roy      2305:                return;
1.393     ozaki-r  2306:        }
1.324     ozaki-r  2307:
1.263     roy      2308: #ifdef DEBUG
                   2309:        log(LOG_DEBUG, "%s: link state %s (was %s)\n", ifp->if_xname,
                   2310:                link_state == LINK_STATE_UP ? "UP" :
                   2311:                link_state == LINK_STATE_DOWN ? "DOWN" :
                   2312:                "UNKNOWN",
1.325     roy      2313:                ifp->if_link_state == LINK_STATE_UP ? "UP" :
                   2314:                ifp->if_link_state == LINK_STATE_DOWN ? "DOWN" :
1.263     roy      2315:                "UNKNOWN");
                   2316: #endif
                   2317:
                   2318:        /*
                   2319:         * When going from UNKNOWN to UP, we need to mark existing
1.314     roy      2320:         * addresses as tentative and restart DAD as we may have
1.263     roy      2321:         * erroneously not found a duplicate.
                   2322:         *
                   2323:         * This needs to happen before rt_ifmsg to avoid a race where
                   2324:         * listeners would have an address and expect it to work right
                   2325:         * away.
                   2326:         */
1.406     ozaki-r  2327:        notify = (link_state == LINK_STATE_UP &&
                   2328:            ifp->if_link_state == LINK_STATE_UNKNOWN);
                   2329:        ifp->if_link_state = link_state;
                   2330:        /* The following routines may sleep so release the spin mutex */
                   2331:        IF_LINK_STATE_CHANGE_UNLOCK(ifp);
                   2332:
                   2333:        KERNEL_LOCK_UNLESS_NET_MPSAFE();
                   2334:        if (notify) {
1.312     roy      2335:                DOMAIN_FOREACH(dp) {
                   2336:                        if (dp->dom_if_link_state_change != NULL)
                   2337:                                dp->dom_if_link_state_change(ifp,
                   2338:                                    LINK_STATE_DOWN);
                   2339:                }
                   2340:        }
1.263     roy      2341:
1.159     dyoung   2342:        /* Notify that the link state has changed. */
1.185     dyoung   2343:        rt_ifmsg(ifp);
1.263     roy      2344:
1.166     liamjfoy 2345: #if NCARP > 0
1.185     dyoung   2346:        if (ifp->if_carp)
                   2347:                carp_carpdev_state(ifp);
1.166     liamjfoy 2348: #endif
1.263     roy      2349:
1.312     roy      2350:        DOMAIN_FOREACH(dp) {
                   2351:                if (dp->dom_if_link_state_change != NULL)
                   2352:                        dp->dom_if_link_state_change(ifp, link_state);
1.270     pooka    2353:        }
1.406     ozaki-r  2354:        KERNEL_UNLOCK_UNLESS_NET_MPSAFE();
1.393     ozaki-r  2355:        splx(s);
1.325     roy      2356: }
                   2357:
                   2358: /*
                   2359:  * Process the interface link state change queue.
                   2360:  */
                   2361: static void
                   2362: if_link_state_change_si(void *arg)
                   2363: {
                   2364:        struct ifnet *ifp = arg;
                   2365:        int s;
                   2366:        uint8_t state;
1.406     ozaki-r  2367:        bool schedule;
1.325     roy      2368:
1.397     ozaki-r  2369:        SOFTNET_KERNEL_LOCK_UNLESS_NET_MPSAFE();
1.325     roy      2370:        s = splnet();
                   2371:
                   2372:        /* Pop a link state change from the queue and process it. */
1.406     ozaki-r  2373:        IF_LINK_STATE_CHANGE_LOCK(ifp);
1.325     roy      2374:        LQ_POP(ifp->if_link_queue, state);
1.406     ozaki-r  2375:        IF_LINK_STATE_CHANGE_UNLOCK(ifp);
                   2376:
1.393     ozaki-r  2377:        if_link_state_change_softint(ifp, state);
1.325     roy      2378:
                   2379:        /* If there is a link state change to come, schedule it. */
1.406     ozaki-r  2380:        IF_LINK_STATE_CHANGE_LOCK(ifp);
                   2381:        schedule = (LQ_ITEM(ifp->if_link_queue, 0) != LINK_STATE_UNSET);
                   2382:        IF_LINK_STATE_CHANGE_UNLOCK(ifp);
                   2383:        if (schedule)
1.325     roy      2384:                softint_schedule(ifp->if_link_si);
1.264     roy      2385:
                   2386:        splx(s);
1.397     ozaki-r  2387:        SOFTNET_KERNEL_UNLOCK_UNLESS_NET_MPSAFE();
1.159     dyoung   2388: }
                   2389:
                   2390: /*
1.310     roy      2391:  * Default action when installing a local route on a point-to-point
                   2392:  * interface.
                   2393:  */
                   2394: void
                   2395: p2p_rtrequest(int req, struct rtentry *rt,
                   2396:     __unused const struct rt_addrinfo *info)
                   2397: {
                   2398:        struct ifnet *ifp = rt->rt_ifp;
                   2399:        struct ifaddr *ifa, *lo0ifa;
1.357     ozaki-r  2400:        int s = pserialize_read_enter();
1.310     roy      2401:
                   2402:        switch (req) {
                   2403:        case RTM_ADD:
                   2404:                if ((rt->rt_flags & RTF_LOCAL) == 0)
                   2405:                        break;
                   2406:
1.351     ozaki-r  2407:                rt->rt_ifp = lo0ifp;
                   2408:
1.354     ozaki-r  2409:                IFADDR_READER_FOREACH(ifa, ifp) {
1.310     roy      2410:                        if (equal(rt_getkey(rt), ifa->ifa_addr))
                   2411:                                break;
                   2412:                }
                   2413:                if (ifa == NULL)
                   2414:                        break;
                   2415:
                   2416:                /*
                   2417:                 * Ensure lo0 has an address of the same family.
                   2418:                 */
1.354     ozaki-r  2419:                IFADDR_READER_FOREACH(lo0ifa, lo0ifp) {
1.310     roy      2420:                        if (lo0ifa->ifa_addr->sa_family ==
                   2421:                            ifa->ifa_addr->sa_family)
                   2422:                                break;
                   2423:                }
                   2424:                if (lo0ifa == NULL)
                   2425:                        break;
                   2426:
                   2427:                /*
                   2428:                 * Make sure to set rt->rt_ifa to the interface
                   2429:                 * address we are using, otherwise we will have trouble
                   2430:                 * with source address selection.
                   2431:                 */
                   2432:                if (ifa != rt->rt_ifa)
                   2433:                        rt_replace_ifa(rt, ifa);
                   2434:                break;
                   2435:        case RTM_DELETE:
                   2436:        default:
                   2437:                break;
                   2438:        }
1.357     ozaki-r  2439:        pserialize_read_exit(s);
1.310     roy      2440: }
                   2441:
1.403     ozaki-r  2442: static void
                   2443: _if_down(struct ifnet *ifp)
1.1       cgd      2444: {
1.61      augustss 2445:        struct ifaddr *ifa;
1.312     roy      2446:        struct domain *dp;
1.357     ozaki-r  2447:        int s, bound;
                   2448:        struct psref psref;
1.1       cgd      2449:
                   2450:        ifp->if_flags &= ~IFF_UP;
1.232     christos 2451:        nanotime(&ifp->if_lastchange);
1.357     ozaki-r  2452:
                   2453:        bound = curlwp_bind();
                   2454:        s = pserialize_read_enter();
                   2455:        IFADDR_READER_FOREACH(ifa, ifp) {
                   2456:                ifa_acquire(ifa, &psref);
                   2457:                pserialize_read_exit(s);
                   2458:
1.1       cgd      2459:                pfctlinput(PRC_IFDOWN, ifa->ifa_addr);
1.357     ozaki-r  2460:
                   2461:                s = pserialize_read_enter();
                   2462:                ifa_release(ifa, &psref);
                   2463:        }
                   2464:        pserialize_read_exit(s);
                   2465:        curlwp_bindx(bound);
                   2466:
1.78      thorpej  2467:        IFQ_PURGE(&ifp->if_snd);
1.166     liamjfoy 2468: #if NCARP > 0
                   2469:        if (ifp->if_carp)
                   2470:                carp_carpdev_state(ifp);
                   2471: #endif
1.15      mycroft  2472:        rt_ifmsg(ifp);
1.312     roy      2473:        DOMAIN_FOREACH(dp) {
                   2474:                if (dp->dom_if_down)
                   2475:                        dp->dom_if_down(ifp);
                   2476:        }
1.15      mycroft  2477: }
                   2478:
1.403     ozaki-r  2479: static void
                   2480: if_down_deactivated(struct ifnet *ifp)
                   2481: {
                   2482:
                   2483:        KASSERT(if_is_deactivated(ifp));
                   2484:        _if_down(ifp);
                   2485: }
                   2486:
                   2487: void
                   2488: if_down_locked(struct ifnet *ifp)
                   2489: {
                   2490:
                   2491:        KASSERT(mutex_owned(ifp->if_ioctl_lock));
                   2492:        _if_down(ifp);
                   2493: }
                   2494:
1.15      mycroft  2495: /*
1.403     ozaki-r  2496:  * Mark an interface down and notify protocols of
1.15      mycroft  2497:  * the transition.
1.23      mycroft  2498:  * NOTE: must be called at splsoftnet or equivalent.
1.15      mycroft  2499:  */
                   2500: void
1.403     ozaki-r  2501: if_down(struct ifnet *ifp)
                   2502: {
                   2503:
                   2504:        mutex_enter(ifp->if_ioctl_lock);
                   2505:        if_down_locked(ifp);
                   2506:        mutex_exit(ifp->if_ioctl_lock);
                   2507: }
                   2508:
                   2509: /*
                   2510:  * Must be called with holding if_ioctl_lock.
                   2511:  */
                   2512: static void
                   2513: if_up_locked(struct ifnet *ifp)
1.15      mycroft  2514: {
1.24      christos 2515: #ifdef notyet
1.61      augustss 2516:        struct ifaddr *ifa;
1.24      christos 2517: #endif
1.312     roy      2518:        struct domain *dp;
1.15      mycroft  2519:
1.403     ozaki-r  2520:        KASSERT(mutex_owned(ifp->if_ioctl_lock));
                   2521:
                   2522:        KASSERT(!if_is_deactivated(ifp));
1.15      mycroft  2523:        ifp->if_flags |= IFF_UP;
1.232     christos 2524:        nanotime(&ifp->if_lastchange);
1.15      mycroft  2525: #ifdef notyet
                   2526:        /* this has no effect on IP, and will kill all ISO connections XXX */
1.354     ozaki-r  2527:        IFADDR_READER_FOREACH(ifa, ifp)
1.15      mycroft  2528:                pfctlinput(PRC_IFUP, ifa->ifa_addr);
                   2529: #endif
1.166     liamjfoy 2530: #if NCARP > 0
                   2531:        if (ifp->if_carp)
                   2532:                carp_carpdev_state(ifp);
                   2533: #endif
1.15      mycroft  2534:        rt_ifmsg(ifp);
1.312     roy      2535:        DOMAIN_FOREACH(dp) {
1.313     roy      2536:                if (dp->dom_if_up)
                   2537:                        dp->dom_if_up(ifp);
1.312     roy      2538:        }
1.1       cgd      2539: }
                   2540:
                   2541: /*
1.405     ozaki-r  2542:  * XXX reusing (ifp)->if_snd->ifq_lock rather than having another spin mutex
                   2543:  * for each ifnet.  It doesn't matter because:
                   2544:  * - if IFEF_MPSAFE is enabled, if_snd isn't used and lock contention on
                   2545:  *   ifq_lock don't happen
                   2546:  * - if IFEF_MPSAFE is disabled, there is no lock contention on ifq_lock
                   2547:  *   because if_snd and if_watchdog_reset is used with KERNEL_LOCK on packet
                   2548:  *   transmissions and if_slowtimo is also called with KERNEL_LOCK
                   2549:  */
                   2550: #define IF_WATCHDOG_LOCK(ifp)  mutex_enter((ifp)->if_snd.ifq_lock)
                   2551: #define IF_WATCHDOG_UNLOCK(ifp)        mutex_exit((ifp)->if_snd.ifq_lock)
                   2552:
                   2553: /*
1.296     ozaki-r  2554:  * Handle interface slowtimo timer routine.  Called
                   2555:  * from softclock, we decrement timer (if set) and
1.1       cgd      2556:  * call the appropriate interface routine on expiration.
                   2557:  */
1.294     ozaki-r  2558: static void
1.177     christos 2559: if_slowtimo(void *arg)
1.1       cgd      2560: {
1.306     martin   2561:        void (*slowtimo)(struct ifnet *);
1.296     ozaki-r  2562:        struct ifnet *ifp = arg;
1.305     martin   2563:        int s;
1.405     ozaki-r  2564:        bool fire;
1.1       cgd      2565:
1.306     martin   2566:        slowtimo = ifp->if_slowtimo;
                   2567:        if (__predict_false(slowtimo == NULL))
1.305     martin   2568:                return;
1.296     ozaki-r  2569:
1.374     ozaki-r  2570:        s = splnet();
1.405     ozaki-r  2571:        IF_WATCHDOG_LOCK(ifp);
                   2572:        fire = (ifp->if_timer != 0 && --ifp->if_timer == 0);
                   2573:        IF_WATCHDOG_UNLOCK(ifp);
                   2574:        if (fire)
1.306     martin   2575:                (*slowtimo)(ifp);
1.1       cgd      2576:        splx(s);
1.305     martin   2577:
                   2578:        if (__predict_true(ifp->if_slowtimo != NULL))
                   2579:                callout_schedule(ifp->if_slowtimo_ch, hz / IFNET_SLOWHZ);
1.65      thorpej  2580: }
                   2581:
1.405     ozaki-r  2582: void
                   2583: if_watchdog_reset(struct ifnet *ifp, short v)
                   2584: {
                   2585:
                   2586:        IF_WATCHDOG_LOCK(ifp);
                   2587:        ifp->if_timer = v;
                   2588:        IF_WATCHDOG_UNLOCK(ifp);
                   2589: }
                   2590:
                   2591: void
                   2592: if_watchdog_stop(struct ifnet *ifp)
                   2593: {
                   2594:
                   2595:        if_watchdog_reset(ifp, 0);
                   2596: }
                   2597:
1.65      thorpej  2598: /*
1.403     ozaki-r  2599:  * Mark an interface up and notify protocols of
                   2600:  * the transition.
                   2601:  * NOTE: must be called at splsoftnet or equivalent.
                   2602:  */
                   2603: void
                   2604: if_up(struct ifnet *ifp)
                   2605: {
                   2606:
                   2607:        mutex_enter(ifp->if_ioctl_lock);
                   2608:        if_up_locked(ifp);
                   2609:        mutex_exit(ifp->if_ioctl_lock);
                   2610: }
                   2611:
                   2612: /*
1.65      thorpej  2613:  * Set/clear promiscuous mode on interface ifp based on the truth value
                   2614:  * of pswitch.  The calls are reference counted so that only the first
                   2615:  * "on" request actually has an effect, as does the final "off" request.
                   2616:  * Results are undefined if the "off" and "on" requests are not matched.
                   2617:  */
                   2618: int
1.402     ozaki-r  2619: ifpromisc_locked(struct ifnet *ifp, int pswitch)
1.65      thorpej  2620: {
1.401     ozaki-r  2621:        int pcount, ret = 0;
1.259     dyoung   2622:        short nflags;
1.65      thorpej  2623:
1.402     ozaki-r  2624:        KASSERT(mutex_owned(ifp->if_ioctl_lock));
1.401     ozaki-r  2625:
1.65      thorpej  2626:        pcount = ifp->if_pcount;
                   2627:        if (pswitch) {
                   2628:                /*
1.71      thorpej  2629:                 * Allow the device to be "placed" into promiscuous
                   2630:                 * mode even if it is not configured up.  It will
1.242     mbalmer  2631:                 * consult IFF_PROMISC when it is brought up.
1.65      thorpej  2632:                 */
1.68      pk       2633:                if (ifp->if_pcount++ != 0)
1.401     ozaki-r  2634:                        goto out;
1.252     dyoung   2635:                nflags = ifp->if_flags | IFF_PROMISC;
1.65      thorpej  2636:        } else {
                   2637:                if (--ifp->if_pcount > 0)
1.401     ozaki-r  2638:                        goto out;
1.252     dyoung   2639:                nflags = ifp->if_flags & ~IFF_PROMISC;
1.65      thorpej  2640:        }
1.252     dyoung   2641:        ret = if_flags_set(ifp, nflags);
1.65      thorpej  2642:        /* Restore interface state if not successful. */
                   2643:        if (ret != 0) {
                   2644:                ifp->if_pcount = pcount;
                   2645:        }
1.401     ozaki-r  2646: out:
1.402     ozaki-r  2647:        return ret;
                   2648: }
                   2649:
                   2650: int
                   2651: ifpromisc(struct ifnet *ifp, int pswitch)
                   2652: {
                   2653:        int e;
                   2654:
                   2655:        mutex_enter(ifp->if_ioctl_lock);
                   2656:        e = ifpromisc_locked(ifp, pswitch);
1.401     ozaki-r  2657:        mutex_exit(ifp->if_ioctl_lock);
1.402     ozaki-r  2658:
                   2659:        return e;
1.1       cgd      2660: }
                   2661:
                   2662: /*
                   2663:  * Map interface name to
                   2664:  * interface structure pointer.
                   2665:  */
                   2666: struct ifnet *
1.163     thorpej  2667: ifunit(const char *name)
1.1       cgd      2668: {
1.61      augustss 2669:        struct ifnet *ifp;
1.105     matt     2670:        const char *cp = name;
                   2671:        u_int unit = 0;
                   2672:        u_int i;
1.334     ozaki-r  2673:        int s;
1.105     matt     2674:
                   2675:        /*
                   2676:         * If the entire name is a number, treat it as an ifindex.
                   2677:         */
                   2678:        for (i = 0; i < IFNAMSIZ && *cp >= '0' && *cp <= '9'; i++, cp++) {
                   2679:                unit = unit * 10 + (*cp - '0');
                   2680:        }
                   2681:
                   2682:        /*
                   2683:         * If the number took all of the name, then it's a valid ifindex.
                   2684:         */
1.387     ozaki-r  2685:        if (i == IFNAMSIZ || (cp != name && *cp == '\0'))
                   2686:                return if_byindex(unit);
1.34      thorpej  2687:
1.334     ozaki-r  2688:        ifp = NULL;
                   2689:        s = pserialize_read_enter();
                   2690:        IFNET_READER_FOREACH(ifp) {
1.348     ozaki-r  2691:                if (if_is_deactivated(ifp))
1.53      thorpej  2692:                        continue;
                   2693:                if (strcmp(ifp->if_xname, name) == 0)
1.334     ozaki-r  2694:                        goto out;
1.53      thorpej  2695:        }
1.334     ozaki-r  2696: out:
                   2697:        pserialize_read_exit(s);
                   2698:        return ifp;
1.1       cgd      2699: }
1.49      itojun   2700:
1.335     ozaki-r  2701: /*
                   2702:  * Get a reference of an ifnet object by an interface name.
                   2703:  * The returned reference is protected by psref(9). The caller
                   2704:  * must release a returned reference by if_put after use.
                   2705:  */
                   2706: struct ifnet *
                   2707: if_get(const char *name, struct psref *psref)
                   2708: {
                   2709:        struct ifnet *ifp;
                   2710:        const char *cp = name;
                   2711:        u_int unit = 0;
                   2712:        u_int i;
                   2713:        int s;
                   2714:
                   2715:        /*
                   2716:         * If the entire name is a number, treat it as an ifindex.
                   2717:         */
                   2718:        for (i = 0; i < IFNAMSIZ && *cp >= '0' && *cp <= '9'; i++, cp++) {
                   2719:                unit = unit * 10 + (*cp - '0');
                   2720:        }
                   2721:
                   2722:        /*
                   2723:         * If the number took all of the name, then it's a valid ifindex.
                   2724:         */
1.387     ozaki-r  2725:        if (i == IFNAMSIZ || (cp != name && *cp == '\0'))
                   2726:                return if_get_byindex(unit, psref);
1.335     ozaki-r  2727:
                   2728:        ifp = NULL;
                   2729:        s = pserialize_read_enter();
                   2730:        IFNET_READER_FOREACH(ifp) {
1.348     ozaki-r  2731:                if (if_is_deactivated(ifp))
1.335     ozaki-r  2732:                        continue;
                   2733:                if (strcmp(ifp->if_xname, name) == 0) {
                   2734:                        psref_acquire(psref, &ifp->if_psref,
                   2735:                            ifnet_psref_class);
                   2736:                        goto out;
                   2737:                }
                   2738:        }
                   2739: out:
                   2740:        pserialize_read_exit(s);
                   2741:        return ifp;
                   2742: }
                   2743:
                   2744: /*
1.395     roy      2745:  * Release a reference of an ifnet object given by if_get, if_get_byindex
                   2746:  * or if_get_bylla.
1.335     ozaki-r  2747:  */
                   2748: void
                   2749: if_put(const struct ifnet *ifp, struct psref *psref)
                   2750: {
                   2751:
1.344     ozaki-r  2752:        if (ifp == NULL)
                   2753:                return;
                   2754:
1.335     ozaki-r  2755:        psref_release(psref, &ifp->if_psref, ifnet_psref_class);
                   2756: }
                   2757:
1.250     rmind    2758: ifnet_t *
                   2759: if_byindex(u_int idx)
                   2760: {
1.357     ozaki-r  2761:        ifnet_t *ifp;
                   2762:
1.387     ozaki-r  2763:        ifp = (__predict_true(idx < if_indexlim)) ? ifindex2ifnet[idx] : NULL;
1.357     ozaki-r  2764:        if (ifp != NULL && if_is_deactivated(ifp))
                   2765:                ifp = NULL;
                   2766:        return ifp;
1.250     rmind    2767: }
                   2768:
1.335     ozaki-r  2769: /*
                   2770:  * Get a reference of an ifnet object by an interface index.
                   2771:  * The returned reference is protected by psref(9). The caller
                   2772:  * must release a returned reference by if_put after use.
                   2773:  */
                   2774: ifnet_t *
                   2775: if_get_byindex(u_int idx, struct psref *psref)
                   2776: {
                   2777:        ifnet_t *ifp;
                   2778:        int s;
                   2779:
                   2780:        s = pserialize_read_enter();
1.387     ozaki-r  2781:        ifp = if_byindex(idx);
1.337     ozaki-r  2782:        if (__predict_true(ifp != NULL))
1.335     ozaki-r  2783:                psref_acquire(psref, &ifp->if_psref, ifnet_psref_class);
                   2784:        pserialize_read_exit(s);
                   2785:
                   2786:        return ifp;
                   2787: }
                   2788:
1.395     roy      2789: ifnet_t *
                   2790: if_get_bylla(const void *lla, unsigned char lla_len, struct psref *psref)
                   2791: {
                   2792:        ifnet_t *ifp;
                   2793:        int s;
                   2794:
                   2795:        s = pserialize_read_enter();
                   2796:        IFNET_READER_FOREACH(ifp) {
                   2797:                if (if_is_deactivated(ifp))
                   2798:                        continue;
                   2799:                if (ifp->if_addrlen != lla_len)
                   2800:                        continue;
                   2801:                if (memcmp(lla, CLLADDR(ifp->if_sadl), lla_len) == 0) {
                   2802:                        psref_acquire(psref, &ifp->if_psref,
                   2803:                            ifnet_psref_class);
                   2804:                        break;
                   2805:                }
                   2806:        }
                   2807:        pserialize_read_exit(s);
                   2808:
                   2809:        return ifp;
                   2810: }
                   2811:
1.338     ozaki-r  2812: /*
1.380     ozaki-r  2813:  * Note that it's safe only if the passed ifp is guaranteed to not be freed,
                   2814:  * for example using pserialize or the ifp is already held or some other
                   2815:  * object is held which guarantes the ifp to not be freed indirectly.
1.338     ozaki-r  2816:  */
                   2817: void
1.380     ozaki-r  2818: if_acquire(struct ifnet *ifp, struct psref *psref)
1.338     ozaki-r  2819: {
                   2820:
                   2821:        KASSERT(ifp->if_index != 0);
                   2822:        psref_acquire(psref, &ifp->if_psref, ifnet_psref_class);
                   2823: }
                   2824:
                   2825: bool
                   2826: if_held(struct ifnet *ifp)
                   2827: {
                   2828:
                   2829:        return psref_held(&ifp->if_psref, ifnet_psref_class);
                   2830: }
                   2831:
1.404     knakahar 2832: /*
                   2833:  * Some tunnel interfaces can nest, e.g. IPv4 over IPv4 gif(4) tunnel over IPv4.
                   2834:  * Check the tunnel nesting count.
                   2835:  * Return > 0, if tunnel nesting count is more than limit.
                   2836:  * Return 0, if tunnel nesting count is equal or less than limit.
                   2837:  */
                   2838: int
                   2839: if_tunnel_check_nesting(struct ifnet *ifp, struct mbuf *m, int limit)
                   2840: {
                   2841:        struct m_tag *mtag;
                   2842:        int *count;
                   2843:
                   2844:        mtag = m_tag_find(m, PACKET_TAG_TUNNEL_INFO, NULL);
                   2845:        if (mtag != NULL) {
                   2846:                count = (int *)(mtag + 1);
                   2847:                if (++(*count) > limit) {
                   2848:                        log(LOG_NOTICE,
                   2849:                            "%s: recursively called too many times(%d)\n",
                   2850:                            ifp->if_xname, *count);
                   2851:                        return EIO;
                   2852:                }
                   2853:        } else {
                   2854:                mtag = m_tag_get(PACKET_TAG_TUNNEL_INFO, sizeof(*count),
                   2855:                    M_NOWAIT);
                   2856:                if (mtag != NULL) {
                   2857:                        m_tag_prepend(m, mtag);
                   2858:                        count = (int *)(mtag + 1);
                   2859:                        *count = 0;
                   2860:                } else {
                   2861:                        log(LOG_DEBUG,
                   2862:                            "%s: m_tag_get() failed, recursion calls are not prevented.\n",
                   2863:                            ifp->if_xname);
                   2864:                }
                   2865:        }
                   2866:
                   2867:        return 0;
                   2868: }
1.338     ozaki-r  2869:
1.211     dyoung   2870: /* common */
1.215     dyoung   2871: int
                   2872: ifioctl_common(struct ifnet *ifp, u_long cmd, void *data)
1.211     dyoung   2873: {
1.224     dyoung   2874:        int s;
1.215     dyoung   2875:        struct ifreq *ifr;
                   2876:        struct ifcapreq *ifcr;
                   2877:        struct ifdatareq *ifdr;
1.211     dyoung   2878:
                   2879:        switch (cmd) {
                   2880:        case SIOCSIFCAP:
1.215     dyoung   2881:                ifcr = data;
1.211     dyoung   2882:                if ((ifcr->ifcr_capenable & ~ifp->if_capabilities) != 0)
                   2883:                        return EINVAL;
                   2884:
1.213     dyoung   2885:                if (ifcr->ifcr_capenable == ifp->if_capenable)
                   2886:                        return 0;
1.211     dyoung   2887:
1.213     dyoung   2888:                ifp->if_capenable = ifcr->ifcr_capenable;
1.211     dyoung   2889:
1.213     dyoung   2890:                /* Pre-compute the checksum flags mask. */
                   2891:                ifp->if_csum_flags_tx = 0;
                   2892:                ifp->if_csum_flags_rx = 0;
                   2893:                if (ifp->if_capenable & IFCAP_CSUM_IPv4_Tx) {
                   2894:                        ifp->if_csum_flags_tx |= M_CSUM_IPv4;
                   2895:                }
                   2896:                if (ifp->if_capenable & IFCAP_CSUM_IPv4_Rx) {
                   2897:                        ifp->if_csum_flags_rx |= M_CSUM_IPv4;
                   2898:                }
1.211     dyoung   2899:
1.213     dyoung   2900:                if (ifp->if_capenable & IFCAP_CSUM_TCPv4_Tx) {
                   2901:                        ifp->if_csum_flags_tx |= M_CSUM_TCPv4;
                   2902:                }
                   2903:                if (ifp->if_capenable & IFCAP_CSUM_TCPv4_Rx) {
                   2904:                        ifp->if_csum_flags_rx |= M_CSUM_TCPv4;
                   2905:                }
1.211     dyoung   2906:
1.213     dyoung   2907:                if (ifp->if_capenable & IFCAP_CSUM_UDPv4_Tx) {
                   2908:                        ifp->if_csum_flags_tx |= M_CSUM_UDPv4;
                   2909:                }
                   2910:                if (ifp->if_capenable & IFCAP_CSUM_UDPv4_Rx) {
                   2911:                        ifp->if_csum_flags_rx |= M_CSUM_UDPv4;
                   2912:                }
1.211     dyoung   2913:
1.213     dyoung   2914:                if (ifp->if_capenable & IFCAP_CSUM_TCPv6_Tx) {
                   2915:                        ifp->if_csum_flags_tx |= M_CSUM_TCPv6;
                   2916:                }
                   2917:                if (ifp->if_capenable & IFCAP_CSUM_TCPv6_Rx) {
                   2918:                        ifp->if_csum_flags_rx |= M_CSUM_TCPv6;
                   2919:                }
1.211     dyoung   2920:
1.213     dyoung   2921:                if (ifp->if_capenable & IFCAP_CSUM_UDPv6_Tx) {
                   2922:                        ifp->if_csum_flags_tx |= M_CSUM_UDPv6;
1.211     dyoung   2923:                }
1.213     dyoung   2924:                if (ifp->if_capenable & IFCAP_CSUM_UDPv6_Rx) {
                   2925:                        ifp->if_csum_flags_rx |= M_CSUM_UDPv6;
                   2926:                }
1.215     dyoung   2927:                if (ifp->if_flags & IFF_UP)
                   2928:                        return ENETRESET;
                   2929:                return 0;
1.211     dyoung   2930:        case SIOCSIFFLAGS:
1.215     dyoung   2931:                ifr = data;
1.399     ozaki-r  2932:                /*
                   2933:                 * If if_is_mpsafe(ifp), KERNEL_LOCK isn't held here, but if_up
                   2934:                 * and if_down aren't MP-safe yet, so we must hold the lock.
                   2935:                 */
                   2936:                KERNEL_LOCK_IF_IFP_MPSAFE(ifp);
1.211     dyoung   2937:                if (ifp->if_flags & IFF_UP && (ifr->ifr_flags & IFF_UP) == 0) {
1.373     ozaki-r  2938:                        s = splsoftnet();
1.403     ozaki-r  2939:                        if_down_locked(ifp);
1.211     dyoung   2940:                        splx(s);
                   2941:                }
                   2942:                if (ifr->ifr_flags & IFF_UP && (ifp->if_flags & IFF_UP) == 0) {
1.373     ozaki-r  2943:                        s = splsoftnet();
1.403     ozaki-r  2944:                        if_up_locked(ifp);
1.211     dyoung   2945:                        splx(s);
                   2946:                }
1.399     ozaki-r  2947:                KERNEL_UNLOCK_IF_IFP_MPSAFE(ifp);
1.211     dyoung   2948:                ifp->if_flags = (ifp->if_flags & IFF_CANTCHANGE) |
                   2949:                        (ifr->ifr_flags &~ IFF_CANTCHANGE);
                   2950:                break;
                   2951:        case SIOCGIFFLAGS:
1.215     dyoung   2952:                ifr = data;
1.211     dyoung   2953:                ifr->ifr_flags = ifp->if_flags;
                   2954:                break;
                   2955:
                   2956:        case SIOCGIFMETRIC:
1.215     dyoung   2957:                ifr = data;
1.211     dyoung   2958:                ifr->ifr_metric = ifp->if_metric;
                   2959:                break;
                   2960:
                   2961:        case SIOCGIFMTU:
1.215     dyoung   2962:                ifr = data;
1.211     dyoung   2963:                ifr->ifr_mtu = ifp->if_mtu;
                   2964:                break;
                   2965:
                   2966:        case SIOCGIFDLT:
1.215     dyoung   2967:                ifr = data;
1.211     dyoung   2968:                ifr->ifr_dlt = ifp->if_dlt;
                   2969:                break;
                   2970:
                   2971:        case SIOCGIFCAP:
1.215     dyoung   2972:                ifcr = data;
1.211     dyoung   2973:                ifcr->ifcr_capabilities = ifp->if_capabilities;
                   2974:                ifcr->ifcr_capenable = ifp->if_capenable;
                   2975:                break;
                   2976:
                   2977:        case SIOCSIFMETRIC:
1.215     dyoung   2978:                ifr = data;
1.211     dyoung   2979:                ifp->if_metric = ifr->ifr_metric;
                   2980:                break;
                   2981:
                   2982:        case SIOCGIFDATA:
1.215     dyoung   2983:                ifdr = data;
1.211     dyoung   2984:                ifdr->ifdr_data = ifp->if_data;
                   2985:                break;
                   2986:
1.266     christos 2987:        case SIOCGIFINDEX:
                   2988:                ifr = data;
                   2989:                ifr->ifr_index = ifp->if_index;
                   2990:                break;
                   2991:
1.211     dyoung   2992:        case SIOCZIFDATA:
1.215     dyoung   2993:                ifdr = data;
1.211     dyoung   2994:                ifdr->ifdr_data = ifp->if_data;
                   2995:                /*
                   2996:                 * Assumes that the volatile counters that can be
                   2997:                 * zero'ed are at the end of if_data.
                   2998:                 */
                   2999:                memset(&ifp->if_data.ifi_ipackets, 0, sizeof(ifp->if_data) -
                   3000:                    offsetof(struct if_data, ifi_ipackets));
1.261     msaitoh  3001:                /*
                   3002:                 * The memset() clears to the bottm of if_data. In the area,
                   3003:                 * if_lastchange is included. Please be careful if new entry
                   3004:                 * will be added into if_data or rewite this.
                   3005:                 *
                   3006:                 * And also, update if_lastchnage.
                   3007:                 */
                   3008:                getnanotime(&ifp->if_lastchange);
1.211     dyoung   3009:                break;
1.215     dyoung   3010:        case SIOCSIFMTU:
                   3011:                ifr = data;
                   3012:                if (ifp->if_mtu == ifr->ifr_mtu)
                   3013:                        break;
                   3014:                ifp->if_mtu = ifr->ifr_mtu;
                   3015:                /*
                   3016:                 * If the link MTU changed, do network layer specific procedure.
                   3017:                 */
                   3018: #ifdef INET6
1.399     ozaki-r  3019:                KERNEL_LOCK_UNLESS_NET_MPSAFE();
1.271     pooka    3020:                if (in6_present)
                   3021:                        nd6_setmtu(ifp);
1.399     ozaki-r  3022:                KERNEL_UNLOCK_UNLESS_NET_MPSAFE();
1.215     dyoung   3023: #endif
                   3024:                return ENETRESET;
1.211     dyoung   3025:        default:
1.223     dyoung   3026:                return ENOTTY;
1.211     dyoung   3027:        }
                   3028:        return 0;
                   3029: }
                   3030:
1.235     dyoung   3031: int
1.284     rtr      3032: ifaddrpref_ioctl(struct socket *so, u_long cmd, void *data, struct ifnet *ifp)
1.235     dyoung   3033: {
                   3034:        struct if_addrprefreq *ifap = (struct if_addrprefreq *)data;
                   3035:        struct ifaddr *ifa;
                   3036:        const struct sockaddr *any, *sa;
                   3037:        union {
                   3038:                struct sockaddr sa;
                   3039:                struct sockaddr_storage ss;
1.236     jakllsch 3040:        } u, v;
1.357     ozaki-r  3041:        int s, error = 0;
1.235     dyoung   3042:
                   3043:        switch (cmd) {
                   3044:        case SIOCSIFADDRPREF:
1.284     rtr      3045:                if (kauth_authorize_network(curlwp->l_cred, KAUTH_NETWORK_INTERFACE,
1.235     dyoung   3046:                    KAUTH_REQ_NETWORK_INTERFACE_SETPRIV, ifp, (void *)cmd,
                   3047:                    NULL) != 0)
                   3048:                        return EPERM;
                   3049:        case SIOCGIFADDRPREF:
                   3050:                break;
                   3051:        default:
                   3052:                return EOPNOTSUPP;
                   3053:        }
                   3054:
                   3055:        /* sanity checks */
                   3056:        if (data == NULL || ifp == NULL) {
                   3057:                panic("invalid argument to %s", __func__);
                   3058:                /*NOTREACHED*/
                   3059:        }
                   3060:
                   3061:        /* address must be specified on ADD and DELETE */
                   3062:        sa = sstocsa(&ifap->ifap_addr);
                   3063:        if (sa->sa_family != sofamily(so))
                   3064:                return EINVAL;
                   3065:        if ((any = sockaddr_any(sa)) == NULL || sa->sa_len != any->sa_len)
                   3066:                return EINVAL;
                   3067:
1.236     jakllsch 3068:        sockaddr_externalize(&v.sa, sizeof(v.ss), sa);
                   3069:
1.357     ozaki-r  3070:        s = pserialize_read_enter();
1.354     ozaki-r  3071:        IFADDR_READER_FOREACH(ifa, ifp) {
1.235     dyoung   3072:                if (ifa->ifa_addr->sa_family != sa->sa_family)
                   3073:                        continue;
                   3074:                sockaddr_externalize(&u.sa, sizeof(u.ss), ifa->ifa_addr);
1.236     jakllsch 3075:                if (sockaddr_cmp(&u.sa, &v.sa) == 0)
1.235     dyoung   3076:                        break;
                   3077:        }
1.357     ozaki-r  3078:        if (ifa == NULL) {
                   3079:                error = EADDRNOTAVAIL;
                   3080:                goto out;
                   3081:        }
1.235     dyoung   3082:
                   3083:        switch (cmd) {
                   3084:        case SIOCSIFADDRPREF:
                   3085:                ifa->ifa_preference = ifap->ifap_preference;
1.357     ozaki-r  3086:                goto out;
1.235     dyoung   3087:        case SIOCGIFADDRPREF:
                   3088:                /* fill in the if_laddrreq structure */
                   3089:                (void)sockaddr_copy(sstosa(&ifap->ifap_addr),
                   3090:                    sizeof(ifap->ifap_addr), ifa->ifa_addr);
                   3091:                ifap->ifap_preference = ifa->ifa_preference;
1.357     ozaki-r  3092:                goto out;
1.235     dyoung   3093:        default:
1.357     ozaki-r  3094:                error = EOPNOTSUPP;
1.235     dyoung   3095:        }
1.357     ozaki-r  3096: out:
                   3097:        pserialize_read_exit(s);
                   3098:        return error;
1.235     dyoung   3099: }
                   3100:
1.1       cgd      3101: /*
                   3102:  * Interface ioctls.
                   3103:  */
1.273     pooka    3104: static int
                   3105: doifioctl(struct socket *so, u_long cmd, void *data, struct lwp *l)
1.1       cgd      3106: {
1.61      augustss 3107:        struct ifnet *ifp;
                   3108:        struct ifreq *ifr;
1.217     martin   3109:        int error = 0;
1.191     christos 3110: #if defined(COMPAT_OSOCK) || defined(COMPAT_OIFREQ)
                   3111:        u_long ocmd = cmd;
                   3112: #endif
1.49      itojun   3113:        short oif_flags;
1.186     christos 3114: #ifdef COMPAT_OIFREQ
                   3115:        struct ifreq ifrb;
1.187     xtraeme  3116:        struct oifreq *oifr = NULL;
1.186     christos 3117: #endif
1.292     christos 3118:        int r;
1.336     ozaki-r  3119:        struct psref psref;
1.339     ozaki-r  3120:        int bound;
1.1       cgd      3121:
                   3122:        switch (cmd) {
1.186     christos 3123: #ifdef COMPAT_OIFREQ
                   3124:        case OSIOCGIFCONF:
                   3125:        case OOSIOCGIFCONF:
                   3126:                return compat_ifconf(cmd, data);
                   3127: #endif
1.232     christos 3128: #ifdef COMPAT_OIFDATA
                   3129:        case OSIOCGIFDATA:
                   3130:        case OSIOCZIFDATA:
                   3131:                return compat_ifdatareq(l, cmd, data);
                   3132: #endif
1.1       cgd      3133:        case SIOCGIFCONF:
1.185     dyoung   3134:                return ifconf(cmd, data);
1.231     dyoung   3135:        case SIOCINITIFADDR:
                   3136:                return EPERM;
1.1       cgd      3137:        }
1.191     christos 3138:
1.186     christos 3139: #ifdef COMPAT_OIFREQ
1.361     pgoyette 3140:        cmd = (*vec_compat_cvtcmd)(cmd);
1.186     christos 3141:        if (cmd != ocmd) {
                   3142:                oifr = data;
                   3143:                data = ifr = &ifrb;
                   3144:                ifreqo2n(oifr, ifr);
                   3145:        } else
                   3146: #endif
                   3147:                ifr = data;
1.63      thorpej  3148:
                   3149:        switch (cmd) {
                   3150:        case SIOCIFCREATE:
                   3151:        case SIOCIFDESTROY:
1.339     ozaki-r  3152:                bound = curlwp_bind();
1.185     dyoung   3153:                if (l != NULL) {
1.336     ozaki-r  3154:                        ifp = if_get(ifr->ifr_name, &psref);
1.174     elad     3155:                        error = kauth_authorize_network(l->l_cred,
                   3156:                            KAUTH_NETWORK_INTERFACE,
                   3157:                            KAUTH_REQ_NETWORK_INTERFACE_SETPRIV, ifp,
                   3158:                            (void *)cmd, NULL);
1.336     ozaki-r  3159:                        if (ifp != NULL)
                   3160:                                if_put(ifp, &psref);
                   3161:                        if (error != 0) {
1.339     ozaki-r  3162:                                curlwp_bindx(bound);
1.151     yamt     3163:                                return error;
1.336     ozaki-r  3164:                        }
1.151     yamt     3165:                }
1.399     ozaki-r  3166:                KERNEL_LOCK_UNLESS_NET_MPSAFE();
1.292     christos 3167:                mutex_enter(&if_clone_mtx);
                   3168:                r = (cmd == SIOCIFCREATE) ?
1.64      thorpej  3169:                        if_clone_create(ifr->ifr_name) :
1.185     dyoung   3170:                        if_clone_destroy(ifr->ifr_name);
1.292     christos 3171:                mutex_exit(&if_clone_mtx);
1.399     ozaki-r  3172:                KERNEL_UNLOCK_UNLESS_NET_MPSAFE();
1.339     ozaki-r  3173:                curlwp_bindx(bound);
1.292     christos 3174:                return r;
1.67      thorpej  3175:
                   3176:        case SIOCIFGCLONERS:
1.315     martin   3177:                {
                   3178:                        struct if_clonereq *req = (struct if_clonereq *)data;
                   3179:                        return if_clone_list(req->ifcr_count, req->ifcr_buffer,
                   3180:                            &req->ifcr_total);
                   3181:                }
1.63      thorpej  3182:        }
                   3183:
1.339     ozaki-r  3184:        bound = curlwp_bind();
1.336     ozaki-r  3185:        ifp = if_get(ifr->ifr_name, &psref);
                   3186:        if (ifp == NULL) {
1.339     ozaki-r  3187:                curlwp_bindx(bound);
1.185     dyoung   3188:                return ENXIO;
1.336     ozaki-r  3189:        }
1.151     yamt     3190:
                   3191:        switch (cmd) {
1.233     christos 3192:        case SIOCALIFADDR:
                   3193:        case SIOCDLIFADDR:
                   3194:        case SIOCSIFADDRPREF:
1.151     yamt     3195:        case SIOCSIFFLAGS:
                   3196:        case SIOCSIFCAP:
                   3197:        case SIOCSIFMETRIC:
                   3198:        case SIOCZIFDATA:
                   3199:        case SIOCSIFMTU:
                   3200:        case SIOCSIFPHYADDR:
                   3201:        case SIOCDIFPHYADDR:
                   3202: #ifdef INET6
                   3203:        case SIOCSIFPHYADDR_IN6:
                   3204: #endif
                   3205:        case SIOCSLIFPHYADDR:
                   3206:        case SIOCADDMULTI:
                   3207:        case SIOCDELMULTI:
                   3208:        case SIOCSIFMEDIA:
1.154     perry    3209:        case SIOCSDRVSPEC:
1.196     skd      3210:        case SIOCG80211:
                   3211:        case SIOCS80211:
1.151     yamt     3212:        case SIOCS80211NWID:
                   3213:        case SIOCS80211NWKEY:
                   3214:        case SIOCS80211POWER:
                   3215:        case SIOCS80211BSSID:
                   3216:        case SIOCS80211CHANNEL:
1.249     pooka    3217:        case SIOCSLINKSTR:
1.185     dyoung   3218:                if (l != NULL) {
1.174     elad     3219:                        error = kauth_authorize_network(l->l_cred,
                   3220:                            KAUTH_NETWORK_INTERFACE,
                   3221:                            KAUTH_REQ_NETWORK_INTERFACE_SETPRIV, ifp,
                   3222:                            (void *)cmd, NULL);
1.185     dyoung   3223:                        if (error != 0)
1.336     ozaki-r  3224:                                goto out;
1.151     yamt     3225:                }
                   3226:        }
                   3227:
1.49      itojun   3228:        oif_flags = ifp->if_flags;
1.1       cgd      3229:
1.399     ozaki-r  3230:        KERNEL_LOCK_UNLESS_IFP_MPSAFE(ifp);
1.336     ozaki-r  3231:        mutex_enter(ifp->if_ioctl_lock);
                   3232:
1.231     dyoung   3233:        error = (*ifp->if_ioctl)(ifp, cmd, data);
                   3234:        if (error != ENOTTY)
                   3235:                ;
                   3236:        else if (so->so_proto == NULL)
1.252     dyoung   3237:                error = EOPNOTSUPP;
1.231     dyoung   3238:        else {
1.399     ozaki-r  3239:                KERNEL_LOCK_IF_IFP_MPSAFE(ifp);
1.161     christos 3240: #ifdef COMPAT_OSOCK
1.361     pgoyette 3241:                if (vec_compat_ifioctl != NULL)
                   3242:                        error = (*vec_compat_ifioctl)(so, ocmd, cmd, data, l);
                   3243:                else
1.1       cgd      3244: #endif
1.361     pgoyette 3245:                        error = (*so->so_proto->pr_usrreqs->pr_ioctl)(so,
                   3246:                            cmd, data, ifp);
1.399     ozaki-r  3247:                KERNEL_UNLOCK_IF_IFP_MPSAFE(ifp);
1.49      itojun   3248:        }
1.1       cgd      3249:
1.49      itojun   3250:        if (((oif_flags ^ ifp->if_flags) & IFF_UP) != 0) {
1.312     roy      3251:                if ((ifp->if_flags & IFF_UP) != 0) {
1.373     ozaki-r  3252:                        int s = splsoftnet();
1.403     ozaki-r  3253:                        if_up_locked(ifp);
1.49      itojun   3254:                        splx(s);
                   3255:                }
1.1       cgd      3256:        }
1.186     christos 3257: #ifdef COMPAT_OIFREQ
                   3258:        if (cmd != ocmd)
1.246     christos 3259:                ifreqn2o(oifr, ifr);
1.186     christos 3260: #endif
1.49      itojun   3261:
1.336     ozaki-r  3262:        mutex_exit(ifp->if_ioctl_lock);
1.399     ozaki-r  3263:        KERNEL_UNLOCK_UNLESS_IFP_MPSAFE(ifp);
1.336     ozaki-r  3264: out:
                   3265:        if_put(ifp, &psref);
1.339     ozaki-r  3266:        curlwp_bindx(bound);
1.185     dyoung   3267:        return error;
1.1       cgd      3268: }
                   3269:
                   3270: /*
                   3271:  * Return interface configuration
                   3272:  * of system.  List may be used
                   3273:  * in later ioctl's (above) to get
                   3274:  * other information.
1.200     gdt      3275:  *
                   3276:  * Each record is a struct ifreq.  Before the addition of
                   3277:  * sockaddr_storage, the API rule was that sockaddr flavors that did
                   3278:  * not fit would extend beyond the struct ifreq, with the next struct
                   3279:  * ifreq starting sa_len beyond the struct sockaddr.  Because the
                   3280:  * union in struct ifreq includes struct sockaddr_storage, every kind
                   3281:  * of sockaddr must fit.  Thus, there are no longer any overlength
                   3282:  * records.
                   3283:  *
                   3284:  * Records are added to the user buffer if they fit, and ifc_len is
                   3285:  * adjusted to the length that was written.  Thus, the user is only
                   3286:  * assured of getting the complete list if ifc_len on return is at
                   3287:  * least sizeof(struct ifreq) less than it was on entry.
                   3288:  *
                   3289:  * If the user buffer pointer is NULL, this routine copies no data and
                   3290:  * returns the amount of space that would be needed.
                   3291:  *
                   3292:  * Invariants:
                   3293:  * ifrp points to the next part of the user's buffer to be used.  If
                   3294:  * ifrp != NULL, space holds the number of bytes remaining that we may
                   3295:  * write at ifrp.  Otherwise, space holds the number of bytes that
                   3296:  * would have been written had there been adequate space.
1.1       cgd      3297:  */
                   3298: /*ARGSUSED*/
1.302     ozaki-r  3299: static int
1.183     christos 3300: ifconf(u_long cmd, void *data)
1.1       cgd      3301: {
1.61      augustss 3302:        struct ifconf *ifc = (struct ifconf *)data;
                   3303:        struct ifnet *ifp;
                   3304:        struct ifaddr *ifa;
1.304     ozaki-r  3305:        struct ifreq ifr, *ifrp = NULL;
                   3306:        int space = 0, error = 0;
1.200     gdt      3307:        const int sz = (int)sizeof(struct ifreq);
1.304     ozaki-r  3308:        const bool docopy = ifc->ifc_req != NULL;
1.334     ozaki-r  3309:        int s;
1.339     ozaki-r  3310:        int bound;
1.334     ozaki-r  3311:        struct psref psref;
1.1       cgd      3312:
1.304     ozaki-r  3313:        if (docopy) {
1.190     enami    3314:                space = ifc->ifc_len;
1.304     ozaki-r  3315:                ifrp = ifc->ifc_req;
                   3316:        }
                   3317:
1.339     ozaki-r  3318:        bound = curlwp_bind();
1.334     ozaki-r  3319:        s = pserialize_read_enter();
                   3320:        IFNET_READER_FOREACH(ifp) {
                   3321:                psref_acquire(&psref, &ifp->if_psref, ifnet_psref_class);
                   3322:                pserialize_read_exit(s);
                   3323:
1.175     christos 3324:                (void)strncpy(ifr.ifr_name, ifp->if_xname,
1.173     christos 3325:                    sizeof(ifr.ifr_name));
1.334     ozaki-r  3326:                if (ifr.ifr_name[sizeof(ifr.ifr_name) - 1] != '\0') {
                   3327:                        error = ENAMETOOLONG;
                   3328:                        goto release_exit;
                   3329:                }
1.354     ozaki-r  3330:                if (IFADDR_READER_EMPTY(ifp)) {
1.200     gdt      3331:                        /* Interface with no addresses - send zero sockaddr. */
1.127     christos 3332:                        memset(&ifr.ifr_addr, 0, sizeof(ifr.ifr_addr));
1.304     ozaki-r  3333:                        if (!docopy) {
1.218     dyoung   3334:                                space += sz;
1.400     ozaki-r  3335:                                goto next;
1.218     dyoung   3336:                        }
                   3337:                        if (space >= sz) {
                   3338:                                error = copyout(&ifr, ifrp, sz);
                   3339:                                if (error != 0)
1.334     ozaki-r  3340:                                        goto release_exit;
1.218     dyoung   3341:                                ifrp++;
                   3342:                                space -= sz;
1.70      mellon   3343:                        }
1.127     christos 3344:                }
                   3345:
1.400     ozaki-r  3346:                s = pserialize_read_enter();
1.354     ozaki-r  3347:                IFADDR_READER_FOREACH(ifa, ifp) {
1.61      augustss 3348:                        struct sockaddr *sa = ifa->ifa_addr;
1.200     gdt      3349:                        /* all sockaddrs must fit in sockaddr_storage */
                   3350:                        KASSERT(sa->sa_len <= sizeof(ifr.ifr_ifru));
                   3351:
1.304     ozaki-r  3352:                        if (!docopy) {
1.218     dyoung   3353:                                space += sz;
                   3354:                                continue;
                   3355:                        }
                   3356:                        memcpy(&ifr.ifr_space, sa, sa->sa_len);
1.400     ozaki-r  3357:                        pserialize_read_exit(s);
                   3358:
1.218     dyoung   3359:                        if (space >= sz) {
                   3360:                                error = copyout(&ifr, ifrp, sz);
                   3361:                                if (error != 0)
1.334     ozaki-r  3362:                                        goto release_exit;
1.218     dyoung   3363:                                ifrp++; space -= sz;
1.1       cgd      3364:                        }
1.400     ozaki-r  3365:                        s = pserialize_read_enter();
1.1       cgd      3366:                }
1.400     ozaki-r  3367:                pserialize_read_exit(s);
1.334     ozaki-r  3368:
1.400     ozaki-r  3369:         next:
1.334     ozaki-r  3370:                s = pserialize_read_enter();
                   3371:                psref_release(&psref, &ifp->if_psref, ifnet_psref_class);
1.1       cgd      3372:        }
1.334     ozaki-r  3373:        pserialize_read_exit(s);
1.339     ozaki-r  3374:        curlwp_bindx(bound);
1.334     ozaki-r  3375:
1.304     ozaki-r  3376:        if (docopy) {
1.200     gdt      3377:                KASSERT(0 <= space && space <= ifc->ifc_len);
1.127     christos 3378:                ifc->ifc_len -= space;
1.218     dyoung   3379:        } else {
1.200     gdt      3380:                KASSERT(space >= 0);
                   3381:                ifc->ifc_len = space;
                   3382:        }
1.190     enami    3383:        return (0);
1.334     ozaki-r  3384:
                   3385: release_exit:
                   3386:        psref_release(&psref, &ifp->if_psref, ifnet_psref_class);
1.339     ozaki-r  3387:        curlwp_bindx(bound);
1.334     ozaki-r  3388:        return error;
1.1       cgd      3389: }
1.133     jonathan 3390:
1.198     dyoung   3391: int
1.247     christos 3392: ifreq_setaddr(u_long cmd, struct ifreq *ifr, const struct sockaddr *sa)
1.198     dyoung   3393: {
                   3394:        uint8_t len;
1.247     christos 3395: #ifdef COMPAT_OIFREQ
                   3396:        struct ifreq ifrb;
                   3397:        struct oifreq *oifr = NULL;
                   3398:        u_long ocmd = cmd;
1.361     pgoyette 3399:        cmd = (*vec_compat_cvtcmd)(cmd);
1.247     christos 3400:        if (cmd != ocmd) {
                   3401:                oifr = (struct oifreq *)(void *)ifr;
                   3402:                ifr = &ifrb;
                   3403:                ifreqo2n(oifr, ifr);
                   3404:                len = sizeof(oifr->ifr_addr);
                   3405:        } else
                   3406: #endif
                   3407:                len = sizeof(ifr->ifr_ifru.ifru_space);
1.198     dyoung   3408:
                   3409:        if (len < sa->sa_len)
                   3410:                return EFBIG;
1.247     christos 3411:
1.241     joerg    3412:        memset(&ifr->ifr_addr, 0, len);
1.202     dyoung   3413:        sockaddr_copy(&ifr->ifr_addr, len, sa);
1.247     christos 3414:
                   3415: #ifdef COMPAT_OIFREQ
                   3416:        if (cmd != ocmd)
                   3417:                ifreqn2o(oifr, ifr);
                   3418: #endif
1.198     dyoung   3419:        return 0;
                   3420: }
                   3421:
1.155     christos 3422: /*
1.332     knakahar 3423:  * wrapper function for the drivers which doesn't have if_transmit().
1.155     christos 3424:  */
1.345     knakahar 3425: static int
1.332     knakahar 3426: if_transmit(struct ifnet *ifp, struct mbuf *m)
1.155     christos 3427: {
1.332     knakahar 3428:        int s, error;
1.389     ozaki-r  3429:        size_t pktlen = m->m_pkthdr.len;
                   3430:        bool mcast = (m->m_flags & M_MCAST) != 0;
1.332     knakahar 3431:
                   3432:        s = splnet();
1.155     christos 3433:
1.330     knakahar 3434:        IFQ_ENQUEUE(&ifp->if_snd, m, error);
1.332     knakahar 3435:        if (error != 0) {
                   3436:                /* mbuf is already freed */
1.185     dyoung   3437:                goto out;
1.332     knakahar 3438:        }
                   3439:
1.389     ozaki-r  3440:        ifp->if_obytes += pktlen;
                   3441:        if (mcast)
1.155     christos 3442:                ifp->if_omcasts++;
1.332     knakahar 3443:
1.155     christos 3444:        if ((ifp->if_flags & IFF_OACTIVE) == 0)
1.343     knakahar 3445:                if_start_lock(ifp);
1.185     dyoung   3446: out:
1.155     christos 3447:        splx(s);
1.332     knakahar 3448:
1.155     christos 3449:        return error;
                   3450: }
                   3451:
1.345     knakahar 3452: int
                   3453: if_transmit_lock(struct ifnet *ifp, struct mbuf *m)
                   3454: {
                   3455:        int error;
                   3456:
                   3457: #ifdef ALTQ
                   3458:        KERNEL_LOCK(1, NULL);
                   3459:        if (ALTQ_IS_ENABLED(&ifp->if_snd)) {
                   3460:                error = if_transmit(ifp, m);
                   3461:                KERNEL_UNLOCK_ONE(NULL);
                   3462:        } else {
                   3463:                KERNEL_UNLOCK_ONE(NULL);
                   3464:                error = (*ifp->if_transmit)(ifp, m);
1.383     knakahar 3465:                /* mbuf is alredy freed */
1.345     knakahar 3466:        }
                   3467: #else /* !ALTQ */
                   3468:        error = (*ifp->if_transmit)(ifp, m);
1.383     knakahar 3469:        /* mbuf is alredy freed */
1.345     knakahar 3470: #endif /* !ALTQ */
                   3471:
                   3472:        return error;
                   3473: }
                   3474:
1.155     christos 3475: /*
1.332     knakahar 3476:  * Queue message on interface, and start output if interface
                   3477:  * not yet active.
                   3478:  */
                   3479: int
                   3480: ifq_enqueue(struct ifnet *ifp, struct mbuf *m)
                   3481: {
                   3482:
1.345     knakahar 3483:        return if_transmit_lock(ifp, m);
1.332     knakahar 3484: }
                   3485:
                   3486: /*
1.155     christos 3487:  * Queue message on interface, possibly using a second fast queue
                   3488:  */
                   3489: int
1.330     knakahar 3490: ifq_enqueue2(struct ifnet *ifp, struct ifqueue *ifq, struct mbuf *m)
1.155     christos 3491: {
                   3492:        int error = 0;
                   3493:
                   3494:        if (ifq != NULL
                   3495: #ifdef ALTQ
                   3496:            && ALTQ_IS_ENABLED(&ifp->if_snd) == 0
                   3497: #endif
                   3498:            ) {
                   3499:                if (IF_QFULL(ifq)) {
                   3500:                        IF_DROP(&ifp->if_snd);
                   3501:                        m_freem(m);
                   3502:                        if (error == 0)
                   3503:                                error = ENOBUFS;
1.185     dyoung   3504:                } else
1.155     christos 3505:                        IF_ENQUEUE(ifq, m);
                   3506:        } else
1.330     knakahar 3507:                IFQ_ENQUEUE(&ifp->if_snd, m, error);
1.155     christos 3508:        if (error != 0) {
                   3509:                ++ifp->if_oerrors;
                   3510:                return error;
                   3511:        }
                   3512:        return 0;
                   3513: }
                   3514:
1.252     dyoung   3515: int
                   3516: if_addr_init(ifnet_t *ifp, struct ifaddr *ifa, const bool src)
                   3517: {
                   3518:        int rc;
                   3519:
1.408   ! ozaki-r  3520:        KASSERT(mutex_owned(ifp->if_ioctl_lock));
1.252     dyoung   3521:        if (ifp->if_initaddr != NULL)
                   3522:                rc = (*ifp->if_initaddr)(ifp, ifa, src);
                   3523:        else if (src ||
1.391     ozaki-r  3524:                /* FIXME: may not hold if_ioctl_lock */
1.252     dyoung   3525:                 (rc = (*ifp->if_ioctl)(ifp, SIOCSIFDSTADDR, ifa)) == ENOTTY)
                   3526:                rc = (*ifp->if_ioctl)(ifp, SIOCINITIFADDR, ifa);
                   3527:
                   3528:        return rc;
                   3529: }
                   3530:
                   3531: int
1.309     roy      3532: if_do_dad(struct ifnet *ifp)
                   3533: {
                   3534:        if ((ifp->if_flags & IFF_LOOPBACK) != 0)
                   3535:                return 0;
                   3536:
                   3537:        switch (ifp->if_type) {
                   3538:        case IFT_FAITH:
                   3539:                /*
                   3540:                 * These interfaces do not have the IFF_LOOPBACK flag,
                   3541:                 * but loop packets back.  We do not have to do DAD on such
                   3542:                 * interfaces.  We should even omit it, because loop-backed
                   3543:                 * responses would confuse the DAD procedure.
                   3544:                 */
                   3545:                return 0;
                   3546:        default:
                   3547:                /*
                   3548:                 * Our DAD routine requires the interface up and running.
                   3549:                 * However, some interfaces can be up before the RUNNING
                   3550:                 * status.  Additionaly, users may try to assign addresses
                   3551:                 * before the interface becomes up (or running).
                   3552:                 * We simply skip DAD in such a case as a work around.
                   3553:                 * XXX: we should rather mark "tentative" on such addresses,
                   3554:                 * and do DAD after the interface becomes ready.
                   3555:                 */
                   3556:                if ((ifp->if_flags & (IFF_UP|IFF_RUNNING)) !=
                   3557:                    (IFF_UP|IFF_RUNNING))
                   3558:                        return 0;
                   3559:
                   3560:                return 1;
                   3561:        }
                   3562: }
                   3563:
                   3564: int
1.252     dyoung   3565: if_flags_set(ifnet_t *ifp, const short flags)
                   3566: {
                   3567:        int rc;
                   3568:
1.401     ozaki-r  3569:        KASSERT(mutex_owned(ifp->if_ioctl_lock));
                   3570:
1.252     dyoung   3571:        if (ifp->if_setflags != NULL)
                   3572:                rc = (*ifp->if_setflags)(ifp, flags);
                   3573:        else {
1.259     dyoung   3574:                short cantflags, chgdflags;
1.256     dyoung   3575:                struct ifreq ifr;
                   3576:
1.259     dyoung   3577:                chgdflags = ifp->if_flags ^ flags;
                   3578:                cantflags = chgdflags & IFF_CANTCHANGE;
1.256     dyoung   3579:
                   3580:                if (cantflags != 0)
                   3581:                        ifp->if_flags ^= cantflags;
                   3582:
1.259     dyoung   3583:                 /* Traditionally, we do not call if_ioctl after
                   3584:                  * setting/clearing only IFF_PROMISC if the interface
                   3585:                  * isn't IFF_UP.  Uphold that tradition.
                   3586:                 */
1.391     ozaki-r  3587:                if (chgdflags == IFF_PROMISC && (ifp->if_flags & IFF_UP) == 0)
                   3588:                        return 0;
1.259     dyoung   3589:
                   3590:                memset(&ifr, 0, sizeof(ifr));
                   3591:
1.256     dyoung   3592:                ifr.ifr_flags = flags & ~IFF_CANTCHANGE;
1.391     ozaki-r  3593:                /* FIXME: may not hold if_ioctl_lock */
1.252     dyoung   3594:                rc = (*ifp->if_ioctl)(ifp, SIOCSIFFLAGS, &ifr);
1.256     dyoung   3595:
                   3596:                if (rc != 0 && cantflags != 0)
                   3597:                        ifp->if_flags ^= cantflags;
1.252     dyoung   3598:        }
                   3599:
                   3600:        return rc;
                   3601: }
                   3602:
                   3603: int
                   3604: if_mcast_op(ifnet_t *ifp, const unsigned long cmd, const struct sockaddr *sa)
                   3605: {
                   3606:        int rc;
                   3607:        struct ifreq ifr;
                   3608:
                   3609:        if (ifp->if_mcastop != NULL)
                   3610:                rc = (*ifp->if_mcastop)(ifp, cmd, sa);
                   3611:        else {
                   3612:                ifreq_setaddr(cmd, &ifr, sa);
                   3613:                rc = (*ifp->if_ioctl)(ifp, cmd, &ifr);
                   3614:        }
                   3615:
                   3616:        return rc;
                   3617: }
1.155     christos 3618:
1.401     ozaki-r  3619: int
                   3620: if_enable_vlan_mtu(struct ifnet *ifp)
                   3621: {
                   3622:        int error;
                   3623:
                   3624:        mutex_enter(ifp->if_ioctl_lock);
                   3625:        error= ether_enable_vlan_mtu(ifp);
                   3626:        mutex_exit(ifp->if_ioctl_lock);
                   3627:
                   3628:        return error;
                   3629: }
                   3630:
                   3631: int
                   3632: if_disable_vlan_mtu(struct ifnet *ifp)
                   3633: {
                   3634:        int error;
                   3635:
                   3636:        mutex_enter(ifp->if_ioctl_lock);
                   3637:        error= ether_disable_vlan_mtu(ifp);
                   3638:        mutex_exit(ifp->if_ioctl_lock);
                   3639:
                   3640:        return error;
                   3641: }
                   3642:
1.234     dyoung   3643: static void
                   3644: sysctl_sndq_setup(struct sysctllog **clog, const char *ifname,
                   3645:     struct ifaltq *ifq)
                   3646: {
                   3647:        const struct sysctlnode *cnode, *rnode;
                   3648:
                   3649:        if (sysctl_createv(clog, 0, NULL, &rnode,
                   3650:                       CTLFLAG_PERMANENT,
                   3651:                       CTLTYPE_NODE, "interfaces",
                   3652:                       SYSCTL_DESCR("Per-interface controls"),
                   3653:                       NULL, 0, NULL, 0,
1.272     pooka    3654:                       CTL_NET, CTL_CREATE, CTL_EOL) != 0)
1.234     dyoung   3655:                goto bad;
                   3656:
                   3657:        if (sysctl_createv(clog, 0, &rnode, &rnode,
                   3658:                       CTLFLAG_PERMANENT,
                   3659:                       CTLTYPE_NODE, ifname,
                   3660:                       SYSCTL_DESCR("Interface controls"),
                   3661:                       NULL, 0, NULL, 0,
                   3662:                       CTL_CREATE, CTL_EOL) != 0)
                   3663:                goto bad;
                   3664:
                   3665:        if (sysctl_createv(clog, 0, &rnode, &rnode,
                   3666:                       CTLFLAG_PERMANENT,
                   3667:                       CTLTYPE_NODE, "sndq",
                   3668:                       SYSCTL_DESCR("Interface output queue controls"),
                   3669:                       NULL, 0, NULL, 0,
                   3670:                       CTL_CREATE, CTL_EOL) != 0)
                   3671:                goto bad;
                   3672:
                   3673:        if (sysctl_createv(clog, 0, &rnode, &cnode,
                   3674:                       CTLFLAG_PERMANENT,
                   3675:                       CTLTYPE_INT, "len",
                   3676:                       SYSCTL_DESCR("Current output queue length"),
                   3677:                       NULL, 0, &ifq->ifq_len, 0,
                   3678:                       CTL_CREATE, CTL_EOL) != 0)
                   3679:                goto bad;
                   3680:
                   3681:        if (sysctl_createv(clog, 0, &rnode, &cnode,
                   3682:                       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
                   3683:                       CTLTYPE_INT, "maxlen",
                   3684:                       SYSCTL_DESCR("Maximum allowed output queue length"),
                   3685:                       NULL, 0, &ifq->ifq_maxlen, 0,
                   3686:                       CTL_CREATE, CTL_EOL) != 0)
                   3687:                goto bad;
                   3688:
                   3689:        if (sysctl_createv(clog, 0, &rnode, &cnode,
                   3690:                       CTLFLAG_PERMANENT,
                   3691:                       CTLTYPE_INT, "drops",
                   3692:                       SYSCTL_DESCR("Packets dropped due to full output queue"),
                   3693:                       NULL, 0, &ifq->ifq_drops, 0,
                   3694:                       CTL_CREATE, CTL_EOL) != 0)
                   3695:                goto bad;
                   3696:
                   3697:        return;
                   3698: bad:
                   3699:        printf("%s: could not attach sysctl nodes\n", ifname);
                   3700:        return;
                   3701: }
                   3702:
1.138     drochner 3703: #if defined(INET) || defined(INET6)
1.276     rmind    3704:
                   3705: #define        SYSCTL_NET_PKTQ(q, cn, c)                                       \
                   3706:        static int                                                      \
                   3707:        sysctl_net_##q##_##cn(SYSCTLFN_ARGS)                            \
                   3708:        {                                                               \
                   3709:                return sysctl_pktq_count(SYSCTLFN_CALL(rnode), q, c);   \
                   3710:        }
                   3711:
                   3712: #if defined(INET)
1.279     rmind    3713: static int
                   3714: sysctl_net_ip_pktq_maxlen(SYSCTLFN_ARGS)
                   3715: {
                   3716:        return sysctl_pktq_maxlen(SYSCTLFN_CALL(rnode), ip_pktq);
                   3717: }
1.276     rmind    3718: SYSCTL_NET_PKTQ(ip_pktq, items, PKTQ_NITEMS)
                   3719: SYSCTL_NET_PKTQ(ip_pktq, drops, PKTQ_DROPS)
                   3720: #endif
1.279     rmind    3721:
1.276     rmind    3722: #if defined(INET6)
1.279     rmind    3723: static int
                   3724: sysctl_net_ip6_pktq_maxlen(SYSCTLFN_ARGS)
                   3725: {
                   3726:        return sysctl_pktq_maxlen(SYSCTLFN_CALL(rnode), ip6_pktq);
                   3727: }
1.276     rmind    3728: SYSCTL_NET_PKTQ(ip6_pktq, items, PKTQ_NITEMS)
                   3729: SYSCTL_NET_PKTQ(ip6_pktq, drops, PKTQ_DROPS)
                   3730: #endif
                   3731:
1.136     atatat   3732: static void
1.276     rmind    3733: sysctl_net_pktq_setup(struct sysctllog **clog, int pf)
1.136     atatat   3734: {
1.276     rmind    3735:        sysctlfn len_func = NULL, maxlen_func = NULL, drops_func = NULL;
                   3736:        const char *pfname = NULL, *ipname = NULL;
                   3737:        int ipn = 0, qid = 0;
                   3738:
                   3739:        switch (pf) {
                   3740: #if defined(INET)
                   3741:        case PF_INET:
                   3742:                len_func = sysctl_net_ip_pktq_items;
                   3743:                maxlen_func = sysctl_net_ip_pktq_maxlen;
                   3744:                drops_func = sysctl_net_ip_pktq_drops;
                   3745:                pfname = "inet", ipn = IPPROTO_IP;
                   3746:                ipname = "ip", qid = IPCTL_IFQ;
                   3747:                break;
                   3748: #endif
                   3749: #if defined(INET6)
                   3750:        case PF_INET6:
                   3751:                len_func = sysctl_net_ip6_pktq_items;
                   3752:                maxlen_func = sysctl_net_ip6_pktq_maxlen;
                   3753:                drops_func = sysctl_net_ip6_pktq_drops;
                   3754:                pfname = "inet6", ipn = IPPROTO_IPV6;
                   3755:                ipname = "ip6", qid = IPV6CTL_IFQ;
                   3756:                break;
                   3757: #endif
                   3758:        default:
                   3759:                KASSERT(false);
                   3760:        }
1.136     atatat   3761:
1.139     atatat   3762:        sysctl_createv(clog, 0, NULL, NULL,
                   3763:                       CTLFLAG_PERMANENT,
1.136     atatat   3764:                       CTLTYPE_NODE, pfname, NULL,
                   3765:                       NULL, 0, NULL, 0,
                   3766:                       CTL_NET, pf, CTL_EOL);
1.139     atatat   3767:        sysctl_createv(clog, 0, NULL, NULL,
                   3768:                       CTLFLAG_PERMANENT,
1.136     atatat   3769:                       CTLTYPE_NODE, ipname, NULL,
                   3770:                       NULL, 0, NULL, 0,
                   3771:                       CTL_NET, pf, ipn, CTL_EOL);
1.139     atatat   3772:        sysctl_createv(clog, 0, NULL, NULL,
                   3773:                       CTLFLAG_PERMANENT,
1.142     atatat   3774:                       CTLTYPE_NODE, "ifq",
                   3775:                       SYSCTL_DESCR("Protocol input queue controls"),
1.139     atatat   3776:                       NULL, 0, NULL, 0,
                   3777:                       CTL_NET, pf, ipn, qid, CTL_EOL);
1.136     atatat   3778:
1.139     atatat   3779:        sysctl_createv(clog, 0, NULL, NULL,
                   3780:                       CTLFLAG_PERMANENT,
1.142     atatat   3781:                       CTLTYPE_INT, "len",
                   3782:                       SYSCTL_DESCR("Current input queue length"),
1.276     rmind    3783:                       len_func, 0, NULL, 0,
1.136     atatat   3784:                       CTL_NET, pf, ipn, qid, IFQCTL_LEN, CTL_EOL);
1.139     atatat   3785:        sysctl_createv(clog, 0, NULL, NULL,
                   3786:                       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
1.142     atatat   3787:                       CTLTYPE_INT, "maxlen",
                   3788:                       SYSCTL_DESCR("Maximum allowed input queue length"),
1.276     rmind    3789:                       maxlen_func, 0, NULL, 0,
1.136     atatat   3790:                       CTL_NET, pf, ipn, qid, IFQCTL_MAXLEN, CTL_EOL);
1.139     atatat   3791:        sysctl_createv(clog, 0, NULL, NULL,
                   3792:                       CTLFLAG_PERMANENT,
1.142     atatat   3793:                       CTLTYPE_INT, "drops",
                   3794:                       SYSCTL_DESCR("Packets dropped due to full input queue"),
1.276     rmind    3795:                       drops_func, 0, NULL, 0,
1.136     atatat   3796:                       CTL_NET, pf, ipn, qid, IFQCTL_DROPS, CTL_EOL);
                   3797: }
1.138     drochner 3798: #endif /* INET || INET6 */
1.280     joerg    3799:
                   3800: static int
                   3801: if_sdl_sysctl(SYSCTLFN_ARGS)
                   3802: {
                   3803:        struct ifnet *ifp;
                   3804:        const struct sockaddr_dl *sdl;
1.340     ozaki-r  3805:        struct psref psref;
                   3806:        int error = 0;
                   3807:        int bound;
1.280     joerg    3808:
                   3809:        if (namelen != 1)
                   3810:                return EINVAL;
                   3811:
1.340     ozaki-r  3812:        bound = curlwp_bind();
                   3813:        ifp = if_get_byindex(name[0], &psref);
                   3814:        if (ifp == NULL) {
                   3815:                error = ENODEV;
1.341     riastrad 3816:                goto out0;
1.340     ozaki-r  3817:        }
1.280     joerg    3818:
                   3819:        sdl = ifp->if_sadl;
                   3820:        if (sdl == NULL) {
                   3821:                *oldlenp = 0;
1.341     riastrad 3822:                goto out1;
1.280     joerg    3823:        }
                   3824:
                   3825:        if (oldp == NULL) {
                   3826:                *oldlenp = sdl->sdl_alen;
1.341     riastrad 3827:                goto out1;
1.280     joerg    3828:        }
                   3829:
                   3830:        if (*oldlenp >= sdl->sdl_alen)
                   3831:                *oldlenp = sdl->sdl_alen;
1.340     ozaki-r  3832:        error = sysctl_copyout(l, &sdl->sdl_data[sdl->sdl_nlen], oldp, *oldlenp);
1.341     riastrad 3833: out1:
1.340     ozaki-r  3834:        if_put(ifp, &psref);
1.341     riastrad 3835: out0:
1.340     ozaki-r  3836:        curlwp_bindx(bound);
                   3837:        return error;
1.280     joerg    3838: }
                   3839:
1.370     ozaki-r  3840: static void
                   3841: if_sysctl_setup(struct sysctllog **clog)
1.280     joerg    3842: {
                   3843:        const struct sysctlnode *rnode = NULL;
                   3844:
                   3845:        sysctl_createv(clog, 0, NULL, &rnode,
                   3846:                       CTLFLAG_PERMANENT,
                   3847:                       CTLTYPE_NODE, "sdl",
                   3848:                       SYSCTL_DESCR("Get active link-layer address"),
                   3849:                       if_sdl_sysctl, 0, NULL, 0,
                   3850:                       CTL_NET, CTL_CREATE, CTL_EOL);
1.370     ozaki-r  3851:
                   3852: #if defined(INET)
                   3853:        sysctl_net_pktq_setup(NULL, PF_INET);
                   3854: #endif
                   3855: #ifdef INET6
                   3856:        if (in6_present)
                   3857:                sysctl_net_pktq_setup(NULL, PF_INET6);
                   3858: #endif
1.280     joerg    3859: }

CVSweb <webmaster@jp.NetBSD.org>