[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.339

1.339   ! ozaki-r     1: /*     $NetBSD: if.c,v 1.338 2016/06/10 13:31:44 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.339   ! ozaki-r    93: __KERNEL_RCSID(0, "$NetBSD: if.c,v 1.338 2016/06/10 13:31:44 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.46      thorpej    97:
1.51      bouyer     98: #include "opt_atalk.h"
1.120     martin     99: #include "opt_natm.h"
1.288     ozaki-r   100: #include "opt_wlan.h"
1.308     ozaki-r   101: #include "opt_net_mpsafe.h"
                    102: #endif
1.1       cgd       103:
1.8       mycroft   104: #include <sys/param.h>
                    105: #include <sys/mbuf.h>
                    106: #include <sys/systm.h>
1.59      thorpej   107: #include <sys/callout.h>
1.15      mycroft   108: #include <sys/proc.h>
1.8       mycroft   109: #include <sys/socket.h>
                    110: #include <sys/socketvar.h>
1.56      thorpej   111: #include <sys/domain.h>
1.8       mycroft   112: #include <sys/protosw.h>
                    113: #include <sys/kernel.h>
                    114: #include <sys/ioctl.h>
1.133     jonathan  115: #include <sys/sysctl.h>
1.159     dyoung    116: #include <sys/syslog.h>
1.165     elad      117: #include <sys/kauth.h>
1.254     dyoung    118: #include <sys/kmem.h>
1.276     rmind     119: #include <sys/xcall.h>
1.323     ozaki-r   120: #include <sys/cpu.h>
                    121: #include <sys/intr.h>
1.1       cgd       122:
1.8       mycroft   123: #include <net/if.h>
                    124: #include <net/if_dl.h>
1.66      onoe      125: #include <net/if_ether.h>
1.124     dyoung    126: #include <net/if_media.h>
1.132     dyoung    127: #include <net80211/ieee80211.h>
                    128: #include <net80211/ieee80211_ioctl.h>
1.8       mycroft   129: #include <net/if_types.h>
1.53      thorpej   130: #include <net/route.h>
1.95      itojun    131: #include <net/netisr.h>
1.262     christos  132: #include <sys/module.h>
1.51      bouyer    133: #ifdef NETATALK
                    134: #include <netatalk/at_extern.h>
                    135: #include <netatalk/at.h>
                    136: #endif
1.143     itojun    137: #include <net/pfil.h>
1.278     he        138: #include <netinet/in.h>
1.276     rmind     139: #include <netinet/in_var.h>
1.1       cgd       140:
1.49      itojun    141: #ifdef INET6
1.72      thorpej   142: #include <netinet6/in6_var.h>
1.108     itojun    143: #include <netinet6/nd6.h>
1.49      itojun    144: #endif
1.117     thorpej   145:
1.288     ozaki-r   146: #include "ether.h"
                    147: #include "fddi.h"
                    148: #include "token.h"
                    149:
1.166     liamjfoy  150: #include "carp.h"
                    151: #if NCARP > 0
                    152: #include <netinet/ip_carp.h>
                    153: #endif
                    154:
1.186     christos  155: #include <compat/sys/sockio.h>
1.161     christos  156: #include <compat/sys/socket.h>
                    157:
1.117     thorpej   158: MALLOC_DEFINE(M_IFADDR, "ifaddr", "interface address");
                    159: MALLOC_DEFINE(M_IFMADDR, "ether_multi", "link-level multicast address");
1.49      itojun    160:
1.274     rmind     161: /*
                    162:  * Global list of interfaces.
                    163:  */
1.334     ozaki-r   164: /* DEPRECATED. Remove it once kvm(3) users disappeared */
1.274     rmind     165: struct ifnet_head              ifnet_list;
1.334     ozaki-r   166:
                    167: struct pslist_head             ifnet_pslist;
1.274     rmind     168: static ifnet_t **              ifindex2ifnet = NULL;
                    169: static u_int                   if_index = 1;
                    170: static size_t                  if_indexlim = 0;
                    171: static uint64_t                        index_gen;
1.334     ozaki-r   172: /* Mutex to protect the above objects. */
                    173: kmutex_t                       ifnet_mtx __cacheline_aligned;
                    174: struct psref_class             *ifnet_psref_class __read_mostly;
                    175: static pserialize_t            ifnet_psz;
                    176:
1.292     christos  177: static kmutex_t                        if_clone_mtx;
1.274     rmind     178:
                    179: struct ifnet *lo0ifp;
1.1       cgd       180: int    ifqmaxlen = IFQ_MAXLEN;
1.104     matt      181:
1.192     dyoung    182: static int     if_rt_walktree(struct rtentry *, void *);
1.53      thorpej   183:
1.163     thorpej   184: static struct if_clone *if_clone_lookup(const char *, int *);
1.63      thorpej   185:
1.163     thorpej   186: static LIST_HEAD(, if_clone) if_cloners = LIST_HEAD_INITIALIZER(if_cloners);
                    187: static int if_cloners_count;
1.63      thorpej   188:
1.265     rmind     189: /* Packet filtering hook for interfaces. */
                    190: pfil_head_t *  if_pfil;
1.143     itojun    191:
1.239     elad      192: static kauth_listener_t if_listener;
                    193:
1.273     pooka     194: static int doifioctl(struct socket *, u_long, void *, struct lwp *);
1.163     thorpej   195: static void if_detach_queues(struct ifnet *, struct ifqueue *);
1.234     dyoung    196: static void sysctl_sndq_setup(struct sysctllog **, const char *,
                    197:     struct ifaltq *);
1.294     ozaki-r   198: static void if_slowtimo(void *);
1.300     ozaki-r   199: static void if_free_sadl(struct ifnet *);
1.302     ozaki-r   200: static void if_attachdomain1(struct ifnet *);
                    201: static int ifconf(u_long, void *);
                    202: static int if_clone_create(const char *);
                    203: static int if_clone_destroy(const char *);
1.324     ozaki-r   204: static void if_link_state_change_si(void *);
1.95      itojun    205:
1.323     ozaki-r   206: struct if_percpuq {
                    207:        struct ifnet    *ipq_ifp;
                    208:        void            *ipq_si;
                    209:        struct percpu   *ipq_ifqs;      /* struct ifqueue */
                    210: };
                    211:
                    212: static struct mbuf *if_percpuq_dequeue(struct if_percpuq *);
                    213:
1.327     knakahar  214: static void if_percpuq_drops(void *, void *, struct cpu_info *);
                    215: static int sysctl_percpuq_drops_handler(SYSCTLFN_PROTO);
                    216: static void sysctl_percpuq_setup(struct sysctllog **, const char *,
                    217:     struct if_percpuq *);
                    218:
1.240     cegger    219: #if defined(INET) || defined(INET6)
1.276     rmind     220: static void sysctl_net_pktq_setup(struct sysctllog **, int);
1.240     cegger    221: #endif
1.237     pooka     222:
1.239     elad      223: static int
                    224: if_listener_cb(kauth_cred_t cred, kauth_action_t action, void *cookie,
                    225:     void *arg0, void *arg1, void *arg2, void *arg3)
                    226: {
                    227:        int result;
                    228:        enum kauth_network_req req;
                    229:
                    230:        result = KAUTH_RESULT_DEFER;
                    231:        req = (enum kauth_network_req)arg1;
                    232:
                    233:        if (action != KAUTH_NETWORK_INTERFACE)
                    234:                return result;
                    235:
                    236:        if ((req == KAUTH_REQ_NETWORK_INTERFACE_GET) ||
                    237:            (req == KAUTH_REQ_NETWORK_INTERFACE_SET))
                    238:                result = KAUTH_RESULT_ALLOW;
                    239:
                    240:        return result;
                    241: }
                    242:
1.1       cgd       243: /*
                    244:  * Network interface utility routines.
                    245:  *
                    246:  * Routines with ifa_ifwith* names take sockaddr *'s as
                    247:  * parameters.
                    248:  */
1.4       andrew    249: void
1.163     thorpej   250: ifinit(void)
1.1       cgd       251: {
1.276     rmind     252: #if defined(INET)
                    253:        sysctl_net_pktq_setup(NULL, PF_INET);
                    254: #endif
1.237     pooka     255: #ifdef INET6
1.311     pooka     256:        if (in6_present)
                    257:                sysctl_net_pktq_setup(NULL, PF_INET6);
1.276     rmind     258: #endif
1.1       cgd       259:
1.239     elad      260:        if_listener = kauth_listen_scope(KAUTH_SCOPE_NETWORK,
                    261:            if_listener_cb, NULL);
1.273     pooka     262:
                    263:        /* interfaces are available, inform socket code */
                    264:        ifioctl = doifioctl;
1.227     yamt      265: }
                    266:
                    267: /*
                    268:  * XXX Initialization before configure().
                    269:  * XXX hack to get pfil_add_hook working in autoconf.
                    270:  */
                    271: void
                    272: ifinit1(void)
                    273: {
1.292     christos  274:        mutex_init(&if_clone_mtx, MUTEX_DEFAULT, IPL_NONE);
1.274     rmind     275:        TAILQ_INIT(&ifnet_list);
1.334     ozaki-r   276:        mutex_init(&ifnet_mtx, MUTEX_DEFAULT, IPL_NONE);
                    277:        ifnet_psz = pserialize_create();
                    278:        ifnet_psref_class = psref_class_create("ifnet", IPL_SOFTNET);
                    279:        PSLIST_INIT(&ifnet_pslist);
1.274     rmind     280:        if_indexlim = 8;
                    281:
1.265     rmind     282:        if_pfil = pfil_head_create(PFIL_TYPE_IFNET, NULL);
                    283:        KASSERT(if_pfil != NULL);
1.286     ozaki-r   284:
1.288     ozaki-r   285: #if NETHER > 0 || NFDDI > 0 || defined(NETATALK) || NTOKEN > 0 || defined(WLAN)
1.286     ozaki-r   286:        etherinit();
1.288     ozaki-r   287: #endif
1.1       cgd       288: }
                    289:
1.274     rmind     290: ifnet_t *
1.226     christos  291: if_alloc(u_char type)
                    292: {
1.274     rmind     293:        return kmem_zalloc(sizeof(ifnet_t), KM_SLEEP);
1.226     christos  294: }
                    295:
                    296: void
1.274     rmind     297: if_free(ifnet_t *ifp)
1.251     dyoung    298: {
1.274     rmind     299:        kmem_free(ifp, sizeof(ifnet_t));
1.251     dyoung    300: }
                    301:
                    302: void
1.226     christos  303: if_initname(struct ifnet *ifp, const char *name, int unit)
                    304: {
                    305:        (void)snprintf(ifp->if_xname, sizeof(ifp->if_xname),
                    306:            "%s%d", name, unit);
                    307: }
                    308:
1.53      thorpej   309: /*
                    310:  * Null routines used while an interface is going away.  These routines
                    311:  * just return an error.
                    312:  */
                    313:
                    314: int
1.177     christos  315: if_nulloutput(struct ifnet *ifp, struct mbuf *m,
1.331     ozaki-r   316:     const struct sockaddr *so, const struct rtentry *rt)
1.53      thorpej   317: {
                    318:
1.185     dyoung    319:        return ENXIO;
1.53      thorpej   320: }
                    321:
                    322: void
1.177     christos  323: if_nullinput(struct ifnet *ifp, struct mbuf *m)
1.53      thorpej   324: {
                    325:
                    326:        /* Nothing. */
                    327: }
                    328:
                    329: void
1.177     christos  330: if_nullstart(struct ifnet *ifp)
1.53      thorpej   331: {
                    332:
                    333:        /* Nothing. */
                    334: }
                    335:
                    336: int
1.332     knakahar  337: if_nulltransmit(struct ifnet *ifp, struct mbuf *m)
                    338: {
                    339:
                    340:        return ENXIO;
                    341: }
                    342:
                    343: int
1.183     christos  344: if_nullioctl(struct ifnet *ifp, u_long cmd, void *data)
1.53      thorpej   345: {
                    346:
1.185     dyoung    347:        return ENXIO;
1.53      thorpej   348: }
                    349:
                    350: int
1.177     christos  351: if_nullinit(struct ifnet *ifp)
1.53      thorpej   352: {
                    353:
1.185     dyoung    354:        return ENXIO;
1.53      thorpej   355: }
                    356:
                    357: void
1.177     christos  358: if_nullstop(struct ifnet *ifp, int disable)
1.75      thorpej   359: {
                    360:
                    361:        /* Nothing. */
                    362: }
                    363:
                    364: void
1.295     ozaki-r   365: if_nullslowtimo(struct ifnet *ifp)
1.53      thorpej   366: {
                    367:
                    368:        /* Nothing. */
                    369: }
                    370:
                    371: void
1.177     christos  372: if_nulldrain(struct ifnet *ifp)
1.53      thorpej   373: {
                    374:
                    375:        /* Nothing. */
                    376: }
                    377:
1.210     dyoung    378: void
1.231     dyoung    379: if_set_sadl(struct ifnet *ifp, const void *lla, u_char addrlen, bool factory)
1.210     dyoung    380: {
                    381:        struct ifaddr *ifa;
                    382:        struct sockaddr_dl *sdl;
                    383:
                    384:        ifp->if_addrlen = addrlen;
                    385:        if_alloc_sadl(ifp);
                    386:        ifa = ifp->if_dl;
                    387:        sdl = satosdl(ifa->ifa_addr);
                    388:
                    389:        (void)sockaddr_dl_setaddr(sdl, sdl->sdl_len, lla, ifp->if_addrlen);
1.231     dyoung    390:        if (factory) {
                    391:                ifp->if_hwdl = ifp->if_dl;
1.291     rmind     392:                ifaref(ifp->if_hwdl);
1.231     dyoung    393:        }
1.223     dyoung    394:        /* TBD routing socket */
1.210     dyoung    395: }
                    396:
1.211     dyoung    397: struct ifaddr *
                    398: if_dl_create(const struct ifnet *ifp, const struct sockaddr_dl **sdlp)
                    399: {
                    400:        unsigned socksize, ifasize;
                    401:        int addrlen, namelen;
                    402:        struct sockaddr_dl *mask, *sdl;
                    403:        struct ifaddr *ifa;
                    404:
                    405:        namelen = strlen(ifp->if_xname);
                    406:        addrlen = ifp->if_addrlen;
                    407:        socksize = roundup(sockaddr_dl_measure(namelen, addrlen), sizeof(long));
                    408:        ifasize = sizeof(*ifa) + 2 * socksize;
                    409:        ifa = (struct ifaddr *)malloc(ifasize, M_IFADDR, M_WAITOK|M_ZERO);
                    410:
                    411:        sdl = (struct sockaddr_dl *)(ifa + 1);
                    412:        mask = (struct sockaddr_dl *)(socksize + (char *)sdl);
                    413:
                    414:        sockaddr_dl_init(sdl, socksize, ifp->if_index, ifp->if_type,
                    415:            ifp->if_xname, namelen, NULL, addrlen);
                    416:        mask->sdl_len = sockaddr_dl_measure(namelen, 0);
                    417:        memset(&mask->sdl_data[0], 0xff, namelen);
                    418:        ifa->ifa_rtrequest = link_rtrequest;
                    419:        ifa->ifa_addr = (struct sockaddr *)sdl;
                    420:        ifa->ifa_netmask = (struct sockaddr *)mask;
                    421:
                    422:        *sdlp = sdl;
                    423:
                    424:        return ifa;
                    425: }
                    426:
1.223     dyoung    427: static void
                    428: if_sadl_setrefs(struct ifnet *ifp, struct ifaddr *ifa)
                    429: {
                    430:        const struct sockaddr_dl *sdl;
1.316     ozaki-r   431:
1.223     dyoung    432:        ifp->if_dl = ifa;
1.291     rmind     433:        ifaref(ifa);
1.223     dyoung    434:        sdl = satosdl(ifa->ifa_addr);
                    435:        ifp->if_sadl = sdl;
                    436: }
                    437:
1.1       cgd       438: /*
1.81      thorpej   439:  * Allocate the link level name for the specified interface.  This
                    440:  * is an attachment helper.  It must be called after ifp->if_addrlen
                    441:  * is initialized, which may not be the case when if_attach() is
                    442:  * called.
                    443:  */
                    444: void
                    445: if_alloc_sadl(struct ifnet *ifp)
                    446: {
                    447:        struct ifaddr *ifa;
1.211     dyoung    448:        const struct sockaddr_dl *sdl;
1.84      thorpej   449:
                    450:        /*
                    451:         * If the interface already has a link name, release it
                    452:         * now.  This is useful for interfaces that can change
                    453:         * link types, and thus switch link names often.
                    454:         */
                    455:        if (ifp->if_sadl != NULL)
                    456:                if_free_sadl(ifp);
1.81      thorpej   457:
1.211     dyoung    458:        ifa = if_dl_create(ifp, &sdl);
1.195     dyoung    459:
1.207     dyoung    460:        ifa_insert(ifp, ifa);
1.223     dyoung    461:        if_sadl_setrefs(ifp, ifa);
                    462: }
                    463:
                    464: static void
                    465: if_deactivate_sadl(struct ifnet *ifp)
                    466: {
                    467:        struct ifaddr *ifa;
                    468:
                    469:        KASSERT(ifp->if_dl != NULL);
                    470:
                    471:        ifa = ifp->if_dl;
                    472:
                    473:        ifp->if_sadl = NULL;
                    474:
                    475:        ifp->if_dl = NULL;
1.291     rmind     476:        ifafree(ifa);
1.223     dyoung    477: }
                    478:
1.224     dyoung    479: void
1.223     dyoung    480: if_activate_sadl(struct ifnet *ifp, struct ifaddr *ifa,
                    481:     const struct sockaddr_dl *sdl)
                    482: {
                    483:        int s;
                    484:
                    485:        s = splnet();
                    486:
                    487:        if_deactivate_sadl(ifp);
                    488:
                    489:        if_sadl_setrefs(ifp, ifa);
1.231     dyoung    490:        IFADDR_FOREACH(ifa, ifp)
                    491:                rtinit(ifa, RTM_LLINFO_UPD, 0);
1.223     dyoung    492:        splx(s);
1.81      thorpej   493: }
                    494:
                    495: /*
                    496:  * Free the link level name for the specified interface.  This is
1.300     ozaki-r   497:  * a detach helper.  This is called from if_detach().
1.81      thorpej   498:  */
1.300     ozaki-r   499: static void
1.81      thorpej   500: if_free_sadl(struct ifnet *ifp)
                    501: {
                    502:        struct ifaddr *ifa;
                    503:        int s;
                    504:
1.316     ozaki-r   505:        ifa = ifp->if_dl;
1.81      thorpej   506:        if (ifa == NULL) {
                    507:                KASSERT(ifp->if_sadl == NULL);
                    508:                return;
                    509:        }
                    510:
                    511:        KASSERT(ifp->if_sadl != NULL);
                    512:
1.88      thorpej   513:        s = splnet();
1.81      thorpej   514:        rtinit(ifa, RTM_DELETE, 0);
1.207     dyoung    515:        ifa_remove(ifp, ifa);
1.223     dyoung    516:        if_deactivate_sadl(ifp);
1.231     dyoung    517:        if (ifp->if_hwdl == ifa) {
1.291     rmind     518:                ifafree(ifa);
1.231     dyoung    519:                ifp->if_hwdl = NULL;
                    520:        }
1.81      thorpej   521:        splx(s);
                    522: }
                    523:
1.274     rmind     524: static void
                    525: if_getindex(ifnet_t *ifp)
1.1       cgd       526: {
1.274     rmind     527:        bool hitlimit = false;
1.231     dyoung    528:
1.234     dyoung    529:        ifp->if_index_gen = index_gen++;
                    530:
1.102     atatat    531:        ifp->if_index = if_index;
1.274     rmind     532:        if (ifindex2ifnet == NULL) {
1.102     atatat    533:                if_index++;
1.274     rmind     534:                goto skip;
                    535:        }
                    536:        while (if_byindex(ifp->if_index)) {
                    537:                /*
                    538:                 * If we hit USHRT_MAX, we skip back to 0 since
                    539:                 * there are a number of places where the value
                    540:                 * of if_index or if_index itself is compared
                    541:                 * to or stored in an unsigned short.  By
                    542:                 * jumping back, we won't botch those assignments
                    543:                 * or comparisons.
                    544:                 */
                    545:                if (++if_index == 0) {
                    546:                        if_index = 1;
                    547:                } else if (if_index == USHRT_MAX) {
1.102     atatat    548:                        /*
1.274     rmind     549:                         * However, if we have to jump back to
                    550:                         * zero *twice* without finding an empty
                    551:                         * slot in ifindex2ifnet[], then there
                    552:                         * there are too many (>65535) interfaces.
1.102     atatat    553:                         */
1.274     rmind     554:                        if (hitlimit) {
                    555:                                panic("too many interfaces");
1.102     atatat    556:                        }
1.274     rmind     557:                        hitlimit = true;
                    558:                        if_index = 1;
1.102     atatat    559:                }
1.274     rmind     560:                ifp->if_index = if_index;
                    561:        }
                    562: skip:
1.49      itojun    563:        /*
1.316     ozaki-r   564:         * ifindex2ifnet is indexed by if_index. Since if_index will
                    565:         * grow dynamically, it should grow too.
1.49      itojun    566:         */
1.316     ozaki-r   567:        if (ifindex2ifnet == NULL || ifp->if_index >= if_indexlim) {
1.131     itojun    568:                size_t m, n, oldlim;
1.183     christos  569:                void *q;
1.154     perry     570:
1.131     itojun    571:                oldlim = if_indexlim;
1.53      thorpej   572:                while (ifp->if_index >= if_indexlim)
1.49      itojun    573:                        if_indexlim <<= 1;
                    574:
                    575:                /* grow ifindex2ifnet */
1.131     itojun    576:                m = oldlim * sizeof(struct ifnet *);
1.49      itojun    577:                n = if_indexlim * sizeof(struct ifnet *);
1.230     dyoung    578:                q = malloc(n, M_IFADDR, M_WAITOK|M_ZERO);
1.185     dyoung    579:                if (ifindex2ifnet != NULL) {
1.230     dyoung    580:                        memcpy(q, ifindex2ifnet, m);
                    581:                        free(ifindex2ifnet, M_IFADDR);
1.49      itojun    582:                }
                    583:                ifindex2ifnet = (struct ifnet **)q;
1.1       cgd       584:        }
1.274     rmind     585:        ifindex2ifnet[ifp->if_index] = ifp;
                    586: }
                    587:
                    588: /*
1.307     ozaki-r   589:  * Initialize an interface and assign an index for it.
                    590:  *
                    591:  * It must be called prior to a device specific attach routine
                    592:  * (e.g., ether_ifattach and ieee80211_ifattach) or if_alloc_sadl,
                    593:  * and be followed by if_register:
                    594:  *
                    595:  *     if_initialize(ifp);
                    596:  *     ether_ifattach(ifp, enaddr);
                    597:  *     if_register(ifp);
1.274     rmind     598:  */
                    599: void
1.307     ozaki-r   600: if_initialize(ifnet_t *ifp)
1.274     rmind     601: {
                    602:        KASSERT(if_indexlim > 0);
                    603:        TAILQ_INIT(&ifp->if_addrlist);
1.49      itojun    604:
1.1       cgd       605:        /*
1.81      thorpej   606:         * Link level name is allocated later by a separate call to
                    607:         * if_alloc_sadl().
1.1       cgd       608:         */
1.81      thorpej   609:
1.40      thorpej   610:        if (ifp->if_snd.ifq_maxlen == 0)
1.94      itojun    611:                ifp->if_snd.ifq_maxlen = ifqmaxlen;
1.234     dyoung    612:
1.42      is        613:        ifp->if_broadcastaddr = 0; /* reliably crash if used uninitialized */
1.57      thorpej   614:
                    615:        ifp->if_link_state = LINK_STATE_UNKNOWN;
1.325     roy       616:        ifp->if_link_queue = -1; /* all bits set, see link_state_change() */
1.57      thorpej   617:
1.89      thorpej   618:        ifp->if_capenable = 0;
1.97      thorpej   619:        ifp->if_csum_flags_tx = 0;
                    620:        ifp->if_csum_flags_rx = 0;
1.89      thorpej   621:
1.86      thorpej   622: #ifdef ALTQ
                    623:        ifp->if_snd.altq_type = 0;
                    624:        ifp->if_snd.altq_disc = NULL;
                    625:        ifp->if_snd.altq_flags &= ALTQF_CANTCHANGE;
                    626:        ifp->if_snd.altq_tbr  = NULL;
                    627:        ifp->if_snd.altq_ifp  = ifp;
                    628: #endif
                    629:
1.285     ozaki-r   630: #ifdef NET_MPSAFE
                    631:        ifp->if_snd.ifq_lock = mutex_obj_alloc(MUTEX_DEFAULT, IPL_NET);
                    632: #else
                    633:        ifp->if_snd.ifq_lock = NULL;
                    634: #endif
                    635:
1.265     rmind     636:        ifp->if_pfil = pfil_head_create(PFIL_TYPE_IFNET, ifp);
                    637:        (void)pfil_run_hooks(if_pfil,
1.144     yamt      638:            (struct mbuf **)PFIL_IFNET_ATTACH, ifp, PFIL_IFNET);
1.87      thorpej   639:
1.318     ozaki-r   640:        IF_AFDATA_LOCK_INIT(ifp);
                    641:
1.324     ozaki-r   642:        ifp->if_link_si = softint_establish(SOFTINT_NET, if_link_state_change_si, ifp);
                    643:        if (ifp->if_link_si == NULL)
                    644:                panic("%s: softint_establish() failed", __func__);
                    645:
1.334     ozaki-r   646:        PSLIST_ENTRY_INIT(ifp, if_pslist_entry);
                    647:        psref_target_init(&ifp->if_psref, ifnet_psref_class);
1.336     ozaki-r   648:        ifp->if_ioctl_lock = mutex_obj_alloc(MUTEX_DEFAULT, IPL_NONE);
1.334     ozaki-r   649:
                    650:        IFNET_LOCK();
1.307     ozaki-r   651:        if_getindex(ifp);
1.334     ozaki-r   652:        IFNET_UNLOCK();
1.307     ozaki-r   653: }
                    654:
                    655: /*
                    656:  * Register an interface to the list of "active" interfaces.
                    657:  */
                    658: void
                    659: if_register(ifnet_t *ifp)
                    660: {
1.336     ozaki-r   661:        /*
                    662:         * If the driver has not supplied its own if_ioctl, then
                    663:         * supply the default.
                    664:         */
                    665:        if (ifp->if_ioctl == NULL)
                    666:                ifp->if_ioctl = ifioctl_common;
1.307     ozaki-r   667:
                    668:        sysctl_sndq_setup(&ifp->if_sysctl_log, ifp->if_xname, &ifp->if_snd);
                    669:
1.152     matt      670:        if (!STAILQ_EMPTY(&domains))
1.147     tron      671:                if_attachdomain1(ifp);
                    672:
1.107     itojun    673:        /* Announce the interface. */
                    674:        rt_ifannouncemsg(ifp, IFAN_ARRIVAL);
1.296     ozaki-r   675:
                    676:        if (ifp->if_slowtimo != NULL) {
1.297     ozaki-r   677:                ifp->if_slowtimo_ch =
                    678:                    kmem_zalloc(sizeof(*ifp->if_slowtimo_ch), KM_SLEEP);
                    679:                callout_init(ifp->if_slowtimo_ch, 0);
                    680:                callout_setfunc(ifp->if_slowtimo_ch, if_slowtimo, ifp);
1.296     ozaki-r   681:                if_slowtimo(ifp);
                    682:        }
1.307     ozaki-r   683:
1.332     knakahar  684:        if (ifp->if_transmit == NULL || ifp->if_transmit == if_nulltransmit)
                    685:                ifp->if_transmit = if_transmit;
                    686:
1.334     ozaki-r   687:        IFNET_LOCK();
1.307     ozaki-r   688:        TAILQ_INSERT_TAIL(&ifnet_list, ifp, if_list);
1.334     ozaki-r   689:        IFNET_WRITER_INSERT_TAIL(ifp);
                    690:        IFNET_UNLOCK();
1.307     ozaki-r   691: }
                    692:
                    693: /*
1.323     ozaki-r   694:  * The if_percpuq framework
                    695:  *
                    696:  * It allows network device drivers to execute the network stack
                    697:  * in softint (so called softint-based if_input). It utilizes
                    698:  * softint and percpu ifqueue. It doesn't distribute any packets
                    699:  * between CPUs, unlike pktqueue(9).
                    700:  *
                    701:  * Currently we support two options for device drivers to apply the framework:
                    702:  * - Use it implicitly with less changes
                    703:  *   - If you use if_attach in driver's _attach function and if_input in
                    704:  *     driver's Rx interrupt handler, a packet is queued and a softint handles
                    705:  *     the packet implicitly
                    706:  * - Use it explicitly in each driver (recommended)
                    707:  *   - You can use if_percpuq_* directly in your driver
                    708:  *   - In this case, you need to allocate struct if_percpuq in driver's softc
                    709:  *   - See wm(4) as a reference implementation
                    710:  */
                    711:
                    712: static void
                    713: if_percpuq_softint(void *arg)
                    714: {
                    715:        struct if_percpuq *ipq = arg;
                    716:        struct ifnet *ifp = ipq->ipq_ifp;
                    717:        struct mbuf *m;
                    718:
                    719:        while ((m = if_percpuq_dequeue(ipq)) != NULL)
                    720:                ifp->_if_input(ifp, m);
                    721: }
                    722:
                    723: static void
                    724: if_percpuq_init_ifq(void *p, void *arg __unused, struct cpu_info *ci __unused)
                    725: {
                    726:        struct ifqueue *const ifq = p;
                    727:
                    728:        memset(ifq, 0, sizeof(*ifq));
                    729:        ifq->ifq_maxlen = IFQ_MAXLEN;
                    730: }
                    731:
                    732: struct if_percpuq *
                    733: if_percpuq_create(struct ifnet *ifp)
                    734: {
                    735:        struct if_percpuq *ipq;
                    736:
                    737:        ipq = kmem_zalloc(sizeof(*ipq), KM_SLEEP);
                    738:        if (ipq == NULL)
                    739:                panic("kmem_zalloc failed");
                    740:
                    741:        ipq->ipq_ifp = ifp;
                    742:        ipq->ipq_si = softint_establish(SOFTINT_NET|SOFTINT_MPSAFE,
                    743:            if_percpuq_softint, ipq);
                    744:        ipq->ipq_ifqs = percpu_alloc(sizeof(struct ifqueue));
                    745:        percpu_foreach(ipq->ipq_ifqs, &if_percpuq_init_ifq, NULL);
                    746:
1.327     knakahar  747:        sysctl_percpuq_setup(&ifp->if_sysctl_log, ifp->if_xname, ipq);
                    748:
1.323     ozaki-r   749:        return ipq;
                    750: }
                    751:
                    752: static struct mbuf *
                    753: if_percpuq_dequeue(struct if_percpuq *ipq)
                    754: {
                    755:        struct mbuf *m;
                    756:        struct ifqueue *ifq;
                    757:        int s;
                    758:
                    759:        s = splnet();
                    760:        ifq = percpu_getref(ipq->ipq_ifqs);
                    761:        IF_DEQUEUE(ifq, m);
                    762:        percpu_putref(ipq->ipq_ifqs);
                    763:        splx(s);
                    764:
                    765:        return m;
                    766: }
                    767:
                    768: static void
                    769: if_percpuq_purge_ifq(void *p, void *arg __unused, struct cpu_info *ci __unused)
                    770: {
                    771:        struct ifqueue *const ifq = p;
                    772:
                    773:        IF_PURGE(ifq);
                    774: }
                    775:
                    776: void
                    777: if_percpuq_destroy(struct if_percpuq *ipq)
                    778: {
                    779:
                    780:        /* if_detach may already destroy it */
                    781:        if (ipq == NULL)
                    782:                return;
                    783:
                    784:        softint_disestablish(ipq->ipq_si);
                    785:        percpu_foreach(ipq->ipq_ifqs, &if_percpuq_purge_ifq, NULL);
                    786:        percpu_free(ipq->ipq_ifqs, sizeof(struct ifqueue));
                    787: }
                    788:
                    789: void
                    790: if_percpuq_enqueue(struct if_percpuq *ipq, struct mbuf *m)
                    791: {
                    792:        struct ifqueue *ifq;
                    793:        int s;
                    794:
                    795:        KASSERT(ipq != NULL);
                    796:
                    797:        s = splnet();
                    798:        ifq = percpu_getref(ipq->ipq_ifqs);
                    799:        if (IF_QFULL(ifq)) {
                    800:                IF_DROP(ifq);
1.326     ozaki-r   801:                percpu_putref(ipq->ipq_ifqs);
1.323     ozaki-r   802:                m_freem(m);
                    803:                goto out;
                    804:        }
                    805:        IF_ENQUEUE(ifq, m);
                    806:        percpu_putref(ipq->ipq_ifqs);
                    807:
                    808:        softint_schedule(ipq->ipq_si);
                    809: out:
                    810:        splx(s);
                    811: }
                    812:
1.327     knakahar  813: static void
                    814: if_percpuq_drops(void *p, void *arg, struct cpu_info *ci __unused)
                    815: {
                    816:        struct ifqueue *const ifq = p;
                    817:        int *sum = arg;
                    818:
                    819:        *sum += ifq->ifq_drops;
                    820: }
                    821:
                    822: static int
                    823: sysctl_percpuq_drops_handler(SYSCTLFN_ARGS)
                    824: {
                    825:        struct sysctlnode node;
                    826:        struct if_percpuq *ipq;
                    827:        int sum = 0;
                    828:        int error;
                    829:
                    830:        node = *rnode;
                    831:        ipq = node.sysctl_data;
                    832:
                    833:        percpu_foreach(ipq->ipq_ifqs, if_percpuq_drops, &sum);
                    834:
                    835:        node.sysctl_data = &sum;
                    836:        error = sysctl_lookup(SYSCTLFN_CALL(&node));
                    837:        if (error != 0 || newp == NULL)
                    838:                return error;
                    839:
                    840:        return 0;
                    841: }
                    842:
                    843: static void
                    844: sysctl_percpuq_setup(struct sysctllog **clog, const char* ifname,
                    845:     struct if_percpuq *ipq)
                    846: {
                    847:        const struct sysctlnode *cnode, *rnode;
                    848:
                    849:        if (sysctl_createv(clog, 0, NULL, &rnode,
                    850:                       CTLFLAG_PERMANENT,
                    851:                       CTLTYPE_NODE, "interfaces",
                    852:                       SYSCTL_DESCR("Per-interface controls"),
                    853:                       NULL, 0, NULL, 0,
                    854:                       CTL_NET, CTL_CREATE, CTL_EOL) != 0)
                    855:                goto bad;
                    856:
                    857:        if (sysctl_createv(clog, 0, &rnode, &rnode,
                    858:                       CTLFLAG_PERMANENT,
                    859:                       CTLTYPE_NODE, ifname,
                    860:                       SYSCTL_DESCR("Interface controls"),
                    861:                       NULL, 0, NULL, 0,
                    862:                       CTL_CREATE, CTL_EOL) != 0)
                    863:                goto bad;
                    864:
                    865:        if (sysctl_createv(clog, 0, &rnode, &rnode,
                    866:                       CTLFLAG_PERMANENT,
                    867:                       CTLTYPE_NODE, "rcvq",
                    868:                       SYSCTL_DESCR("Interface input queue controls"),
                    869:                       NULL, 0, NULL, 0,
                    870:                       CTL_CREATE, CTL_EOL) != 0)
                    871:                goto bad;
                    872:
                    873: #ifdef NOTYET
                    874:        /* XXX Should show each per-CPU queue length? */
                    875:        if (sysctl_createv(clog, 0, &rnode, &rnode,
                    876:                       CTLFLAG_PERMANENT,
                    877:                       CTLTYPE_INT, "len",
                    878:                       SYSCTL_DESCR("Current input queue length"),
                    879:                       sysctl_percpuq_len, 0, NULL, 0,
                    880:                       CTL_CREATE, CTL_EOL) != 0)
                    881:                goto bad;
                    882:
                    883:        if (sysctl_createv(clog, 0, &rnode, &cnode,
                    884:                       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
                    885:                       CTLTYPE_INT, "maxlen",
                    886:                       SYSCTL_DESCR("Maximum allowed input queue length"),
                    887:                       sysctl_percpuq_maxlen_handler, 0, (void *)ipq, 0,
                    888:                       CTL_CREATE, CTL_EOL) != 0)
                    889:                goto bad;
                    890: #endif
                    891:
                    892:        if (sysctl_createv(clog, 0, &rnode, &cnode,
                    893:                       CTLFLAG_PERMANENT,
                    894:                       CTLTYPE_INT, "drops",
                    895:                       SYSCTL_DESCR("Total packets dropped due to full input queue"),
                    896:                       sysctl_percpuq_drops_handler, 0, (void *)ipq, 0,
                    897:                       CTL_CREATE, CTL_EOL) != 0)
                    898:                goto bad;
                    899:
                    900:        return;
                    901: bad:
                    902:        printf("%s: could not attach sysctl nodes\n", ifname);
                    903:        return;
                    904: }
                    905:
                    906:
1.323     ozaki-r   907: /*
                    908:  * The common interface input routine that is called by device drivers,
                    909:  * which should be used only when the driver's rx handler already runs
                    910:  * in softint.
                    911:  */
                    912: void
                    913: if_input(struct ifnet *ifp, struct mbuf *m)
                    914: {
                    915:
                    916:        KASSERT(ifp->if_percpuq == NULL);
                    917:        KASSERT(!cpu_intr_p());
                    918:
                    919:        ifp->_if_input(ifp, m);
                    920: }
                    921:
                    922: /*
                    923:  * DEPRECATED. Use if_initialize and if_register instead.
1.307     ozaki-r   924:  * See the above comment of if_initialize.
1.323     ozaki-r   925:  *
                    926:  * Note that it implicitly enables if_percpuq to make drivers easy to
1.333     skrll     927:  * migrate softint-based if_input without much changes. If you don't
1.323     ozaki-r   928:  * want to enable it, use if_initialize instead.
1.307     ozaki-r   929:  */
                    930: void
                    931: if_attach(ifnet_t *ifp)
                    932: {
1.323     ozaki-r   933:
1.307     ozaki-r   934:        if_initialize(ifp);
1.323     ozaki-r   935:        ifp->if_percpuq = if_percpuq_create(ifp);
1.307     ozaki-r   936:        if_register(ifp);
1.107     itojun    937: }
                    938:
                    939: void
1.163     thorpej   940: if_attachdomain(void)
1.107     itojun    941: {
                    942:        struct ifnet *ifp;
1.110     itojun    943:        int s;
1.339   ! ozaki-r   944:        int bound = curlwp_bind();
1.107     itojun    945:
1.334     ozaki-r   946:        s = pserialize_read_enter();
                    947:        IFNET_READER_FOREACH(ifp) {
                    948:                struct psref psref;
                    949:                psref_acquire(&psref, &ifp->if_psref, ifnet_psref_class);
                    950:                pserialize_read_exit(s);
1.107     itojun    951:                if_attachdomain1(ifp);
1.334     ozaki-r   952:                s = pserialize_read_enter();
                    953:                psref_release(&psref, &ifp->if_psref, ifnet_psref_class);
                    954:        }
                    955:        pserialize_read_exit(s);
1.339   ! ozaki-r   956:        curlwp_bindx(bound);
1.107     itojun    957: }
                    958:
1.302     ozaki-r   959: static void
1.163     thorpej   960: if_attachdomain1(struct ifnet *ifp)
1.107     itojun    961: {
                    962:        struct domain *dp;
1.109     itojun    963:        int s;
                    964:
                    965:        s = splnet();
1.107     itojun    966:
1.106     itojun    967:        /* address family dependent data region */
                    968:        memset(ifp->if_afdata, 0, sizeof(ifp->if_afdata));
1.152     matt      969:        DOMAIN_FOREACH(dp) {
1.185     dyoung    970:                if (dp->dom_ifattach != NULL)
1.106     itojun    971:                        ifp->if_afdata[dp->dom_family] =
                    972:                            (*dp->dom_ifattach)(ifp);
                    973:        }
1.109     itojun    974:
                    975:        splx(s);
1.1       cgd       976: }
1.53      thorpej   977:
                    978: /*
                    979:  * Deactivate an interface.  This points all of the procedure
                    980:  * handles at error stubs.  May be called from interrupt context.
                    981:  */
                    982: void
1.163     thorpej   983: if_deactivate(struct ifnet *ifp)
1.53      thorpej   984: {
                    985:        int s;
                    986:
1.88      thorpej   987:        s = splnet();
1.53      thorpej   988:
                    989:        ifp->if_output   = if_nulloutput;
1.323     ozaki-r   990:        ifp->_if_input   = if_nullinput;
1.53      thorpej   991:        ifp->if_start    = if_nullstart;
1.332     knakahar  992:        ifp->if_transmit = if_nulltransmit;
1.53      thorpej   993:        ifp->if_ioctl    = if_nullioctl;
1.75      thorpej   994:        ifp->if_init     = if_nullinit;
                    995:        ifp->if_stop     = if_nullstop;
1.295     ozaki-r   996:        ifp->if_slowtimo = if_nullslowtimo;
1.53      thorpej   997:        ifp->if_drain    = if_nulldrain;
                    998:
                    999:        /* No more packets may be enqueued. */
                   1000:        ifp->if_snd.ifq_maxlen = 0;
                   1001:
                   1002:        splx(s);
                   1003: }
                   1004:
1.206     dyoung   1005: void
1.218     dyoung   1006: if_purgeaddrs(struct ifnet *ifp, int family, void (*purgeaddr)(struct ifaddr *))
1.206     dyoung   1007: {
1.289     ozaki-r  1008:        struct ifaddr *ifa, *nifa;
1.206     dyoung   1009:
1.289     ozaki-r  1010:        IFADDR_FOREACH_SAFE(ifa, ifp, nifa) {
1.206     dyoung   1011:                if (ifa->ifa_addr->sa_family != family)
                   1012:                        continue;
                   1013:                (*purgeaddr)(ifa);
                   1014:        }
                   1015: }
                   1016:
1.53      thorpej  1017: /*
                   1018:  * Detach an interface from the list of "active" interfaces,
                   1019:  * freeing any resources as we go along.
                   1020:  *
                   1021:  * NOTE: This routine must be called with a valid thread context,
                   1022:  * as it may block.
                   1023:  */
                   1024: void
1.163     thorpej  1025: if_detach(struct ifnet *ifp)
1.53      thorpej  1026: {
1.56      thorpej  1027:        struct socket so;
1.178     dyoung   1028:        struct ifaddr *ifa;
1.53      thorpej  1029: #ifdef IFAREF_DEBUG
                   1030:        struct ifaddr *last_ifa = NULL;
                   1031: #endif
1.56      thorpej  1032:        struct domain *dp;
1.141     matt     1033:        const struct protosw *pr;
1.322     riastrad 1034:        int s, i, family, purged;
1.276     rmind    1035:        uint64_t xc;
1.53      thorpej  1036:
1.56      thorpej  1037:        /*
                   1038:         * XXX It's kind of lame that we have to have the
                   1039:         * XXX socket structure...
                   1040:         */
                   1041:        memset(&so, 0, sizeof(so));
1.53      thorpej  1042:
1.88      thorpej  1043:        s = splnet();
1.53      thorpej  1044:
1.334     ozaki-r  1045:        sysctl_teardown(&ifp->if_sysctl_log);
1.336     ozaki-r  1046:        mutex_enter(ifp->if_ioctl_lock);
                   1047:        ifp->if_ioctl = if_nullioctl;
                   1048:        mutex_exit(ifp->if_ioctl_lock);
1.334     ozaki-r  1049:
                   1050:        IFNET_LOCK();
1.319     ozaki-r  1051:        ifindex2ifnet[ifp->if_index] = NULL;
                   1052:        TAILQ_REMOVE(&ifnet_list, ifp, if_list);
1.334     ozaki-r  1053:        IFNET_WRITER_REMOVE(ifp);
                   1054:        pserialize_perform(ifnet_psz);
                   1055:        IFNET_UNLOCK();
                   1056:
                   1057:        /* Wait for all readers to drain before freeing.  */
                   1058:        psref_target_destroy(&ifp->if_psref, ifnet_psref_class);
                   1059:        PSLIST_ENTRY_DESTROY(ifp, if_pslist_entry);
1.319     ozaki-r  1060:
1.336     ozaki-r  1061:        mutex_obj_free(ifp->if_ioctl_lock);
                   1062:        ifp->if_ioctl_lock = NULL;
                   1063:
1.296     ozaki-r  1064:        if (ifp->if_slowtimo != NULL) {
1.305     martin   1065:                ifp->if_slowtimo = NULL;
1.297     ozaki-r  1066:                callout_halt(ifp->if_slowtimo_ch, NULL);
                   1067:                callout_destroy(ifp->if_slowtimo_ch);
                   1068:                kmem_free(ifp->if_slowtimo_ch, sizeof(*ifp->if_slowtimo_ch));
1.296     ozaki-r  1069:        }
                   1070:
1.53      thorpej  1071:        /*
                   1072:         * Do an if_down() to give protocols a chance to do something.
                   1073:         */
                   1074:        if_down(ifp);
1.86      thorpej  1075:
                   1076: #ifdef ALTQ
                   1077:        if (ALTQ_IS_ENABLED(&ifp->if_snd))
                   1078:                altq_disable(&ifp->if_snd);
                   1079:        if (ALTQ_IS_ATTACHED(&ifp->if_snd))
                   1080:                altq_detach(&ifp->if_snd);
1.87      thorpej  1081: #endif
                   1082:
1.285     ozaki-r  1083:        if (ifp->if_snd.ifq_lock)
                   1084:                mutex_obj_free(ifp->if_snd.ifq_lock);
                   1085:
1.166     liamjfoy 1086: #if NCARP > 0
                   1087:        /* Remove the interface from any carp group it is a part of.  */
1.185     dyoung   1088:        if (ifp->if_carp != NULL && ifp->if_type != IFT_CARP)
1.166     liamjfoy 1089:                carp_ifdetach(ifp);
                   1090: #endif
                   1091:
1.53      thorpej  1092:        /*
                   1093:         * Rip all the addresses off the interface.  This should make
                   1094:         * all of the routes go away.
1.178     dyoung   1095:         *
                   1096:         * pr_usrreq calls can remove an arbitrary number of ifaddrs
                   1097:         * from the list, including our "cursor", ifa.  For safety,
                   1098:         * and to honor the TAILQ abstraction, I just restart the
                   1099:         * loop after each removal.  Note that the loop will exit
                   1100:         * when all of the remaining ifaddrs belong to the AF_LINK
                   1101:         * family.  I am counting on the historical fact that at
                   1102:         * least one pr_usrreq in each address domain removes at
                   1103:         * least one ifaddr.
1.53      thorpej  1104:         */
1.178     dyoung   1105: again:
1.204     dyoung   1106:        IFADDR_FOREACH(ifa, ifp) {
1.56      thorpej  1107:                family = ifa->ifa_addr->sa_family;
1.53      thorpej  1108: #ifdef IFAREF_DEBUG
                   1109:                printf("if_detach: ifaddr %p, family %d, refcnt %d\n",
1.56      thorpej  1110:                    ifa, family, ifa->ifa_refcnt);
1.53      thorpej  1111:                if (last_ifa != NULL && ifa == last_ifa)
1.56      thorpej  1112:                        panic("if_detach: loop detected");
1.53      thorpej  1113:                last_ifa = ifa;
                   1114: #endif
1.178     dyoung   1115:                if (family == AF_LINK)
1.118     itojun   1116:                        continue;
                   1117:                dp = pffinddomain(family);
1.56      thorpej  1118: #ifdef DIAGNOSTIC
1.118     itojun   1119:                if (dp == NULL)
                   1120:                        panic("if_detach: no domain for AF %d",
                   1121:                            family);
1.56      thorpej  1122: #endif
1.160     gdt      1123:                /*
                   1124:                 * XXX These PURGEIF calls are redundant with the
                   1125:                 * purge-all-families calls below, but are left in for
                   1126:                 * now both to make a smaller change, and to avoid
                   1127:                 * unplanned interactions with clearing of
                   1128:                 * ifp->if_addrlist.
                   1129:                 */
1.118     itojun   1130:                purged = 0;
1.322     riastrad 1131:                for (pr = dp->dom_protosw;
                   1132:                     pr < dp->dom_protoswNPROTOSW; pr++) {
1.118     itojun   1133:                        so.so_proto = pr;
1.275     rmind    1134:                        if (pr->pr_usrreqs) {
1.290     rtr      1135:                                (void) (*pr->pr_usrreqs->pr_purgeif)(&so, ifp);
1.118     itojun   1136:                                purged = 1;
1.53      thorpej  1137:                        }
1.118     itojun   1138:                }
                   1139:                if (purged == 0) {
                   1140:                        /*
                   1141:                         * XXX What's really the best thing to do
1.135     keihan   1142:                         * XXX here?  --thorpej@NetBSD.org
1.118     itojun   1143:                         */
                   1144:                        printf("if_detach: WARNING: AF %d not purged\n",
                   1145:                            family);
1.207     dyoung   1146:                        ifa_remove(ifp, ifa);
1.53      thorpej  1147:                }
1.178     dyoung   1148:                goto again;
1.53      thorpej  1149:        }
1.118     itojun   1150:
                   1151:        if_free_sadl(ifp);
1.53      thorpej  1152:
1.180     dyoung   1153:        /* Walk the routing table looking for stragglers. */
1.243     dyoung   1154:        for (i = 0; i <= AF_MAX; i++) {
                   1155:                while (rt_walktree(i, if_rt_walktree, ifp) == ERESTART)
1.260     christos 1156:                        continue;
1.243     dyoung   1157:        }
1.106     itojun   1158:
1.152     matt     1159:        DOMAIN_FOREACH(dp) {
1.185     dyoung   1160:                if (dp->dom_ifdetach != NULL && ifp->if_afdata[dp->dom_family])
1.260     christos 1161:                {
                   1162:                        void *p = ifp->if_afdata[dp->dom_family];
                   1163:                        if (p) {
                   1164:                                ifp->if_afdata[dp->dom_family] = NULL;
                   1165:                                (*dp->dom_ifdetach)(ifp, p);
                   1166:                        }
                   1167:                }
1.160     gdt      1168:
                   1169:                /*
                   1170:                 * One would expect multicast memberships (INET and
                   1171:                 * INET6) on UDP sockets to be purged by the PURGEIF
                   1172:                 * calls above, but if all addresses were removed from
                   1173:                 * the interface prior to destruction, the calls will
                   1174:                 * not be made (e.g. ppp, for which pppd(8) generally
                   1175:                 * removes addresses before destroying the interface).
                   1176:                 * Because there is no invariant that multicast
                   1177:                 * memberships only exist for interfaces with IPv4
                   1178:                 * addresses, we must call PURGEIF regardless of
                   1179:                 * addresses.  (Protocols which might store ifnet
                   1180:                 * pointers are marked with PR_PURGEIF.)
                   1181:                 */
1.185     dyoung   1182:                for (pr = dp->dom_protosw; pr < dp->dom_protoswNPROTOSW; pr++) {
1.160     gdt      1183:                        so.so_proto = pr;
1.275     rmind    1184:                        if (pr->pr_usrreqs && pr->pr_flags & PR_PURGEIF)
1.290     rtr      1185:                                (void)(*pr->pr_usrreqs->pr_purgeif)(&so, ifp);
1.160     gdt      1186:                }
1.53      thorpej  1187:        }
1.57      thorpej  1188:
1.265     rmind    1189:        (void)pfil_run_hooks(if_pfil,
1.184     dyoung   1190:            (struct mbuf **)PFIL_IFNET_DETACH, ifp, PFIL_IFNET);
1.265     rmind    1191:        (void)pfil_head_destroy(ifp->if_pfil);
1.184     dyoung   1192:
1.57      thorpej  1193:        /* Announce that the interface is gone. */
                   1194:        rt_ifannouncemsg(ifp, IFAN_DEPARTURE);
1.93      itojun   1195:
1.320     ozaki-r  1196:        IF_AFDATA_LOCK_DESTROY(ifp);
                   1197:
1.324     ozaki-r  1198:        softint_disestablish(ifp->if_link_si);
                   1199:        ifp->if_link_si = NULL;
                   1200:
1.95      itojun   1201:        /*
1.168     matt     1202:         * remove packets that came from ifp, from software interrupt queues.
1.95      itojun   1203:         */
1.168     matt     1204:        DOMAIN_FOREACH(dp) {
                   1205:                for (i = 0; i < __arraycount(dp->dom_ifqueues); i++) {
1.260     christos 1206:                        struct ifqueue *iq = dp->dom_ifqueues[i];
                   1207:                        if (iq == NULL)
1.168     matt     1208:                                break;
1.260     christos 1209:                        dp->dom_ifqueues[i] = NULL;
                   1210:                        if_detach_queues(ifp, iq);
1.168     matt     1211:                }
                   1212:        }
1.95      itojun   1213:
1.276     rmind    1214:        /*
                   1215:         * IP queues have to be processed separately: net-queue barrier
                   1216:         * ensures that the packets are dequeued while a cross-call will
                   1217:         * ensure that the interrupts have completed. FIXME: not quite..
                   1218:         */
1.278     he       1219: #ifdef INET
1.276     rmind    1220:        pktq_barrier(ip_pktq);
1.278     he       1221: #endif
1.281     rmind    1222: #ifdef INET6
1.293     pooka    1223:        if (in6_present)
                   1224:                pktq_barrier(ip6_pktq);
1.281     rmind    1225: #endif
1.276     rmind    1226:        xc = xc_broadcast(0, (xcfunc_t)nullop, NULL, NULL);
                   1227:        xc_wait(xc);
                   1228:
1.323     ozaki-r  1229:        if (ifp->if_percpuq != NULL) {
                   1230:                if_percpuq_destroy(ifp->if_percpuq);
                   1231:                ifp->if_percpuq = NULL;
                   1232:        }
                   1233:
1.53      thorpej  1234:        splx(s);
1.95      itojun   1235: }
                   1236:
                   1237: static void
1.163     thorpej  1238: if_detach_queues(struct ifnet *ifp, struct ifqueue *q)
1.95      itojun   1239: {
                   1240:        struct mbuf *m, *prev, *next;
                   1241:
                   1242:        prev = NULL;
1.185     dyoung   1243:        for (m = q->ifq_head; m != NULL; m = next) {
1.274     rmind    1244:                KASSERT((m->m_flags & M_PKTHDR) != 0);
                   1245:
1.95      itojun   1246:                next = m->m_nextpkt;
1.338     ozaki-r  1247:                if (m->m_pkthdr.rcvif_index != ifp->if_index) {
1.96      itojun   1248:                        prev = m;
1.95      itojun   1249:                        continue;
1.96      itojun   1250:                }
1.95      itojun   1251:
1.185     dyoung   1252:                if (prev != NULL)
1.95      itojun   1253:                        prev->m_nextpkt = m->m_nextpkt;
                   1254:                else
                   1255:                        q->ifq_head = m->m_nextpkt;
                   1256:                if (q->ifq_tail == m)
                   1257:                        q->ifq_tail = prev;
                   1258:                q->ifq_len--;
                   1259:
                   1260:                m->m_nextpkt = NULL;
                   1261:                m_freem(m);
                   1262:                IF_DROP(q);
                   1263:        }
1.53      thorpej  1264: }
                   1265:
                   1266: /*
                   1267:  * Callback for a radix tree walk to delete all references to an
                   1268:  * ifnet.
                   1269:  */
1.163     thorpej  1270: static int
1.192     dyoung   1271: if_rt_walktree(struct rtentry *rt, void *v)
1.53      thorpej  1272: {
1.55      itojun   1273:        struct ifnet *ifp = (struct ifnet *)v;
1.53      thorpej  1274:        int error;
1.317     ozaki-r  1275:        struct rtentry *retrt;
1.53      thorpej  1276:
1.185     dyoung   1277:        if (rt->rt_ifp != ifp)
                   1278:                return 0;
                   1279:
                   1280:        /* Delete the entry. */
1.194     dyoung   1281:        error = rtrequest(RTM_DELETE, rt_getkey(rt), rt->rt_gateway,
1.317     ozaki-r  1282:            rt_mask(rt), rt->rt_flags, &retrt);
                   1283:        if (error == 0) {
                   1284:                KASSERT(retrt == rt);
                   1285:                KASSERT((retrt->rt_flags & RTF_UP) == 0);
                   1286:                retrt->rt_ifp = NULL;
                   1287:                rtfree(retrt);
                   1288:        } else {
1.185     dyoung   1289:                printf("%s: warning: unable to delete rtentry @ %p, "
                   1290:                    "error = %d\n", ifp->if_xname, rt, error);
1.317     ozaki-r  1291:        }
1.243     dyoung   1292:        return ERESTART;
1.53      thorpej  1293: }
                   1294:
1.1       cgd      1295: /*
1.63      thorpej  1296:  * Create a clone network interface.
                   1297:  */
1.302     ozaki-r  1298: static int
1.163     thorpej  1299: if_clone_create(const char *name)
1.63      thorpej  1300: {
                   1301:        struct if_clone *ifc;
                   1302:        int unit;
1.336     ozaki-r  1303:        struct ifnet *ifp;
                   1304:        struct psref psref;
1.63      thorpej  1305:
                   1306:        ifc = if_clone_lookup(name, &unit);
                   1307:        if (ifc == NULL)
1.185     dyoung   1308:                return EINVAL;
1.63      thorpej  1309:
1.336     ozaki-r  1310:        ifp = if_get(name, &psref);
                   1311:        if (ifp != NULL) {
                   1312:                if_put(ifp, &psref);
1.185     dyoung   1313:                return EEXIST;
1.336     ozaki-r  1314:        }
1.63      thorpej  1315:
1.185     dyoung   1316:        return (*ifc->ifc_create)(ifc, unit);
1.63      thorpej  1317: }
                   1318:
                   1319: /*
                   1320:  * Destroy a clone network interface.
                   1321:  */
1.302     ozaki-r  1322: static int
1.163     thorpej  1323: if_clone_destroy(const char *name)
1.63      thorpej  1324: {
                   1325:        struct if_clone *ifc;
                   1326:        struct ifnet *ifp;
1.336     ozaki-r  1327:        struct psref psref;
1.63      thorpej  1328:
                   1329:        ifc = if_clone_lookup(name, NULL);
                   1330:        if (ifc == NULL)
1.185     dyoung   1331:                return EINVAL;
1.63      thorpej  1332:
1.336     ozaki-r  1333:        if (ifc->ifc_destroy == NULL)
                   1334:                return EOPNOTSUPP;
                   1335:
                   1336:        ifp = if_get(name, &psref);
1.63      thorpej  1337:        if (ifp == NULL)
1.185     dyoung   1338:                return ENXIO;
1.63      thorpej  1339:
1.336     ozaki-r  1340:        /* We have to disable ioctls here */
                   1341:        mutex_enter(ifp->if_ioctl_lock);
                   1342:        ifp->if_ioctl = if_nullioctl;
                   1343:        mutex_exit(ifp->if_ioctl_lock);
                   1344:
                   1345:        /*
                   1346:         * We cannot call ifc_destroy with holding ifp.
                   1347:         * Releasing ifp here is safe thanks to if_clone_mtx.
                   1348:         */
                   1349:        if_put(ifp, &psref);
1.63      thorpej  1350:
1.185     dyoung   1351:        return (*ifc->ifc_destroy)(ifp);
1.63      thorpej  1352: }
                   1353:
                   1354: /*
                   1355:  * Look up a network interface cloner.
                   1356:  */
1.163     thorpej  1357: static struct if_clone *
                   1358: if_clone_lookup(const char *name, int *unitp)
1.63      thorpej  1359: {
                   1360:        struct if_clone *ifc;
                   1361:        const char *cp;
1.262     christos 1362:        char *dp, ifname[IFNAMSIZ + 3];
1.128     itojun   1363:        int unit;
1.63      thorpej  1364:
1.262     christos 1365:        strcpy(ifname, "if_");
1.128     itojun   1366:        /* separate interface name from unit */
1.262     christos 1367:        for (dp = ifname + 3, cp = name; cp - name < IFNAMSIZ &&
                   1368:            *cp && (*cp < '0' || *cp > '9');)
                   1369:                *dp++ = *cp++;
1.128     itojun   1370:
                   1371:        if (cp == name || cp - name == IFNAMSIZ || !*cp)
1.185     dyoung   1372:                return NULL;    /* No name or unit number */
1.262     christos 1373:        *dp++ = '\0';
1.128     itojun   1374:
1.262     christos 1375: again:
1.128     itojun   1376:        LIST_FOREACH(ifc, &if_cloners, ifc_list) {
1.262     christos 1377:                if (strcmp(ifname + 3, ifc->ifc_name) == 0)
1.128     itojun   1378:                        break;
1.63      thorpej  1379:        }
                   1380:
1.262     christos 1381:        if (ifc == NULL) {
                   1382:                if (*ifname == '\0' ||
1.267     christos 1383:                    module_autoload(ifname, MODULE_CLASS_DRIVER))
1.262     christos 1384:                        return NULL;
                   1385:                *ifname = '\0';
                   1386:                goto again;
                   1387:        }
1.63      thorpej  1388:
1.128     itojun   1389:        unit = 0;
1.129     itojun   1390:        while (cp - name < IFNAMSIZ && *cp) {
1.245     christos 1391:                if (*cp < '0' || *cp > '9' || unit >= INT_MAX / 10) {
1.63      thorpej  1392:                        /* Bogus unit number. */
1.185     dyoung   1393:                        return NULL;
1.63      thorpej  1394:                }
1.128     itojun   1395:                unit = (unit * 10) + (*cp++ - '0');
1.63      thorpej  1396:        }
                   1397:
                   1398:        if (unitp != NULL)
1.128     itojun   1399:                *unitp = unit;
1.185     dyoung   1400:        return ifc;
1.63      thorpej  1401: }
                   1402:
                   1403: /*
                   1404:  * Register a network interface cloner.
                   1405:  */
                   1406: void
1.163     thorpej  1407: if_clone_attach(struct if_clone *ifc)
1.63      thorpej  1408: {
                   1409:
                   1410:        LIST_INSERT_HEAD(&if_cloners, ifc, ifc_list);
1.67      thorpej  1411:        if_cloners_count++;
1.63      thorpej  1412: }
                   1413:
                   1414: /*
                   1415:  * Unregister a network interface cloner.
                   1416:  */
                   1417: void
1.163     thorpej  1418: if_clone_detach(struct if_clone *ifc)
1.63      thorpej  1419: {
                   1420:
                   1421:        LIST_REMOVE(ifc, ifc_list);
1.67      thorpej  1422:        if_cloners_count--;
                   1423: }
                   1424:
                   1425: /*
                   1426:  * Provide list of interface cloners to userspace.
                   1427:  */
1.315     martin   1428: int
                   1429: if_clone_list(int buf_count, char *buffer, int *total)
1.67      thorpej  1430: {
                   1431:        char outbuf[IFNAMSIZ], *dst;
                   1432:        struct if_clone *ifc;
                   1433:        int count, error = 0;
                   1434:
1.315     martin   1435:        *total = if_cloners_count;
                   1436:        if ((dst = buffer) == NULL) {
1.67      thorpej  1437:                /* Just asking how many there are. */
1.185     dyoung   1438:                return 0;
1.67      thorpej  1439:        }
                   1440:
1.315     martin   1441:        if (buf_count < 0)
1.185     dyoung   1442:                return EINVAL;
1.67      thorpej  1443:
1.315     martin   1444:        count = (if_cloners_count < buf_count) ?
                   1445:            if_cloners_count : buf_count;
1.67      thorpej  1446:
                   1447:        for (ifc = LIST_FIRST(&if_cloners); ifc != NULL && count != 0;
                   1448:             ifc = LIST_NEXT(ifc, ifc_list), count--, dst += IFNAMSIZ) {
1.175     christos 1449:                (void)strncpy(outbuf, ifc->ifc_name, sizeof(outbuf));
                   1450:                if (outbuf[sizeof(outbuf) - 1] != '\0')
                   1451:                        return ENAMETOOLONG;
1.172     christos 1452:                error = copyout(outbuf, dst, sizeof(outbuf));
1.185     dyoung   1453:                if (error != 0)
1.67      thorpej  1454:                        break;
                   1455:        }
                   1456:
1.185     dyoung   1457:        return error;
1.63      thorpej  1458: }
                   1459:
1.207     dyoung   1460: void
1.291     rmind    1461: ifaref(struct ifaddr *ifa)
                   1462: {
                   1463:        ifa->ifa_refcnt++;
                   1464: }
                   1465:
                   1466: void
                   1467: ifafree(struct ifaddr *ifa)
                   1468: {
                   1469:        KASSERT(ifa != NULL);
                   1470:        KASSERT(ifa->ifa_refcnt > 0);
                   1471:
                   1472:        if (--ifa->ifa_refcnt == 0) {
                   1473:                free(ifa, M_IFADDR);
                   1474:        }
                   1475: }
                   1476:
                   1477: void
1.207     dyoung   1478: ifa_insert(struct ifnet *ifp, struct ifaddr *ifa)
                   1479: {
                   1480:        ifa->ifa_ifp = ifp;
1.208     dyoung   1481:        TAILQ_INSERT_TAIL(&ifp->if_addrlist, ifa, ifa_list);
1.291     rmind    1482:        ifaref(ifa);
1.207     dyoung   1483: }
                   1484:
                   1485: void
                   1486: ifa_remove(struct ifnet *ifp, struct ifaddr *ifa)
                   1487: {
                   1488:        KASSERT(ifa->ifa_ifp == ifp);
                   1489:        TAILQ_REMOVE(&ifp->if_addrlist, ifa, ifa_list);
1.291     rmind    1490:        ifafree(ifa);
1.207     dyoung   1491: }
                   1492:
1.194     dyoung   1493: static inline int
                   1494: equal(const struct sockaddr *sa1, const struct sockaddr *sa2)
                   1495: {
                   1496:        return sockaddr_cmp(sa1, sa2) == 0;
                   1497: }
                   1498:
1.63      thorpej  1499: /*
1.1       cgd      1500:  * Locate an interface based on a complete address.
                   1501:  */
                   1502: /*ARGSUSED*/
                   1503: struct ifaddr *
1.163     thorpej  1504: ifa_ifwithaddr(const struct sockaddr *addr)
1.1       cgd      1505: {
1.61      augustss 1506:        struct ifnet *ifp;
                   1507:        struct ifaddr *ifa;
1.334     ozaki-r  1508:        int s;
1.1       cgd      1509:
1.334     ozaki-r  1510:        s = pserialize_read_enter();
                   1511:        IFNET_READER_FOREACH(ifp) {
1.53      thorpej  1512:                if (ifp->if_output == if_nulloutput)
1.1       cgd      1513:                        continue;
1.204     dyoung   1514:                IFADDR_FOREACH(ifa, ifp) {
1.53      thorpej  1515:                        if (ifa->ifa_addr->sa_family != addr->sa_family)
                   1516:                                continue;
                   1517:                        if (equal(addr, ifa->ifa_addr))
1.185     dyoung   1518:                                return ifa;
1.53      thorpej  1519:                        if ((ifp->if_flags & IFF_BROADCAST) &&
                   1520:                            ifa->ifa_broadaddr &&
                   1521:                            /* IP6 doesn't have broadcast */
                   1522:                            ifa->ifa_broadaddr->sa_len != 0 &&
                   1523:                            equal(ifa->ifa_broadaddr, addr))
1.185     dyoung   1524:                                return ifa;
1.53      thorpej  1525:                }
1.1       cgd      1526:        }
1.334     ozaki-r  1527:        pserialize_read_exit(s);
1.185     dyoung   1528:        return NULL;
1.1       cgd      1529: }
1.49      itojun   1530:
1.1       cgd      1531: /*
                   1532:  * Locate the point to point interface with a given destination address.
                   1533:  */
                   1534: /*ARGSUSED*/
                   1535: struct ifaddr *
1.163     thorpej  1536: ifa_ifwithdstaddr(const struct sockaddr *addr)
1.1       cgd      1537: {
1.61      augustss 1538:        struct ifnet *ifp;
                   1539:        struct ifaddr *ifa;
1.334     ozaki-r  1540:        int s;
1.1       cgd      1541:
1.334     ozaki-r  1542:        s = pserialize_read_enter();
                   1543:        IFNET_READER_FOREACH(ifp) {
1.53      thorpej  1544:                if (ifp->if_output == if_nulloutput)
                   1545:                        continue;
1.185     dyoung   1546:                if ((ifp->if_flags & IFF_POINTOPOINT) == 0)
                   1547:                        continue;
1.204     dyoung   1548:                IFADDR_FOREACH(ifa, ifp) {
1.185     dyoung   1549:                        if (ifa->ifa_addr->sa_family != addr->sa_family ||
                   1550:                            ifa->ifa_dstaddr == NULL)
                   1551:                                continue;
                   1552:                        if (equal(addr, ifa->ifa_dstaddr))
                   1553:                                return ifa;
1.53      thorpej  1554:                }
1.1       cgd      1555:        }
1.334     ozaki-r  1556:        pserialize_read_exit(s);
1.185     dyoung   1557:        return NULL;
1.1       cgd      1558: }
                   1559:
                   1560: /*
                   1561:  * Find an interface on a specific network.  If many, choice
1.15      mycroft  1562:  * is most specific found.
1.1       cgd      1563:  */
                   1564: struct ifaddr *
1.163     thorpej  1565: ifa_ifwithnet(const struct sockaddr *addr)
1.1       cgd      1566: {
1.61      augustss 1567:        struct ifnet *ifp;
                   1568:        struct ifaddr *ifa;
1.140     matt     1569:        const struct sockaddr_dl *sdl;
1.15      mycroft  1570:        struct ifaddr *ifa_maybe = 0;
1.1       cgd      1571:        u_int af = addr->sa_family;
1.171     pooka    1572:        const char *addr_data = addr->sa_data, *cplim;
1.334     ozaki-r  1573:        int s;
1.1       cgd      1574:
                   1575:        if (af == AF_LINK) {
1.195     dyoung   1576:                sdl = satocsdl(addr);
1.137     itojun   1577:                if (sdl->sdl_index && sdl->sdl_index < if_indexlim &&
                   1578:                    ifindex2ifnet[sdl->sdl_index] &&
1.316     ozaki-r  1579:                    ifindex2ifnet[sdl->sdl_index]->if_output != if_nulloutput) {
                   1580:                        return ifindex2ifnet[sdl->sdl_index]->if_dl;
                   1581:                }
1.1       cgd      1582:        }
1.51      bouyer   1583: #ifdef NETATALK
                   1584:        if (af == AF_APPLETALK) {
1.140     matt     1585:                const struct sockaddr_at *sat, *sat2;
1.158     christos 1586:                sat = (const struct sockaddr_at *)addr;
1.334     ozaki-r  1587:                s = pserialize_read_enter();
                   1588:                IFNET_READER_FOREACH(ifp) {
1.53      thorpej  1589:                        if (ifp->if_output == if_nulloutput)
                   1590:                                continue;
1.158     christos 1591:                        ifa = at_ifawithnet((const struct sockaddr_at *)addr, ifp);
1.62      bouyer   1592:                        if (ifa == NULL)
                   1593:                                continue;
                   1594:                        sat2 = (struct sockaddr_at *)ifa->ifa_addr;
                   1595:                        if (sat2->sat_addr.s_net == sat->sat_addr.s_net)
1.185     dyoung   1596:                                return ifa; /* exact match */
1.62      bouyer   1597:                        if (ifa_maybe == NULL) {
1.112     wiz      1598:                                /* else keep the if with the right range */
1.62      bouyer   1599:                                ifa_maybe = ifa;
                   1600:                        }
1.51      bouyer   1601:                }
1.334     ozaki-r  1602:                pserialize_read_exit(s);
1.185     dyoung   1603:                return ifa_maybe;
1.51      bouyer   1604:        }
                   1605: #endif
1.334     ozaki-r  1606:        s = pserialize_read_enter();
                   1607:        IFNET_READER_FOREACH(ifp) {
1.53      thorpej  1608:                if (ifp->if_output == if_nulloutput)
                   1609:                        continue;
1.204     dyoung   1610:                IFADDR_FOREACH(ifa, ifp) {
1.171     pooka    1611:                        const char *cp, *cp2, *cp3;
1.15      mycroft  1612:
                   1613:                        if (ifa->ifa_addr->sa_family != af ||
1.185     dyoung   1614:                            ifa->ifa_netmask == NULL)
1.53      thorpej  1615:  next:                         continue;
1.15      mycroft  1616:                        cp = addr_data;
                   1617:                        cp2 = ifa->ifa_addr->sa_data;
                   1618:                        cp3 = ifa->ifa_netmask->sa_data;
1.171     pooka    1619:                        cplim = (const char *)ifa->ifa_netmask +
1.53      thorpej  1620:                            ifa->ifa_netmask->sa_len;
                   1621:                        while (cp3 < cplim) {
                   1622:                                if ((*cp++ ^ *cp2++) & *cp3++) {
                   1623:                                        /* want to continue for() loop */
1.32      mrg      1624:                                        goto next;
1.53      thorpej  1625:                                }
                   1626:                        }
1.185     dyoung   1627:                        if (ifa_maybe == NULL ||
1.329     ozaki-r  1628:                            rt_refines(ifa->ifa_netmask,
                   1629:                                       ifa_maybe->ifa_netmask))
1.15      mycroft  1630:                                ifa_maybe = ifa;
                   1631:                }
1.53      thorpej  1632:        }
1.334     ozaki-r  1633:        pserialize_read_exit(s);
1.185     dyoung   1634:        return ifa_maybe;
1.26      mrg      1635: }
1.53      thorpej  1636:
1.26      mrg      1637: /*
                   1638:  * Find the interface of the addresss.
                   1639:  */
                   1640: struct ifaddr *
1.163     thorpej  1641: ifa_ifwithladdr(const struct sockaddr *addr)
1.26      mrg      1642: {
                   1643:        struct ifaddr *ia;
                   1644:
1.53      thorpej  1645:        if ((ia = ifa_ifwithaddr(addr)) || (ia = ifa_ifwithdstaddr(addr)) ||
                   1646:            (ia = ifa_ifwithnet(addr)))
1.185     dyoung   1647:                return ia;
                   1648:        return NULL;
1.1       cgd      1649: }
                   1650:
                   1651: /*
                   1652:  * Find an interface using a specific address family
                   1653:  */
                   1654: struct ifaddr *
1.163     thorpej  1655: ifa_ifwithaf(int af)
1.1       cgd      1656: {
1.61      augustss 1657:        struct ifnet *ifp;
1.334     ozaki-r  1658:        struct ifaddr *ifa = NULL;
                   1659:        int s;
1.1       cgd      1660:
1.334     ozaki-r  1661:        s = pserialize_read_enter();
                   1662:        IFNET_READER_FOREACH(ifp) {
1.53      thorpej  1663:                if (ifp->if_output == if_nulloutput)
                   1664:                        continue;
1.204     dyoung   1665:                IFADDR_FOREACH(ifa, ifp) {
1.21      mycroft  1666:                        if (ifa->ifa_addr->sa_family == af)
1.334     ozaki-r  1667:                                goto out;
1.53      thorpej  1668:                }
                   1669:        }
1.334     ozaki-r  1670: out:
                   1671:        pserialize_read_exit(s);
                   1672:        return ifa;
1.1       cgd      1673: }
                   1674:
                   1675: /*
                   1676:  * Find an interface address specific to an interface best matching
                   1677:  * a given address.
                   1678:  */
                   1679: struct ifaddr *
1.163     thorpej  1680: ifaof_ifpforaddr(const struct sockaddr *addr, struct ifnet *ifp)
1.1       cgd      1681: {
1.61      augustss 1682:        struct ifaddr *ifa;
1.140     matt     1683:        const char *cp, *cp2, *cp3;
                   1684:        const char *cplim;
1.1       cgd      1685:        struct ifaddr *ifa_maybe = 0;
                   1686:        u_int af = addr->sa_family;
                   1687:
1.53      thorpej  1688:        if (ifp->if_output == if_nulloutput)
1.185     dyoung   1689:                return NULL;
1.53      thorpej  1690:
1.1       cgd      1691:        if (af >= AF_MAX)
1.185     dyoung   1692:                return NULL;
1.53      thorpej  1693:
1.204     dyoung   1694:        IFADDR_FOREACH(ifa, ifp) {
1.1       cgd      1695:                if (ifa->ifa_addr->sa_family != af)
                   1696:                        continue;
                   1697:                ifa_maybe = ifa;
1.185     dyoung   1698:                if (ifa->ifa_netmask == NULL) {
1.1       cgd      1699:                        if (equal(addr, ifa->ifa_addr) ||
1.53      thorpej  1700:                            (ifa->ifa_dstaddr &&
                   1701:                             equal(addr, ifa->ifa_dstaddr)))
1.185     dyoung   1702:                                return ifa;
1.1       cgd      1703:                        continue;
                   1704:                }
                   1705:                cp = addr->sa_data;
                   1706:                cp2 = ifa->ifa_addr->sa_data;
                   1707:                cp3 = ifa->ifa_netmask->sa_data;
                   1708:                cplim = ifa->ifa_netmask->sa_len + (char *)ifa->ifa_netmask;
1.53      thorpej  1709:                for (; cp3 < cplim; cp3++) {
1.1       cgd      1710:                        if ((*cp++ ^ *cp2++) & *cp3)
                   1711:                                break;
1.53      thorpej  1712:                }
1.1       cgd      1713:                if (cp3 == cplim)
1.185     dyoung   1714:                        return ifa;
1.1       cgd      1715:        }
1.185     dyoung   1716:        return ifa_maybe;
1.1       cgd      1717: }
1.9       mycroft  1718:
1.1       cgd      1719: /*
                   1720:  * Default action when installing a route with a Link Level gateway.
                   1721:  * Lookup an appropriate real ifa to point to.
                   1722:  * This should be moved to /sys/net/link.c eventually.
                   1723:  */
1.15      mycroft  1724: void
1.228     dyoung   1725: link_rtrequest(int cmd, struct rtentry *rt, const struct rt_addrinfo *info)
1.1       cgd      1726: {
1.61      augustss 1727:        struct ifaddr *ifa;
1.194     dyoung   1728:        const struct sockaddr *dst;
1.15      mycroft  1729:        struct ifnet *ifp;
1.1       cgd      1730:
1.225     dyoung   1731:        if (cmd != RTM_ADD || (ifa = rt->rt_ifa) == NULL ||
                   1732:            (ifp = ifa->ifa_ifp) == NULL || (dst = rt_getkey(rt)) == NULL)
1.1       cgd      1733:                return;
1.24      christos 1734:        if ((ifa = ifaof_ifpforaddr(dst, ifp)) != NULL) {
1.176     dyoung   1735:                rt_replace_ifa(rt, ifa);
1.1       cgd      1736:                if (ifa->ifa_rtrequest && ifa->ifa_rtrequest != link_rtrequest)
1.82      itojun   1737:                        ifa->ifa_rtrequest(cmd, rt, info);
1.1       cgd      1738:        }
                   1739: }
                   1740:
                   1741: /*
1.325     roy      1742:  * bitmask macros to manage a densely packed link_state change queue.
                   1743:  * Because we need to store LINK_STATE_UNKNOWN(0), LINK_STATE_DOWN(1) and
                   1744:  * LINK_STATE_UP(2) we need 2 bits for each state change.
                   1745:  * As a state change to store is 0, treat all bits set as an unset item.
                   1746:  */
                   1747: #define LQ_ITEM_BITS           2
                   1748: #define LQ_ITEM_MASK           ((1 << LQ_ITEM_BITS) - 1)
                   1749: #define LQ_MASK(i)             (LQ_ITEM_MASK << (i) * LQ_ITEM_BITS)
                   1750: #define LINK_STATE_UNSET       LQ_ITEM_MASK
                   1751: #define LQ_ITEM(q, i)          (((q) & LQ_MASK((i))) >> (i) * LQ_ITEM_BITS)
                   1752: #define LQ_STORE(q, i, v)                                                    \
                   1753:        do {                                                                  \
                   1754:                (q) &= ~LQ_MASK((i));                                         \
                   1755:                (q) |= (v) << (i) * LQ_ITEM_BITS;                             \
                   1756:        } while (0 /* CONSTCOND */)
                   1757: #define LQ_MAX(q)              ((sizeof((q)) * NBBY) / LQ_ITEM_BITS)
                   1758: #define LQ_POP(q, v)                                                         \
                   1759:        do {                                                                  \
                   1760:                (v) = LQ_ITEM((q), 0);                                        \
                   1761:                (q) >>= LQ_ITEM_BITS;                                         \
                   1762:                (q) |= LINK_STATE_UNSET << (LQ_MAX((q)) - 1) * LQ_ITEM_BITS;  \
                   1763:        } while (0 /* CONSTCOND */)
                   1764: #define LQ_PUSH(q, v)                                                        \
                   1765:        do {                                                                  \
                   1766:                (q) >>= LQ_ITEM_BITS;                                         \
                   1767:                (q) |= (v) << (LQ_MAX((q)) - 1) * LQ_ITEM_BITS;               \
                   1768:        } while (0 /* CONSTCOND */)
                   1769: #define LQ_FIND_UNSET(q, i)                                                  \
                   1770:        for ((i) = 0; i < LQ_MAX((q)); (i)++) {                               \
                   1771:                if (LQ_ITEM((q), (i)) == LINK_STATE_UNSET)                    \
                   1772:                        break;                                                \
                   1773:        }
                   1774: /*
                   1775:  * Handle a change in the interface link state and
                   1776:  * queue notifications.
1.159     dyoung   1777:  */
                   1778: void
                   1779: if_link_state_change(struct ifnet *ifp, int link_state)
                   1780: {
1.325     roy      1781:        int s, idx;
1.263     roy      1782:
1.325     roy      1783:        /* Ensure change is to a valid state */
                   1784:        switch (link_state) {
                   1785:        case LINK_STATE_UNKNOWN:        /* FALLTHROUGH */
                   1786:        case LINK_STATE_DOWN:           /* FALLTHROUGH */
                   1787:        case LINK_STATE_UP:
                   1788:                break;
                   1789:        default:
                   1790: #ifdef DEBUG
                   1791:                printf("%s: invalid link state %d\n",
                   1792:                    ifp->if_xname, link_state);
                   1793: #endif
1.185     dyoung   1794:                return;
1.264     roy      1795:        }
1.263     roy      1796:
1.325     roy      1797:        s = splnet();
                   1798:
                   1799:        /* Find the last unset event in the queue. */
                   1800:        LQ_FIND_UNSET(ifp->if_link_queue, idx);
                   1801:
                   1802:        /*
                   1803:         * Ensure link_state doesn't match the last event in the queue.
                   1804:         * ifp->if_link_state is not checked and set here because
                   1805:         * that would present an inconsistent picture to the system.
                   1806:         */
                   1807:        if (idx != 0 &&
                   1808:            LQ_ITEM(ifp->if_link_queue, idx - 1) == (uint8_t)link_state)
                   1809:                goto out;
                   1810:
                   1811:        /* Handle queue overflow. */
                   1812:        if (idx == LQ_MAX(ifp->if_link_queue)) {
                   1813:                uint8_t lost;
                   1814:
                   1815:                /*
                   1816:                 * The DOWN state must be protected from being pushed off
                   1817:                 * the queue to ensure that userland will always be
                   1818:                 * in a sane state.
                   1819:                 * Because DOWN is protected, there is no need to protect
                   1820:                 * UNKNOWN.
                   1821:                 * It should be invalid to change from any other state to
                   1822:                 * UNKNOWN anyway ...
                   1823:                 */
                   1824:                lost = LQ_ITEM(ifp->if_link_queue, 0);
                   1825:                LQ_PUSH(ifp->if_link_queue, (uint8_t)link_state);
                   1826:                if (lost == LINK_STATE_DOWN) {
                   1827:                        lost = LQ_ITEM(ifp->if_link_queue, 0);
                   1828:                        LQ_STORE(ifp->if_link_queue, 0, LINK_STATE_DOWN);
                   1829:                }
                   1830:                printf("%s: lost link state change %s\n",
                   1831:                    ifp->if_xname,
                   1832:                    lost == LINK_STATE_UP ? "UP" :
                   1833:                    lost == LINK_STATE_DOWN ? "DOWN" :
                   1834:                    "UNKNOWN");
                   1835:        } else
                   1836:                LQ_STORE(ifp->if_link_queue, idx, (uint8_t)link_state);
                   1837:
1.324     ozaki-r  1838:        softint_schedule(ifp->if_link_si);
                   1839:
1.325     roy      1840: out:
1.324     ozaki-r  1841:        splx(s);
                   1842: }
                   1843:
1.325     roy      1844: /*
                   1845:  * Handle interface link state change notifications.
                   1846:  * Must be called at splnet().
                   1847:  */
1.324     ozaki-r  1848: static void
1.325     roy      1849: if_link_state_change0(struct ifnet *ifp, int link_state)
1.324     ozaki-r  1850: {
                   1851:        struct domain *dp;
                   1852:
1.325     roy      1853:        /* Ensure the change is still valid. */
                   1854:        if (ifp->if_link_state == link_state)
                   1855:                return;
1.324     ozaki-r  1856:
1.263     roy      1857: #ifdef DEBUG
                   1858:        log(LOG_DEBUG, "%s: link state %s (was %s)\n", ifp->if_xname,
                   1859:                link_state == LINK_STATE_UP ? "UP" :
                   1860:                link_state == LINK_STATE_DOWN ? "DOWN" :
                   1861:                "UNKNOWN",
1.325     roy      1862:                ifp->if_link_state == LINK_STATE_UP ? "UP" :
                   1863:                ifp->if_link_state == LINK_STATE_DOWN ? "DOWN" :
1.263     roy      1864:                "UNKNOWN");
                   1865: #endif
                   1866:
                   1867:        /*
                   1868:         * When going from UNKNOWN to UP, we need to mark existing
1.314     roy      1869:         * addresses as tentative and restart DAD as we may have
1.263     roy      1870:         * erroneously not found a duplicate.
                   1871:         *
                   1872:         * This needs to happen before rt_ifmsg to avoid a race where
                   1873:         * listeners would have an address and expect it to work right
                   1874:         * away.
                   1875:         */
1.312     roy      1876:        if (link_state == LINK_STATE_UP &&
1.325     roy      1877:            ifp->if_link_state == LINK_STATE_UNKNOWN)
1.312     roy      1878:        {
                   1879:                DOMAIN_FOREACH(dp) {
                   1880:                        if (dp->dom_if_link_state_change != NULL)
                   1881:                                dp->dom_if_link_state_change(ifp,
                   1882:                                    LINK_STATE_DOWN);
                   1883:                }
                   1884:        }
1.263     roy      1885:
1.325     roy      1886:        ifp->if_link_state = link_state;
                   1887:
1.159     dyoung   1888:        /* Notify that the link state has changed. */
1.185     dyoung   1889:        rt_ifmsg(ifp);
1.263     roy      1890:
1.166     liamjfoy 1891: #if NCARP > 0
1.185     dyoung   1892:        if (ifp->if_carp)
                   1893:                carp_carpdev_state(ifp);
1.166     liamjfoy 1894: #endif
1.263     roy      1895:
1.312     roy      1896:        DOMAIN_FOREACH(dp) {
                   1897:                if (dp->dom_if_link_state_change != NULL)
                   1898:                        dp->dom_if_link_state_change(ifp, link_state);
1.270     pooka    1899:        }
1.325     roy      1900: }
                   1901:
                   1902: /*
                   1903:  * Process the interface link state change queue.
                   1904:  */
                   1905: static void
                   1906: if_link_state_change_si(void *arg)
                   1907: {
                   1908:        struct ifnet *ifp = arg;
                   1909:        int s;
                   1910:        uint8_t state;
                   1911:
                   1912:        s = splnet();
                   1913:
                   1914:        /* Pop a link state change from the queue and process it. */
                   1915:        LQ_POP(ifp->if_link_queue, state);
                   1916:        if_link_state_change0(ifp, state);
                   1917:
                   1918:        /* If there is a link state change to come, schedule it. */
                   1919:        if (LQ_ITEM(ifp->if_link_queue, 0) != LINK_STATE_UNSET)
                   1920:                softint_schedule(ifp->if_link_si);
1.264     roy      1921:
                   1922:        splx(s);
1.159     dyoung   1923: }
                   1924:
                   1925: /*
1.310     roy      1926:  * Default action when installing a local route on a point-to-point
                   1927:  * interface.
                   1928:  */
                   1929: void
                   1930: p2p_rtrequest(int req, struct rtentry *rt,
                   1931:     __unused const struct rt_addrinfo *info)
                   1932: {
                   1933:        struct ifnet *ifp = rt->rt_ifp;
                   1934:        struct ifaddr *ifa, *lo0ifa;
                   1935:
                   1936:        switch (req) {
                   1937:        case RTM_ADD:
                   1938:                if ((rt->rt_flags & RTF_LOCAL) == 0)
                   1939:                        break;
                   1940:
                   1941:                IFADDR_FOREACH(ifa, ifp) {
                   1942:                        if (equal(rt_getkey(rt), ifa->ifa_addr))
                   1943:                                break;
                   1944:                }
                   1945:                if (ifa == NULL)
                   1946:                        break;
                   1947:
                   1948:                /*
                   1949:                 * Ensure lo0 has an address of the same family.
                   1950:                 */
                   1951:                IFADDR_FOREACH(lo0ifa, lo0ifp) {
                   1952:                        if (lo0ifa->ifa_addr->sa_family ==
                   1953:                            ifa->ifa_addr->sa_family)
                   1954:                                break;
                   1955:                }
                   1956:                if (lo0ifa == NULL)
                   1957:                        break;
                   1958:
                   1959:                rt->rt_ifp = lo0ifp;
                   1960:
                   1961:                /*
                   1962:                 * Make sure to set rt->rt_ifa to the interface
                   1963:                 * address we are using, otherwise we will have trouble
                   1964:                 * with source address selection.
                   1965:                 */
                   1966:                if (ifa != rt->rt_ifa)
                   1967:                        rt_replace_ifa(rt, ifa);
                   1968:                break;
                   1969:        case RTM_DELETE:
                   1970:        default:
                   1971:                break;
                   1972:        }
                   1973: }
                   1974:
                   1975: /*
1.1       cgd      1976:  * Mark an interface down and notify protocols of
                   1977:  * the transition.
1.23      mycroft  1978:  * NOTE: must be called at splsoftnet or equivalent.
1.1       cgd      1979:  */
1.15      mycroft  1980: void
1.163     thorpej  1981: if_down(struct ifnet *ifp)
1.1       cgd      1982: {
1.61      augustss 1983:        struct ifaddr *ifa;
1.312     roy      1984:        struct domain *dp;
1.1       cgd      1985:
                   1986:        ifp->if_flags &= ~IFF_UP;
1.232     christos 1987:        nanotime(&ifp->if_lastchange);
1.204     dyoung   1988:        IFADDR_FOREACH(ifa, ifp)
1.1       cgd      1989:                pfctlinput(PRC_IFDOWN, ifa->ifa_addr);
1.78      thorpej  1990:        IFQ_PURGE(&ifp->if_snd);
1.166     liamjfoy 1991: #if NCARP > 0
                   1992:        if (ifp->if_carp)
                   1993:                carp_carpdev_state(ifp);
                   1994: #endif
1.15      mycroft  1995:        rt_ifmsg(ifp);
1.312     roy      1996:        DOMAIN_FOREACH(dp) {
                   1997:                if (dp->dom_if_down)
                   1998:                        dp->dom_if_down(ifp);
                   1999:        }
1.15      mycroft  2000: }
                   2001:
                   2002: /*
                   2003:  * Mark an interface up and notify protocols of
                   2004:  * the transition.
1.23      mycroft  2005:  * NOTE: must be called at splsoftnet or equivalent.
1.15      mycroft  2006:  */
                   2007: void
1.163     thorpej  2008: if_up(struct ifnet *ifp)
1.15      mycroft  2009: {
1.24      christos 2010: #ifdef notyet
1.61      augustss 2011:        struct ifaddr *ifa;
1.24      christos 2012: #endif
1.312     roy      2013:        struct domain *dp;
1.15      mycroft  2014:
                   2015:        ifp->if_flags |= IFF_UP;
1.232     christos 2016:        nanotime(&ifp->if_lastchange);
1.15      mycroft  2017: #ifdef notyet
                   2018:        /* this has no effect on IP, and will kill all ISO connections XXX */
1.204     dyoung   2019:        IFADDR_FOREACH(ifa, ifp)
1.15      mycroft  2020:                pfctlinput(PRC_IFUP, ifa->ifa_addr);
                   2021: #endif
1.166     liamjfoy 2022: #if NCARP > 0
                   2023:        if (ifp->if_carp)
                   2024:                carp_carpdev_state(ifp);
                   2025: #endif
1.15      mycroft  2026:        rt_ifmsg(ifp);
1.312     roy      2027:        DOMAIN_FOREACH(dp) {
1.313     roy      2028:                if (dp->dom_if_up)
                   2029:                        dp->dom_if_up(ifp);
1.312     roy      2030:        }
1.1       cgd      2031: }
                   2032:
                   2033: /*
1.296     ozaki-r  2034:  * Handle interface slowtimo timer routine.  Called
                   2035:  * from softclock, we decrement timer (if set) and
1.1       cgd      2036:  * call the appropriate interface routine on expiration.
                   2037:  */
1.294     ozaki-r  2038: static void
1.177     christos 2039: if_slowtimo(void *arg)
1.1       cgd      2040: {
1.306     martin   2041:        void (*slowtimo)(struct ifnet *);
1.296     ozaki-r  2042:        struct ifnet *ifp = arg;
1.305     martin   2043:        int s;
1.1       cgd      2044:
1.306     martin   2045:        slowtimo = ifp->if_slowtimo;
                   2046:        if (__predict_false(slowtimo == NULL))
1.305     martin   2047:                return;
1.296     ozaki-r  2048:
1.305     martin   2049:        s = splnet();
1.296     ozaki-r  2050:        if (ifp->if_timer != 0 && --ifp->if_timer == 0)
1.306     martin   2051:                (*slowtimo)(ifp);
1.296     ozaki-r  2052:
1.1       cgd      2053:        splx(s);
1.305     martin   2054:
                   2055:        if (__predict_true(ifp->if_slowtimo != NULL))
                   2056:                callout_schedule(ifp->if_slowtimo_ch, hz / IFNET_SLOWHZ);
1.65      thorpej  2057: }
                   2058:
                   2059: /*
                   2060:  * Set/clear promiscuous mode on interface ifp based on the truth value
                   2061:  * of pswitch.  The calls are reference counted so that only the first
                   2062:  * "on" request actually has an effect, as does the final "off" request.
                   2063:  * Results are undefined if the "off" and "on" requests are not matched.
                   2064:  */
                   2065: int
1.163     thorpej  2066: ifpromisc(struct ifnet *ifp, int pswitch)
1.65      thorpej  2067: {
                   2068:        int pcount, ret;
1.259     dyoung   2069:        short nflags;
1.65      thorpej  2070:
                   2071:        pcount = ifp->if_pcount;
                   2072:        if (pswitch) {
                   2073:                /*
1.71      thorpej  2074:                 * Allow the device to be "placed" into promiscuous
                   2075:                 * mode even if it is not configured up.  It will
1.242     mbalmer  2076:                 * consult IFF_PROMISC when it is brought up.
1.65      thorpej  2077:                 */
1.68      pk       2078:                if (ifp->if_pcount++ != 0)
1.185     dyoung   2079:                        return 0;
1.252     dyoung   2080:                nflags = ifp->if_flags | IFF_PROMISC;
1.65      thorpej  2081:        } else {
                   2082:                if (--ifp->if_pcount > 0)
1.185     dyoung   2083:                        return 0;
1.252     dyoung   2084:                nflags = ifp->if_flags & ~IFF_PROMISC;
1.65      thorpej  2085:        }
1.252     dyoung   2086:        ret = if_flags_set(ifp, nflags);
1.65      thorpej  2087:        /* Restore interface state if not successful. */
                   2088:        if (ret != 0) {
                   2089:                ifp->if_pcount = pcount;
                   2090:        }
1.185     dyoung   2091:        return ret;
1.1       cgd      2092: }
                   2093:
                   2094: /*
                   2095:  * Map interface name to
                   2096:  * interface structure pointer.
                   2097:  */
                   2098: struct ifnet *
1.163     thorpej  2099: ifunit(const char *name)
1.1       cgd      2100: {
1.61      augustss 2101:        struct ifnet *ifp;
1.105     matt     2102:        const char *cp = name;
                   2103:        u_int unit = 0;
                   2104:        u_int i;
1.334     ozaki-r  2105:        int s;
1.105     matt     2106:
                   2107:        /*
                   2108:         * If the entire name is a number, treat it as an ifindex.
                   2109:         */
                   2110:        for (i = 0; i < IFNAMSIZ && *cp >= '0' && *cp <= '9'; i++, cp++) {
                   2111:                unit = unit * 10 + (*cp - '0');
                   2112:        }
                   2113:
                   2114:        /*
                   2115:         * If the number took all of the name, then it's a valid ifindex.
                   2116:         */
                   2117:        if (i == IFNAMSIZ || (cp != name && *cp == '\0')) {
1.137     itojun   2118:                if (unit >= if_indexlim)
1.185     dyoung   2119:                        return NULL;
1.105     matt     2120:                ifp = ifindex2ifnet[unit];
                   2121:                if (ifp == NULL || ifp->if_output == if_nulloutput)
1.185     dyoung   2122:                        return NULL;
                   2123:                return ifp;
1.105     matt     2124:        }
1.34      thorpej  2125:
1.334     ozaki-r  2126:        ifp = NULL;
                   2127:        s = pserialize_read_enter();
                   2128:        IFNET_READER_FOREACH(ifp) {
1.53      thorpej  2129:                if (ifp->if_output == if_nulloutput)
                   2130:                        continue;
                   2131:                if (strcmp(ifp->if_xname, name) == 0)
1.334     ozaki-r  2132:                        goto out;
1.53      thorpej  2133:        }
1.334     ozaki-r  2134: out:
                   2135:        pserialize_read_exit(s);
                   2136:        return ifp;
1.1       cgd      2137: }
1.49      itojun   2138:
1.335     ozaki-r  2139: /*
                   2140:  * Get a reference of an ifnet object by an interface name.
                   2141:  * The returned reference is protected by psref(9). The caller
                   2142:  * must release a returned reference by if_put after use.
                   2143:  */
                   2144: struct ifnet *
                   2145: if_get(const char *name, struct psref *psref)
                   2146: {
                   2147:        struct ifnet *ifp;
                   2148:        const char *cp = name;
                   2149:        u_int unit = 0;
                   2150:        u_int i;
                   2151:        int s;
                   2152:
                   2153:        /*
                   2154:         * If the entire name is a number, treat it as an ifindex.
                   2155:         */
                   2156:        for (i = 0; i < IFNAMSIZ && *cp >= '0' && *cp <= '9'; i++, cp++) {
                   2157:                unit = unit * 10 + (*cp - '0');
                   2158:        }
                   2159:
                   2160:        /*
                   2161:         * If the number took all of the name, then it's a valid ifindex.
                   2162:         */
                   2163:        if (i == IFNAMSIZ || (cp != name && *cp == '\0')) {
                   2164:                if (unit >= if_indexlim)
                   2165:                        return NULL;
                   2166:                ifp = ifindex2ifnet[unit];
                   2167:                if (ifp == NULL || ifp->if_output == if_nulloutput)
                   2168:                        return NULL;
                   2169:                return ifp;
                   2170:        }
                   2171:
                   2172:        ifp = NULL;
                   2173:        s = pserialize_read_enter();
                   2174:        IFNET_READER_FOREACH(ifp) {
                   2175:                if (ifp->if_output == if_nulloutput)
                   2176:                        continue;
                   2177:                if (strcmp(ifp->if_xname, name) == 0) {
                   2178:                        psref_acquire(psref, &ifp->if_psref,
                   2179:                            ifnet_psref_class);
                   2180:                        goto out;
                   2181:                }
                   2182:        }
                   2183: out:
                   2184:        pserialize_read_exit(s);
                   2185:        return ifp;
                   2186: }
                   2187:
                   2188: /*
                   2189:  * Release a reference of an ifnet object given by if_get or
                   2190:  * if_get_byindex.
                   2191:  */
                   2192: void
                   2193: if_put(const struct ifnet *ifp, struct psref *psref)
                   2194: {
                   2195:
                   2196:        psref_release(psref, &ifp->if_psref, ifnet_psref_class);
                   2197: }
                   2198:
1.250     rmind    2199: ifnet_t *
                   2200: if_byindex(u_int idx)
                   2201: {
                   2202:        return (idx < if_indexlim) ? ifindex2ifnet[idx] : NULL;
                   2203: }
                   2204:
1.335     ozaki-r  2205: /*
                   2206:  * Get a reference of an ifnet object by an interface index.
                   2207:  * The returned reference is protected by psref(9). The caller
                   2208:  * must release a returned reference by if_put after use.
                   2209:  */
                   2210: ifnet_t *
                   2211: if_get_byindex(u_int idx, struct psref *psref)
                   2212: {
                   2213:        ifnet_t *ifp;
                   2214:        int s;
                   2215:
                   2216:        s = pserialize_read_enter();
1.337     ozaki-r  2217:        ifp = (__predict_true(idx < if_indexlim)) ? ifindex2ifnet[idx] : NULL;
                   2218:        if (__predict_true(ifp != NULL))
1.335     ozaki-r  2219:                psref_acquire(psref, &ifp->if_psref, ifnet_psref_class);
                   2220:        pserialize_read_exit(s);
                   2221:
                   2222:        return ifp;
                   2223: }
                   2224:
1.338     ozaki-r  2225: /*
                   2226:  * XXX unsafe
                   2227:  */
                   2228: void
                   2229: if_acquire_unsafe(struct ifnet *ifp, struct psref *psref)
                   2230: {
                   2231:
                   2232:        KASSERT(ifp->if_index != 0);
                   2233:        KASSERT(if_byindex(ifp->if_index) != NULL);
                   2234:        psref_acquire(psref, &ifp->if_psref, ifnet_psref_class);
                   2235: }
                   2236:
                   2237: bool
                   2238: if_held(struct ifnet *ifp)
                   2239: {
                   2240:
                   2241:        return psref_held(&ifp->if_psref, ifnet_psref_class);
                   2242: }
                   2243:
                   2244:
1.211     dyoung   2245: /* common */
1.215     dyoung   2246: int
                   2247: ifioctl_common(struct ifnet *ifp, u_long cmd, void *data)
1.211     dyoung   2248: {
1.224     dyoung   2249:        int s;
1.215     dyoung   2250:        struct ifreq *ifr;
                   2251:        struct ifcapreq *ifcr;
                   2252:        struct ifdatareq *ifdr;
1.211     dyoung   2253:
                   2254:        switch (cmd) {
                   2255:        case SIOCSIFCAP:
1.215     dyoung   2256:                ifcr = data;
1.211     dyoung   2257:                if ((ifcr->ifcr_capenable & ~ifp->if_capabilities) != 0)
                   2258:                        return EINVAL;
                   2259:
1.213     dyoung   2260:                if (ifcr->ifcr_capenable == ifp->if_capenable)
                   2261:                        return 0;
1.211     dyoung   2262:
1.213     dyoung   2263:                ifp->if_capenable = ifcr->ifcr_capenable;
1.211     dyoung   2264:
1.213     dyoung   2265:                /* Pre-compute the checksum flags mask. */
                   2266:                ifp->if_csum_flags_tx = 0;
                   2267:                ifp->if_csum_flags_rx = 0;
                   2268:                if (ifp->if_capenable & IFCAP_CSUM_IPv4_Tx) {
                   2269:                        ifp->if_csum_flags_tx |= M_CSUM_IPv4;
                   2270:                }
                   2271:                if (ifp->if_capenable & IFCAP_CSUM_IPv4_Rx) {
                   2272:                        ifp->if_csum_flags_rx |= M_CSUM_IPv4;
                   2273:                }
1.211     dyoung   2274:
1.213     dyoung   2275:                if (ifp->if_capenable & IFCAP_CSUM_TCPv4_Tx) {
                   2276:                        ifp->if_csum_flags_tx |= M_CSUM_TCPv4;
                   2277:                }
                   2278:                if (ifp->if_capenable & IFCAP_CSUM_TCPv4_Rx) {
                   2279:                        ifp->if_csum_flags_rx |= M_CSUM_TCPv4;
                   2280:                }
1.211     dyoung   2281:
1.213     dyoung   2282:                if (ifp->if_capenable & IFCAP_CSUM_UDPv4_Tx) {
                   2283:                        ifp->if_csum_flags_tx |= M_CSUM_UDPv4;
                   2284:                }
                   2285:                if (ifp->if_capenable & IFCAP_CSUM_UDPv4_Rx) {
                   2286:                        ifp->if_csum_flags_rx |= M_CSUM_UDPv4;
                   2287:                }
1.211     dyoung   2288:
1.213     dyoung   2289:                if (ifp->if_capenable & IFCAP_CSUM_TCPv6_Tx) {
                   2290:                        ifp->if_csum_flags_tx |= M_CSUM_TCPv6;
                   2291:                }
                   2292:                if (ifp->if_capenable & IFCAP_CSUM_TCPv6_Rx) {
                   2293:                        ifp->if_csum_flags_rx |= M_CSUM_TCPv6;
                   2294:                }
1.211     dyoung   2295:
1.213     dyoung   2296:                if (ifp->if_capenable & IFCAP_CSUM_UDPv6_Tx) {
                   2297:                        ifp->if_csum_flags_tx |= M_CSUM_UDPv6;
1.211     dyoung   2298:                }
1.213     dyoung   2299:                if (ifp->if_capenable & IFCAP_CSUM_UDPv6_Rx) {
                   2300:                        ifp->if_csum_flags_rx |= M_CSUM_UDPv6;
                   2301:                }
1.215     dyoung   2302:                if (ifp->if_flags & IFF_UP)
                   2303:                        return ENETRESET;
                   2304:                return 0;
1.211     dyoung   2305:        case SIOCSIFFLAGS:
1.215     dyoung   2306:                ifr = data;
1.211     dyoung   2307:                if (ifp->if_flags & IFF_UP && (ifr->ifr_flags & IFF_UP) == 0) {
                   2308:                        s = splnet();
                   2309:                        if_down(ifp);
                   2310:                        splx(s);
                   2311:                }
                   2312:                if (ifr->ifr_flags & IFF_UP && (ifp->if_flags & IFF_UP) == 0) {
                   2313:                        s = splnet();
                   2314:                        if_up(ifp);
                   2315:                        splx(s);
                   2316:                }
                   2317:                ifp->if_flags = (ifp->if_flags & IFF_CANTCHANGE) |
                   2318:                        (ifr->ifr_flags &~ IFF_CANTCHANGE);
                   2319:                break;
                   2320:        case SIOCGIFFLAGS:
1.215     dyoung   2321:                ifr = data;
1.211     dyoung   2322:                ifr->ifr_flags = ifp->if_flags;
                   2323:                break;
                   2324:
                   2325:        case SIOCGIFMETRIC:
1.215     dyoung   2326:                ifr = data;
1.211     dyoung   2327:                ifr->ifr_metric = ifp->if_metric;
                   2328:                break;
                   2329:
                   2330:        case SIOCGIFMTU:
1.215     dyoung   2331:                ifr = data;
1.211     dyoung   2332:                ifr->ifr_mtu = ifp->if_mtu;
                   2333:                break;
                   2334:
                   2335:        case SIOCGIFDLT:
1.215     dyoung   2336:                ifr = data;
1.211     dyoung   2337:                ifr->ifr_dlt = ifp->if_dlt;
                   2338:                break;
                   2339:
                   2340:        case SIOCGIFCAP:
1.215     dyoung   2341:                ifcr = data;
1.211     dyoung   2342:                ifcr->ifcr_capabilities = ifp->if_capabilities;
                   2343:                ifcr->ifcr_capenable = ifp->if_capenable;
                   2344:                break;
                   2345:
                   2346:        case SIOCSIFMETRIC:
1.215     dyoung   2347:                ifr = data;
1.211     dyoung   2348:                ifp->if_metric = ifr->ifr_metric;
                   2349:                break;
                   2350:
                   2351:        case SIOCGIFDATA:
1.215     dyoung   2352:                ifdr = data;
1.211     dyoung   2353:                ifdr->ifdr_data = ifp->if_data;
                   2354:                break;
                   2355:
1.266     christos 2356:        case SIOCGIFINDEX:
                   2357:                ifr = data;
                   2358:                ifr->ifr_index = ifp->if_index;
                   2359:                break;
                   2360:
1.211     dyoung   2361:        case SIOCZIFDATA:
1.215     dyoung   2362:                ifdr = data;
1.211     dyoung   2363:                ifdr->ifdr_data = ifp->if_data;
                   2364:                /*
                   2365:                 * Assumes that the volatile counters that can be
                   2366:                 * zero'ed are at the end of if_data.
                   2367:                 */
                   2368:                memset(&ifp->if_data.ifi_ipackets, 0, sizeof(ifp->if_data) -
                   2369:                    offsetof(struct if_data, ifi_ipackets));
1.261     msaitoh  2370:                /*
                   2371:                 * The memset() clears to the bottm of if_data. In the area,
                   2372:                 * if_lastchange is included. Please be careful if new entry
                   2373:                 * will be added into if_data or rewite this.
                   2374:                 *
                   2375:                 * And also, update if_lastchnage.
                   2376:                 */
                   2377:                getnanotime(&ifp->if_lastchange);
1.211     dyoung   2378:                break;
1.215     dyoung   2379:        case SIOCSIFMTU:
                   2380:                ifr = data;
                   2381:                if (ifp->if_mtu == ifr->ifr_mtu)
                   2382:                        break;
                   2383:                ifp->if_mtu = ifr->ifr_mtu;
                   2384:                /*
                   2385:                 * If the link MTU changed, do network layer specific procedure.
                   2386:                 */
                   2387: #ifdef INET6
1.271     pooka    2388:                if (in6_present)
                   2389:                        nd6_setmtu(ifp);
1.215     dyoung   2390: #endif
                   2391:                return ENETRESET;
1.211     dyoung   2392:        default:
1.223     dyoung   2393:                return ENOTTY;
1.211     dyoung   2394:        }
                   2395:        return 0;
                   2396: }
                   2397:
1.235     dyoung   2398: int
1.284     rtr      2399: ifaddrpref_ioctl(struct socket *so, u_long cmd, void *data, struct ifnet *ifp)
1.235     dyoung   2400: {
                   2401:        struct if_addrprefreq *ifap = (struct if_addrprefreq *)data;
                   2402:        struct ifaddr *ifa;
                   2403:        const struct sockaddr *any, *sa;
                   2404:        union {
                   2405:                struct sockaddr sa;
                   2406:                struct sockaddr_storage ss;
1.236     jakllsch 2407:        } u, v;
1.235     dyoung   2408:
                   2409:        switch (cmd) {
                   2410:        case SIOCSIFADDRPREF:
1.284     rtr      2411:                if (kauth_authorize_network(curlwp->l_cred, KAUTH_NETWORK_INTERFACE,
1.235     dyoung   2412:                    KAUTH_REQ_NETWORK_INTERFACE_SETPRIV, ifp, (void *)cmd,
                   2413:                    NULL) != 0)
                   2414:                        return EPERM;
                   2415:        case SIOCGIFADDRPREF:
                   2416:                break;
                   2417:        default:
                   2418:                return EOPNOTSUPP;
                   2419:        }
                   2420:
                   2421:        /* sanity checks */
                   2422:        if (data == NULL || ifp == NULL) {
                   2423:                panic("invalid argument to %s", __func__);
                   2424:                /*NOTREACHED*/
                   2425:        }
                   2426:
                   2427:        /* address must be specified on ADD and DELETE */
                   2428:        sa = sstocsa(&ifap->ifap_addr);
                   2429:        if (sa->sa_family != sofamily(so))
                   2430:                return EINVAL;
                   2431:        if ((any = sockaddr_any(sa)) == NULL || sa->sa_len != any->sa_len)
                   2432:                return EINVAL;
                   2433:
1.236     jakllsch 2434:        sockaddr_externalize(&v.sa, sizeof(v.ss), sa);
                   2435:
1.235     dyoung   2436:        IFADDR_FOREACH(ifa, ifp) {
                   2437:                if (ifa->ifa_addr->sa_family != sa->sa_family)
                   2438:                        continue;
                   2439:                sockaddr_externalize(&u.sa, sizeof(u.ss), ifa->ifa_addr);
1.236     jakllsch 2440:                if (sockaddr_cmp(&u.sa, &v.sa) == 0)
1.235     dyoung   2441:                        break;
                   2442:        }
                   2443:        if (ifa == NULL)
                   2444:                return EADDRNOTAVAIL;
                   2445:
                   2446:        switch (cmd) {
                   2447:        case SIOCSIFADDRPREF:
                   2448:                ifa->ifa_preference = ifap->ifap_preference;
                   2449:                return 0;
                   2450:        case SIOCGIFADDRPREF:
                   2451:                /* fill in the if_laddrreq structure */
                   2452:                (void)sockaddr_copy(sstosa(&ifap->ifap_addr),
                   2453:                    sizeof(ifap->ifap_addr), ifa->ifa_addr);
                   2454:                ifap->ifap_preference = ifa->ifa_preference;
                   2455:                return 0;
                   2456:        default:
                   2457:                return EOPNOTSUPP;
                   2458:        }
                   2459: }
                   2460:
1.1       cgd      2461: /*
                   2462:  * Interface ioctls.
                   2463:  */
1.273     pooka    2464: static int
                   2465: doifioctl(struct socket *so, u_long cmd, void *data, struct lwp *l)
1.1       cgd      2466: {
1.61      augustss 2467:        struct ifnet *ifp;
                   2468:        struct ifreq *ifr;
1.217     martin   2469:        int error = 0;
1.191     christos 2470: #if defined(COMPAT_OSOCK) || defined(COMPAT_OIFREQ)
                   2471:        u_long ocmd = cmd;
                   2472: #endif
1.49      itojun   2473:        short oif_flags;
1.186     christos 2474: #ifdef COMPAT_OIFREQ
                   2475:        struct ifreq ifrb;
1.187     xtraeme  2476:        struct oifreq *oifr = NULL;
1.186     christos 2477: #endif
1.292     christos 2478:        int r;
1.336     ozaki-r  2479:        struct psref psref;
1.339   ! ozaki-r  2480:        int bound;
1.1       cgd      2481:
                   2482:        switch (cmd) {
1.186     christos 2483: #ifdef COMPAT_OIFREQ
                   2484:        case OSIOCGIFCONF:
                   2485:        case OOSIOCGIFCONF:
                   2486:                return compat_ifconf(cmd, data);
                   2487: #endif
1.232     christos 2488: #ifdef COMPAT_OIFDATA
                   2489:        case OSIOCGIFDATA:
                   2490:        case OSIOCZIFDATA:
                   2491:                return compat_ifdatareq(l, cmd, data);
                   2492: #endif
1.1       cgd      2493:        case SIOCGIFCONF:
1.185     dyoung   2494:                return ifconf(cmd, data);
1.231     dyoung   2495:        case SIOCINITIFADDR:
                   2496:                return EPERM;
1.1       cgd      2497:        }
1.191     christos 2498:
1.186     christos 2499: #ifdef COMPAT_OIFREQ
1.196     skd      2500:        cmd = compat_cvtcmd(cmd);
1.186     christos 2501:        if (cmd != ocmd) {
                   2502:                oifr = data;
                   2503:                data = ifr = &ifrb;
                   2504:                ifreqo2n(oifr, ifr);
                   2505:        } else
                   2506: #endif
                   2507:                ifr = data;
1.63      thorpej  2508:
                   2509:        switch (cmd) {
                   2510:        case SIOCIFCREATE:
                   2511:        case SIOCIFDESTROY:
1.339   ! ozaki-r  2512:                bound = curlwp_bind();
1.185     dyoung   2513:                if (l != NULL) {
1.336     ozaki-r  2514:                        ifp = if_get(ifr->ifr_name, &psref);
1.174     elad     2515:                        error = kauth_authorize_network(l->l_cred,
                   2516:                            KAUTH_NETWORK_INTERFACE,
                   2517:                            KAUTH_REQ_NETWORK_INTERFACE_SETPRIV, ifp,
                   2518:                            (void *)cmd, NULL);
1.336     ozaki-r  2519:                        if (ifp != NULL)
                   2520:                                if_put(ifp, &psref);
                   2521:                        if (error != 0) {
1.339   ! ozaki-r  2522:                                curlwp_bindx(bound);
1.151     yamt     2523:                                return error;
1.336     ozaki-r  2524:                        }
1.151     yamt     2525:                }
1.292     christos 2526:                mutex_enter(&if_clone_mtx);
                   2527:                r = (cmd == SIOCIFCREATE) ?
1.64      thorpej  2528:                        if_clone_create(ifr->ifr_name) :
1.185     dyoung   2529:                        if_clone_destroy(ifr->ifr_name);
1.292     christos 2530:                mutex_exit(&if_clone_mtx);
1.339   ! ozaki-r  2531:                curlwp_bindx(bound);
1.292     christos 2532:                return r;
1.67      thorpej  2533:
                   2534:        case SIOCIFGCLONERS:
1.315     martin   2535:                {
                   2536:                        struct if_clonereq *req = (struct if_clonereq *)data;
                   2537:                        return if_clone_list(req->ifcr_count, req->ifcr_buffer,
                   2538:                            &req->ifcr_total);
                   2539:                }
1.63      thorpej  2540:        }
                   2541:
1.339   ! ozaki-r  2542:        bound = curlwp_bind();
1.336     ozaki-r  2543:        ifp = if_get(ifr->ifr_name, &psref);
                   2544:        if (ifp == NULL) {
1.339   ! ozaki-r  2545:                curlwp_bindx(bound);
1.185     dyoung   2546:                return ENXIO;
1.336     ozaki-r  2547:        }
1.151     yamt     2548:
                   2549:        switch (cmd) {
1.233     christos 2550:        case SIOCALIFADDR:
                   2551:        case SIOCDLIFADDR:
                   2552:        case SIOCSIFADDRPREF:
1.151     yamt     2553:        case SIOCSIFFLAGS:
                   2554:        case SIOCSIFCAP:
                   2555:        case SIOCSIFMETRIC:
                   2556:        case SIOCZIFDATA:
                   2557:        case SIOCSIFMTU:
                   2558:        case SIOCSIFPHYADDR:
                   2559:        case SIOCDIFPHYADDR:
                   2560: #ifdef INET6
                   2561:        case SIOCSIFPHYADDR_IN6:
                   2562: #endif
                   2563:        case SIOCSLIFPHYADDR:
                   2564:        case SIOCADDMULTI:
                   2565:        case SIOCDELMULTI:
                   2566:        case SIOCSIFMEDIA:
1.154     perry    2567:        case SIOCSDRVSPEC:
1.196     skd      2568:        case SIOCG80211:
                   2569:        case SIOCS80211:
1.151     yamt     2570:        case SIOCS80211NWID:
                   2571:        case SIOCS80211NWKEY:
                   2572:        case SIOCS80211POWER:
                   2573:        case SIOCS80211BSSID:
                   2574:        case SIOCS80211CHANNEL:
1.249     pooka    2575:        case SIOCSLINKSTR:
1.185     dyoung   2576:                if (l != NULL) {
1.174     elad     2577:                        error = kauth_authorize_network(l->l_cred,
                   2578:                            KAUTH_NETWORK_INTERFACE,
                   2579:                            KAUTH_REQ_NETWORK_INTERFACE_SETPRIV, ifp,
                   2580:                            (void *)cmd, NULL);
1.185     dyoung   2581:                        if (error != 0)
1.336     ozaki-r  2582:                                goto out;
1.151     yamt     2583:                }
                   2584:        }
                   2585:
1.49      itojun   2586:        oif_flags = ifp->if_flags;
1.1       cgd      2587:
1.336     ozaki-r  2588:        mutex_enter(ifp->if_ioctl_lock);
                   2589:
1.231     dyoung   2590:        error = (*ifp->if_ioctl)(ifp, cmd, data);
                   2591:        if (error != ENOTTY)
                   2592:                ;
                   2593:        else if (so->so_proto == NULL)
1.252     dyoung   2594:                error = EOPNOTSUPP;
1.231     dyoung   2595:        else {
1.161     christos 2596: #ifdef COMPAT_OSOCK
1.186     christos 2597:                error = compat_ifioctl(so, ocmd, cmd, data, l);
1.161     christos 2598: #else
1.283     rtr      2599:                error = (*so->so_proto->pr_usrreqs->pr_ioctl)(so,
1.284     rtr      2600:                    cmd, data, ifp);
1.1       cgd      2601: #endif
1.49      itojun   2602:        }
1.1       cgd      2603:
1.49      itojun   2604:        if (((oif_flags ^ ifp->if_flags) & IFF_UP) != 0) {
1.312     roy      2605:                if ((ifp->if_flags & IFF_UP) != 0) {
1.217     martin   2606:                        int s = splnet();
1.312     roy      2607:                        if_up(ifp);
1.49      itojun   2608:                        splx(s);
                   2609:                }
1.1       cgd      2610:        }
1.186     christos 2611: #ifdef COMPAT_OIFREQ
                   2612:        if (cmd != ocmd)
1.246     christos 2613:                ifreqn2o(oifr, ifr);
1.186     christos 2614: #endif
1.49      itojun   2615:
1.336     ozaki-r  2616:        mutex_exit(ifp->if_ioctl_lock);
                   2617: out:
                   2618:        if_put(ifp, &psref);
1.339   ! ozaki-r  2619:        curlwp_bindx(bound);
1.185     dyoung   2620:        return error;
1.1       cgd      2621: }
                   2622:
                   2623: /*
                   2624:  * Return interface configuration
                   2625:  * of system.  List may be used
                   2626:  * in later ioctl's (above) to get
                   2627:  * other information.
1.200     gdt      2628:  *
                   2629:  * Each record is a struct ifreq.  Before the addition of
                   2630:  * sockaddr_storage, the API rule was that sockaddr flavors that did
                   2631:  * not fit would extend beyond the struct ifreq, with the next struct
                   2632:  * ifreq starting sa_len beyond the struct sockaddr.  Because the
                   2633:  * union in struct ifreq includes struct sockaddr_storage, every kind
                   2634:  * of sockaddr must fit.  Thus, there are no longer any overlength
                   2635:  * records.
                   2636:  *
                   2637:  * Records are added to the user buffer if they fit, and ifc_len is
                   2638:  * adjusted to the length that was written.  Thus, the user is only
                   2639:  * assured of getting the complete list if ifc_len on return is at
                   2640:  * least sizeof(struct ifreq) less than it was on entry.
                   2641:  *
                   2642:  * If the user buffer pointer is NULL, this routine copies no data and
                   2643:  * returns the amount of space that would be needed.
                   2644:  *
                   2645:  * Invariants:
                   2646:  * ifrp points to the next part of the user's buffer to be used.  If
                   2647:  * ifrp != NULL, space holds the number of bytes remaining that we may
                   2648:  * write at ifrp.  Otherwise, space holds the number of bytes that
                   2649:  * would have been written had there been adequate space.
1.1       cgd      2650:  */
                   2651: /*ARGSUSED*/
1.302     ozaki-r  2652: static int
1.183     christos 2653: ifconf(u_long cmd, void *data)
1.1       cgd      2654: {
1.61      augustss 2655:        struct ifconf *ifc = (struct ifconf *)data;
                   2656:        struct ifnet *ifp;
                   2657:        struct ifaddr *ifa;
1.304     ozaki-r  2658:        struct ifreq ifr, *ifrp = NULL;
                   2659:        int space = 0, error = 0;
1.200     gdt      2660:        const int sz = (int)sizeof(struct ifreq);
1.304     ozaki-r  2661:        const bool docopy = ifc->ifc_req != NULL;
1.334     ozaki-r  2662:        int s;
1.339   ! ozaki-r  2663:        int bound;
1.334     ozaki-r  2664:        struct psref psref;
1.1       cgd      2665:
1.304     ozaki-r  2666:        if (docopy) {
1.190     enami    2667:                space = ifc->ifc_len;
1.304     ozaki-r  2668:                ifrp = ifc->ifc_req;
                   2669:        }
                   2670:
1.339   ! ozaki-r  2671:        bound = curlwp_bind();
1.334     ozaki-r  2672:        s = pserialize_read_enter();
                   2673:        IFNET_READER_FOREACH(ifp) {
                   2674:                psref_acquire(&psref, &ifp->if_psref, ifnet_psref_class);
                   2675:                pserialize_read_exit(s);
                   2676:
1.175     christos 2677:                (void)strncpy(ifr.ifr_name, ifp->if_xname,
1.173     christos 2678:                    sizeof(ifr.ifr_name));
1.334     ozaki-r  2679:                if (ifr.ifr_name[sizeof(ifr.ifr_name) - 1] != '\0') {
                   2680:                        error = ENAMETOOLONG;
                   2681:                        goto release_exit;
                   2682:                }
1.205     dyoung   2683:                if (IFADDR_EMPTY(ifp)) {
1.200     gdt      2684:                        /* Interface with no addresses - send zero sockaddr. */
1.127     christos 2685:                        memset(&ifr.ifr_addr, 0, sizeof(ifr.ifr_addr));
1.304     ozaki-r  2686:                        if (!docopy) {
1.218     dyoung   2687:                                space += sz;
                   2688:                                continue;
                   2689:                        }
                   2690:                        if (space >= sz) {
                   2691:                                error = copyout(&ifr, ifrp, sz);
                   2692:                                if (error != 0)
1.334     ozaki-r  2693:                                        goto release_exit;
1.218     dyoung   2694:                                ifrp++;
                   2695:                                space -= sz;
1.70      mellon   2696:                        }
1.127     christos 2697:                }
                   2698:
1.204     dyoung   2699:                IFADDR_FOREACH(ifa, ifp) {
1.61      augustss 2700:                        struct sockaddr *sa = ifa->ifa_addr;
1.200     gdt      2701:                        /* all sockaddrs must fit in sockaddr_storage */
                   2702:                        KASSERT(sa->sa_len <= sizeof(ifr.ifr_ifru));
                   2703:
1.304     ozaki-r  2704:                        if (!docopy) {
1.218     dyoung   2705:                                space += sz;
                   2706:                                continue;
                   2707:                        }
                   2708:                        memcpy(&ifr.ifr_space, sa, sa->sa_len);
                   2709:                        if (space >= sz) {
                   2710:                                error = copyout(&ifr, ifrp, sz);
                   2711:                                if (error != 0)
1.334     ozaki-r  2712:                                        goto release_exit;
1.218     dyoung   2713:                                ifrp++; space -= sz;
1.1       cgd      2714:                        }
                   2715:                }
1.334     ozaki-r  2716:
                   2717:                s = pserialize_read_enter();
                   2718:                psref_release(&psref, &ifp->if_psref, ifnet_psref_class);
1.1       cgd      2719:        }
1.334     ozaki-r  2720:        pserialize_read_exit(s);
1.339   ! ozaki-r  2721:        curlwp_bindx(bound);
1.334     ozaki-r  2722:
1.304     ozaki-r  2723:        if (docopy) {
1.200     gdt      2724:                KASSERT(0 <= space && space <= ifc->ifc_len);
1.127     christos 2725:                ifc->ifc_len -= space;
1.218     dyoung   2726:        } else {
1.200     gdt      2727:                KASSERT(space >= 0);
                   2728:                ifc->ifc_len = space;
                   2729:        }
1.190     enami    2730:        return (0);
1.334     ozaki-r  2731:
                   2732: release_exit:
                   2733:        psref_release(&psref, &ifp->if_psref, ifnet_psref_class);
1.339   ! ozaki-r  2734:        curlwp_bindx(bound);
1.334     ozaki-r  2735:        return error;
1.1       cgd      2736: }
1.133     jonathan 2737:
1.198     dyoung   2738: int
1.247     christos 2739: ifreq_setaddr(u_long cmd, struct ifreq *ifr, const struct sockaddr *sa)
1.198     dyoung   2740: {
                   2741:        uint8_t len;
1.247     christos 2742: #ifdef COMPAT_OIFREQ
                   2743:        struct ifreq ifrb;
                   2744:        struct oifreq *oifr = NULL;
                   2745:        u_long ocmd = cmd;
                   2746:        cmd = compat_cvtcmd(cmd);
                   2747:        if (cmd != ocmd) {
                   2748:                oifr = (struct oifreq *)(void *)ifr;
                   2749:                ifr = &ifrb;
                   2750:                ifreqo2n(oifr, ifr);
                   2751:                len = sizeof(oifr->ifr_addr);
                   2752:        } else
                   2753: #endif
                   2754:                len = sizeof(ifr->ifr_ifru.ifru_space);
1.198     dyoung   2755:
                   2756:        if (len < sa->sa_len)
                   2757:                return EFBIG;
1.247     christos 2758:
1.241     joerg    2759:        memset(&ifr->ifr_addr, 0, len);
1.202     dyoung   2760:        sockaddr_copy(&ifr->ifr_addr, len, sa);
1.247     christos 2761:
                   2762: #ifdef COMPAT_OIFREQ
                   2763:        if (cmd != ocmd)
                   2764:                ifreqn2o(oifr, ifr);
                   2765: #endif
1.198     dyoung   2766:        return 0;
                   2767: }
                   2768:
1.155     christos 2769: /*
1.332     knakahar 2770:  * wrapper function for the drivers which doesn't have if_transmit().
1.155     christos 2771:  */
                   2772: int
1.332     knakahar 2773: if_transmit(struct ifnet *ifp, struct mbuf *m)
1.155     christos 2774: {
1.332     knakahar 2775:        int s, error;
                   2776:
                   2777:        s = splnet();
1.155     christos 2778:
1.330     knakahar 2779:        IFQ_ENQUEUE(&ifp->if_snd, m, error);
1.332     knakahar 2780:        if (error != 0) {
                   2781:                /* mbuf is already freed */
1.185     dyoung   2782:                goto out;
1.332     knakahar 2783:        }
                   2784:
                   2785:        ifp->if_obytes += m->m_pkthdr.len;;
                   2786:        if (m->m_flags & M_MCAST)
1.155     christos 2787:                ifp->if_omcasts++;
1.332     knakahar 2788:
1.155     christos 2789:        if ((ifp->if_flags & IFF_OACTIVE) == 0)
                   2790:                (*ifp->if_start)(ifp);
1.185     dyoung   2791: out:
1.155     christos 2792:        splx(s);
1.332     knakahar 2793:
1.155     christos 2794:        return error;
                   2795: }
                   2796:
                   2797: /*
1.332     knakahar 2798:  * Queue message on interface, and start output if interface
                   2799:  * not yet active.
                   2800:  */
                   2801: int
                   2802: ifq_enqueue(struct ifnet *ifp, struct mbuf *m)
                   2803: {
                   2804:
                   2805:        return (*ifp->if_transmit)(ifp, m);
                   2806: }
                   2807:
                   2808: /*
1.155     christos 2809:  * Queue message on interface, possibly using a second fast queue
                   2810:  */
                   2811: int
1.330     knakahar 2812: ifq_enqueue2(struct ifnet *ifp, struct ifqueue *ifq, struct mbuf *m)
1.155     christos 2813: {
                   2814:        int error = 0;
                   2815:
                   2816:        if (ifq != NULL
                   2817: #ifdef ALTQ
                   2818:            && ALTQ_IS_ENABLED(&ifp->if_snd) == 0
                   2819: #endif
                   2820:            ) {
                   2821:                if (IF_QFULL(ifq)) {
                   2822:                        IF_DROP(&ifp->if_snd);
                   2823:                        m_freem(m);
                   2824:                        if (error == 0)
                   2825:                                error = ENOBUFS;
1.185     dyoung   2826:                } else
1.155     christos 2827:                        IF_ENQUEUE(ifq, m);
                   2828:        } else
1.330     knakahar 2829:                IFQ_ENQUEUE(&ifp->if_snd, m, error);
1.155     christos 2830:        if (error != 0) {
                   2831:                ++ifp->if_oerrors;
                   2832:                return error;
                   2833:        }
                   2834:        return 0;
                   2835: }
                   2836:
1.252     dyoung   2837: int
                   2838: if_addr_init(ifnet_t *ifp, struct ifaddr *ifa, const bool src)
                   2839: {
                   2840:        int rc;
                   2841:
                   2842:        if (ifp->if_initaddr != NULL)
                   2843:                rc = (*ifp->if_initaddr)(ifp, ifa, src);
                   2844:        else if (src ||
1.336     ozaki-r  2845:                /* FIXME: may not hold if_ioctl_lock */
1.252     dyoung   2846:                 (rc = (*ifp->if_ioctl)(ifp, SIOCSIFDSTADDR, ifa)) == ENOTTY)
                   2847:                rc = (*ifp->if_ioctl)(ifp, SIOCINITIFADDR, ifa);
                   2848:
                   2849:        return rc;
                   2850: }
                   2851:
                   2852: int
1.309     roy      2853: if_do_dad(struct ifnet *ifp)
                   2854: {
                   2855:        if ((ifp->if_flags & IFF_LOOPBACK) != 0)
                   2856:                return 0;
                   2857:
                   2858:        switch (ifp->if_type) {
                   2859:        case IFT_FAITH:
                   2860:                /*
                   2861:                 * These interfaces do not have the IFF_LOOPBACK flag,
                   2862:                 * but loop packets back.  We do not have to do DAD on such
                   2863:                 * interfaces.  We should even omit it, because loop-backed
                   2864:                 * responses would confuse the DAD procedure.
                   2865:                 */
                   2866:                return 0;
                   2867:        default:
                   2868:                /*
                   2869:                 * Our DAD routine requires the interface up and running.
                   2870:                 * However, some interfaces can be up before the RUNNING
                   2871:                 * status.  Additionaly, users may try to assign addresses
                   2872:                 * before the interface becomes up (or running).
                   2873:                 * We simply skip DAD in such a case as a work around.
                   2874:                 * XXX: we should rather mark "tentative" on such addresses,
                   2875:                 * and do DAD after the interface becomes ready.
                   2876:                 */
                   2877:                if ((ifp->if_flags & (IFF_UP|IFF_RUNNING)) !=
                   2878:                    (IFF_UP|IFF_RUNNING))
                   2879:                        return 0;
                   2880:
                   2881:                return 1;
                   2882:        }
                   2883: }
                   2884:
                   2885: int
1.252     dyoung   2886: if_flags_set(ifnet_t *ifp, const short flags)
                   2887: {
                   2888:        int rc;
                   2889:
                   2890:        if (ifp->if_setflags != NULL)
                   2891:                rc = (*ifp->if_setflags)(ifp, flags);
                   2892:        else {
1.259     dyoung   2893:                short cantflags, chgdflags;
1.256     dyoung   2894:                struct ifreq ifr;
                   2895:
1.259     dyoung   2896:                chgdflags = ifp->if_flags ^ flags;
                   2897:                cantflags = chgdflags & IFF_CANTCHANGE;
1.256     dyoung   2898:
                   2899:                if (cantflags != 0)
                   2900:                        ifp->if_flags ^= cantflags;
                   2901:
1.259     dyoung   2902:                 /* Traditionally, we do not call if_ioctl after
                   2903:                  * setting/clearing only IFF_PROMISC if the interface
                   2904:                  * isn't IFF_UP.  Uphold that tradition.
                   2905:                 */
                   2906:                if (chgdflags == IFF_PROMISC && (ifp->if_flags & IFF_UP) == 0)
                   2907:                        return 0;
                   2908:
                   2909:                memset(&ifr, 0, sizeof(ifr));
                   2910:
1.256     dyoung   2911:                ifr.ifr_flags = flags & ~IFF_CANTCHANGE;
1.336     ozaki-r  2912:                /* FIXME: may not hold if_ioctl_lock */
1.252     dyoung   2913:                rc = (*ifp->if_ioctl)(ifp, SIOCSIFFLAGS, &ifr);
1.256     dyoung   2914:
                   2915:                if (rc != 0 && cantflags != 0)
                   2916:                        ifp->if_flags ^= cantflags;
1.252     dyoung   2917:        }
                   2918:
                   2919:        return rc;
                   2920: }
                   2921:
                   2922: int
                   2923: if_mcast_op(ifnet_t *ifp, const unsigned long cmd, const struct sockaddr *sa)
                   2924: {
                   2925:        int rc;
                   2926:        struct ifreq ifr;
                   2927:
                   2928:        if (ifp->if_mcastop != NULL)
                   2929:                rc = (*ifp->if_mcastop)(ifp, cmd, sa);
                   2930:        else {
                   2931:                ifreq_setaddr(cmd, &ifr, sa);
                   2932:                rc = (*ifp->if_ioctl)(ifp, cmd, &ifr);
                   2933:        }
                   2934:
                   2935:        return rc;
                   2936: }
1.155     christos 2937:
1.234     dyoung   2938: static void
                   2939: sysctl_sndq_setup(struct sysctllog **clog, const char *ifname,
                   2940:     struct ifaltq *ifq)
                   2941: {
                   2942:        const struct sysctlnode *cnode, *rnode;
                   2943:
                   2944:        if (sysctl_createv(clog, 0, NULL, &rnode,
                   2945:                       CTLFLAG_PERMANENT,
                   2946:                       CTLTYPE_NODE, "interfaces",
                   2947:                       SYSCTL_DESCR("Per-interface controls"),
                   2948:                       NULL, 0, NULL, 0,
1.272     pooka    2949:                       CTL_NET, CTL_CREATE, CTL_EOL) != 0)
1.234     dyoung   2950:                goto bad;
                   2951:
                   2952:        if (sysctl_createv(clog, 0, &rnode, &rnode,
                   2953:                       CTLFLAG_PERMANENT,
                   2954:                       CTLTYPE_NODE, ifname,
                   2955:                       SYSCTL_DESCR("Interface controls"),
                   2956:                       NULL, 0, NULL, 0,
                   2957:                       CTL_CREATE, CTL_EOL) != 0)
                   2958:                goto bad;
                   2959:
                   2960:        if (sysctl_createv(clog, 0, &rnode, &rnode,
                   2961:                       CTLFLAG_PERMANENT,
                   2962:                       CTLTYPE_NODE, "sndq",
                   2963:                       SYSCTL_DESCR("Interface output queue controls"),
                   2964:                       NULL, 0, NULL, 0,
                   2965:                       CTL_CREATE, CTL_EOL) != 0)
                   2966:                goto bad;
                   2967:
                   2968:        if (sysctl_createv(clog, 0, &rnode, &cnode,
                   2969:                       CTLFLAG_PERMANENT,
                   2970:                       CTLTYPE_INT, "len",
                   2971:                       SYSCTL_DESCR("Current output queue length"),
                   2972:                       NULL, 0, &ifq->ifq_len, 0,
                   2973:                       CTL_CREATE, CTL_EOL) != 0)
                   2974:                goto bad;
                   2975:
                   2976:        if (sysctl_createv(clog, 0, &rnode, &cnode,
                   2977:                       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
                   2978:                       CTLTYPE_INT, "maxlen",
                   2979:                       SYSCTL_DESCR("Maximum allowed output queue length"),
                   2980:                       NULL, 0, &ifq->ifq_maxlen, 0,
                   2981:                       CTL_CREATE, CTL_EOL) != 0)
                   2982:                goto bad;
                   2983:
                   2984:        if (sysctl_createv(clog, 0, &rnode, &cnode,
                   2985:                       CTLFLAG_PERMANENT,
                   2986:                       CTLTYPE_INT, "drops",
                   2987:                       SYSCTL_DESCR("Packets dropped due to full output queue"),
                   2988:                       NULL, 0, &ifq->ifq_drops, 0,
                   2989:                       CTL_CREATE, CTL_EOL) != 0)
                   2990:                goto bad;
                   2991:
                   2992:        return;
                   2993: bad:
                   2994:        printf("%s: could not attach sysctl nodes\n", ifname);
                   2995:        return;
                   2996: }
                   2997:
1.138     drochner 2998: #if defined(INET) || defined(INET6)
1.276     rmind    2999:
                   3000: #define        SYSCTL_NET_PKTQ(q, cn, c)                                       \
                   3001:        static int                                                      \
                   3002:        sysctl_net_##q##_##cn(SYSCTLFN_ARGS)                            \
                   3003:        {                                                               \
                   3004:                return sysctl_pktq_count(SYSCTLFN_CALL(rnode), q, c);   \
                   3005:        }
                   3006:
                   3007: #if defined(INET)
1.279     rmind    3008: static int
                   3009: sysctl_net_ip_pktq_maxlen(SYSCTLFN_ARGS)
                   3010: {
                   3011:        return sysctl_pktq_maxlen(SYSCTLFN_CALL(rnode), ip_pktq);
                   3012: }
1.276     rmind    3013: SYSCTL_NET_PKTQ(ip_pktq, items, PKTQ_NITEMS)
                   3014: SYSCTL_NET_PKTQ(ip_pktq, drops, PKTQ_DROPS)
                   3015: #endif
1.279     rmind    3016:
1.276     rmind    3017: #if defined(INET6)
1.279     rmind    3018: static int
                   3019: sysctl_net_ip6_pktq_maxlen(SYSCTLFN_ARGS)
                   3020: {
                   3021:        return sysctl_pktq_maxlen(SYSCTLFN_CALL(rnode), ip6_pktq);
                   3022: }
1.276     rmind    3023: SYSCTL_NET_PKTQ(ip6_pktq, items, PKTQ_NITEMS)
                   3024: SYSCTL_NET_PKTQ(ip6_pktq, drops, PKTQ_DROPS)
                   3025: #endif
                   3026:
1.136     atatat   3027: static void
1.276     rmind    3028: sysctl_net_pktq_setup(struct sysctllog **clog, int pf)
1.136     atatat   3029: {
1.276     rmind    3030:        sysctlfn len_func = NULL, maxlen_func = NULL, drops_func = NULL;
                   3031:        const char *pfname = NULL, *ipname = NULL;
                   3032:        int ipn = 0, qid = 0;
                   3033:
                   3034:        switch (pf) {
                   3035: #if defined(INET)
                   3036:        case PF_INET:
                   3037:                len_func = sysctl_net_ip_pktq_items;
                   3038:                maxlen_func = sysctl_net_ip_pktq_maxlen;
                   3039:                drops_func = sysctl_net_ip_pktq_drops;
                   3040:                pfname = "inet", ipn = IPPROTO_IP;
                   3041:                ipname = "ip", qid = IPCTL_IFQ;
                   3042:                break;
                   3043: #endif
                   3044: #if defined(INET6)
                   3045:        case PF_INET6:
                   3046:                len_func = sysctl_net_ip6_pktq_items;
                   3047:                maxlen_func = sysctl_net_ip6_pktq_maxlen;
                   3048:                drops_func = sysctl_net_ip6_pktq_drops;
                   3049:                pfname = "inet6", ipn = IPPROTO_IPV6;
                   3050:                ipname = "ip6", qid = IPV6CTL_IFQ;
                   3051:                break;
                   3052: #endif
                   3053:        default:
                   3054:                KASSERT(false);
                   3055:        }
1.136     atatat   3056:
1.139     atatat   3057:        sysctl_createv(clog, 0, NULL, NULL,
                   3058:                       CTLFLAG_PERMANENT,
1.136     atatat   3059:                       CTLTYPE_NODE, pfname, NULL,
                   3060:                       NULL, 0, NULL, 0,
                   3061:                       CTL_NET, pf, CTL_EOL);
1.139     atatat   3062:        sysctl_createv(clog, 0, NULL, NULL,
                   3063:                       CTLFLAG_PERMANENT,
1.136     atatat   3064:                       CTLTYPE_NODE, ipname, NULL,
                   3065:                       NULL, 0, NULL, 0,
                   3066:                       CTL_NET, pf, ipn, CTL_EOL);
1.139     atatat   3067:        sysctl_createv(clog, 0, NULL, NULL,
                   3068:                       CTLFLAG_PERMANENT,
1.142     atatat   3069:                       CTLTYPE_NODE, "ifq",
                   3070:                       SYSCTL_DESCR("Protocol input queue controls"),
1.139     atatat   3071:                       NULL, 0, NULL, 0,
                   3072:                       CTL_NET, pf, ipn, qid, CTL_EOL);
1.136     atatat   3073:
1.139     atatat   3074:        sysctl_createv(clog, 0, NULL, NULL,
                   3075:                       CTLFLAG_PERMANENT,
1.142     atatat   3076:                       CTLTYPE_INT, "len",
                   3077:                       SYSCTL_DESCR("Current input queue length"),
1.276     rmind    3078:                       len_func, 0, NULL, 0,
1.136     atatat   3079:                       CTL_NET, pf, ipn, qid, IFQCTL_LEN, CTL_EOL);
1.139     atatat   3080:        sysctl_createv(clog, 0, NULL, NULL,
                   3081:                       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
1.142     atatat   3082:                       CTLTYPE_INT, "maxlen",
                   3083:                       SYSCTL_DESCR("Maximum allowed input queue length"),
1.276     rmind    3084:                       maxlen_func, 0, NULL, 0,
1.136     atatat   3085:                       CTL_NET, pf, ipn, qid, IFQCTL_MAXLEN, CTL_EOL);
1.139     atatat   3086:        sysctl_createv(clog, 0, NULL, NULL,
                   3087:                       CTLFLAG_PERMANENT,
1.142     atatat   3088:                       CTLTYPE_INT, "drops",
                   3089:                       SYSCTL_DESCR("Packets dropped due to full input queue"),
1.276     rmind    3090:                       drops_func, 0, NULL, 0,
1.136     atatat   3091:                       CTL_NET, pf, ipn, qid, IFQCTL_DROPS, CTL_EOL);
                   3092: }
1.138     drochner 3093: #endif /* INET || INET6 */
1.280     joerg    3094:
                   3095: static int
                   3096: if_sdl_sysctl(SYSCTLFN_ARGS)
                   3097: {
                   3098:        struct ifnet *ifp;
                   3099:        const struct sockaddr_dl *sdl;
                   3100:
                   3101:        if (namelen != 1)
                   3102:                return EINVAL;
                   3103:
                   3104:        ifp = if_byindex(name[0]);
                   3105:        if (ifp == NULL)
                   3106:                return ENODEV;
                   3107:
                   3108:        sdl = ifp->if_sadl;
                   3109:        if (sdl == NULL) {
                   3110:                *oldlenp = 0;
                   3111:                return 0;
                   3112:        }
                   3113:
                   3114:        if (oldp == NULL) {
                   3115:                *oldlenp = sdl->sdl_alen;
                   3116:                return 0;
                   3117:        }
                   3118:
                   3119:        if (*oldlenp >= sdl->sdl_alen)
                   3120:                *oldlenp = sdl->sdl_alen;
                   3121:        return sysctl_copyout(l, &sdl->sdl_data[sdl->sdl_nlen], oldp, *oldlenp);
                   3122: }
                   3123:
                   3124: SYSCTL_SETUP(sysctl_net_sdl_setup, "sysctl net.sdl subtree setup")
                   3125: {
                   3126:        const struct sysctlnode *rnode = NULL;
                   3127:
                   3128:        sysctl_createv(clog, 0, NULL, &rnode,
                   3129:                       CTLFLAG_PERMANENT,
                   3130:                       CTLTYPE_NODE, "sdl",
                   3131:                       SYSCTL_DESCR("Get active link-layer address"),
                   3132:                       if_sdl_sysctl, 0, NULL, 0,
                   3133:                       CTL_NET, CTL_CREATE, CTL_EOL);
                   3134: }

CVSweb <webmaster@jp.NetBSD.org>