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

1.337   ! ozaki-r     1: /*     $NetBSD: if.c,v 1.336 2016/05/16 01:16:24 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.337   ! ozaki-r    93: __KERNEL_RCSID(0, "$NetBSD: if.c,v 1.336 2016/05/16 01:16:24 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.334     ozaki-r   944:        int bound = curlwp->l_pflag & LP_BOUND;
1.107     itojun    945:
1.334     ozaki-r   946:        curlwp->l_pflag |= LP_BOUND;
                    947:        s = pserialize_read_enter();
                    948:        IFNET_READER_FOREACH(ifp) {
                    949:                struct psref psref;
                    950:                psref_acquire(&psref, &ifp->if_psref, ifnet_psref_class);
                    951:                pserialize_read_exit(s);
1.107     itojun    952:                if_attachdomain1(ifp);
1.334     ozaki-r   953:                s = pserialize_read_enter();
                    954:                psref_release(&psref, &ifp->if_psref, ifnet_psref_class);
                    955:        }
                    956:        pserialize_read_exit(s);
                    957:        curlwp->l_pflag ^= bound ^ LP_BOUND;
1.107     itojun    958: }
                    959:
1.302     ozaki-r   960: static void
1.163     thorpej   961: if_attachdomain1(struct ifnet *ifp)
1.107     itojun    962: {
                    963:        struct domain *dp;
1.109     itojun    964:        int s;
                    965:
                    966:        s = splnet();
1.107     itojun    967:
1.106     itojun    968:        /* address family dependent data region */
                    969:        memset(ifp->if_afdata, 0, sizeof(ifp->if_afdata));
1.152     matt      970:        DOMAIN_FOREACH(dp) {
1.185     dyoung    971:                if (dp->dom_ifattach != NULL)
1.106     itojun    972:                        ifp->if_afdata[dp->dom_family] =
                    973:                            (*dp->dom_ifattach)(ifp);
                    974:        }
1.109     itojun    975:
                    976:        splx(s);
1.1       cgd       977: }
1.53      thorpej   978:
                    979: /*
                    980:  * Deactivate an interface.  This points all of the procedure
                    981:  * handles at error stubs.  May be called from interrupt context.
                    982:  */
                    983: void
1.163     thorpej   984: if_deactivate(struct ifnet *ifp)
1.53      thorpej   985: {
                    986:        int s;
                    987:
1.88      thorpej   988:        s = splnet();
1.53      thorpej   989:
                    990:        ifp->if_output   = if_nulloutput;
1.323     ozaki-r   991:        ifp->_if_input   = if_nullinput;
1.53      thorpej   992:        ifp->if_start    = if_nullstart;
1.332     knakahar  993:        ifp->if_transmit = if_nulltransmit;
1.53      thorpej   994:        ifp->if_ioctl    = if_nullioctl;
1.75      thorpej   995:        ifp->if_init     = if_nullinit;
                    996:        ifp->if_stop     = if_nullstop;
1.295     ozaki-r   997:        ifp->if_slowtimo = if_nullslowtimo;
1.53      thorpej   998:        ifp->if_drain    = if_nulldrain;
                    999:
                   1000:        /* No more packets may be enqueued. */
                   1001:        ifp->if_snd.ifq_maxlen = 0;
                   1002:
                   1003:        splx(s);
                   1004: }
                   1005:
1.206     dyoung   1006: void
1.218     dyoung   1007: if_purgeaddrs(struct ifnet *ifp, int family, void (*purgeaddr)(struct ifaddr *))
1.206     dyoung   1008: {
1.289     ozaki-r  1009:        struct ifaddr *ifa, *nifa;
1.206     dyoung   1010:
1.289     ozaki-r  1011:        IFADDR_FOREACH_SAFE(ifa, ifp, nifa) {
1.206     dyoung   1012:                if (ifa->ifa_addr->sa_family != family)
                   1013:                        continue;
                   1014:                (*purgeaddr)(ifa);
                   1015:        }
                   1016: }
                   1017:
1.53      thorpej  1018: /*
                   1019:  * Detach an interface from the list of "active" interfaces,
                   1020:  * freeing any resources as we go along.
                   1021:  *
                   1022:  * NOTE: This routine must be called with a valid thread context,
                   1023:  * as it may block.
                   1024:  */
                   1025: void
1.163     thorpej  1026: if_detach(struct ifnet *ifp)
1.53      thorpej  1027: {
1.56      thorpej  1028:        struct socket so;
1.178     dyoung   1029:        struct ifaddr *ifa;
1.53      thorpej  1030: #ifdef IFAREF_DEBUG
                   1031:        struct ifaddr *last_ifa = NULL;
                   1032: #endif
1.56      thorpej  1033:        struct domain *dp;
1.141     matt     1034:        const struct protosw *pr;
1.322     riastrad 1035:        int s, i, family, purged;
1.276     rmind    1036:        uint64_t xc;
1.53      thorpej  1037:
1.56      thorpej  1038:        /*
                   1039:         * XXX It's kind of lame that we have to have the
                   1040:         * XXX socket structure...
                   1041:         */
                   1042:        memset(&so, 0, sizeof(so));
1.53      thorpej  1043:
1.88      thorpej  1044:        s = splnet();
1.53      thorpej  1045:
1.334     ozaki-r  1046:        sysctl_teardown(&ifp->if_sysctl_log);
1.336     ozaki-r  1047:        mutex_enter(ifp->if_ioctl_lock);
                   1048:        ifp->if_ioctl = if_nullioctl;
                   1049:        mutex_exit(ifp->if_ioctl_lock);
1.334     ozaki-r  1050:
                   1051:        IFNET_LOCK();
1.319     ozaki-r  1052:        ifindex2ifnet[ifp->if_index] = NULL;
                   1053:        TAILQ_REMOVE(&ifnet_list, ifp, if_list);
1.334     ozaki-r  1054:        IFNET_WRITER_REMOVE(ifp);
                   1055:        pserialize_perform(ifnet_psz);
                   1056:        IFNET_UNLOCK();
                   1057:
                   1058:        /* Wait for all readers to drain before freeing.  */
                   1059:        psref_target_destroy(&ifp->if_psref, ifnet_psref_class);
                   1060:        PSLIST_ENTRY_DESTROY(ifp, if_pslist_entry);
1.319     ozaki-r  1061:
1.336     ozaki-r  1062:        mutex_obj_free(ifp->if_ioctl_lock);
                   1063:        ifp->if_ioctl_lock = NULL;
                   1064:
1.296     ozaki-r  1065:        if (ifp->if_slowtimo != NULL) {
1.305     martin   1066:                ifp->if_slowtimo = NULL;
1.297     ozaki-r  1067:                callout_halt(ifp->if_slowtimo_ch, NULL);
                   1068:                callout_destroy(ifp->if_slowtimo_ch);
                   1069:                kmem_free(ifp->if_slowtimo_ch, sizeof(*ifp->if_slowtimo_ch));
1.296     ozaki-r  1070:        }
                   1071:
1.53      thorpej  1072:        /*
                   1073:         * Do an if_down() to give protocols a chance to do something.
                   1074:         */
                   1075:        if_down(ifp);
1.86      thorpej  1076:
                   1077: #ifdef ALTQ
                   1078:        if (ALTQ_IS_ENABLED(&ifp->if_snd))
                   1079:                altq_disable(&ifp->if_snd);
                   1080:        if (ALTQ_IS_ATTACHED(&ifp->if_snd))
                   1081:                altq_detach(&ifp->if_snd);
1.87      thorpej  1082: #endif
                   1083:
1.285     ozaki-r  1084:        if (ifp->if_snd.ifq_lock)
                   1085:                mutex_obj_free(ifp->if_snd.ifq_lock);
                   1086:
1.166     liamjfoy 1087: #if NCARP > 0
                   1088:        /* Remove the interface from any carp group it is a part of.  */
1.185     dyoung   1089:        if (ifp->if_carp != NULL && ifp->if_type != IFT_CARP)
1.166     liamjfoy 1090:                carp_ifdetach(ifp);
                   1091: #endif
                   1092:
1.53      thorpej  1093:        /*
                   1094:         * Rip all the addresses off the interface.  This should make
                   1095:         * all of the routes go away.
1.178     dyoung   1096:         *
                   1097:         * pr_usrreq calls can remove an arbitrary number of ifaddrs
                   1098:         * from the list, including our "cursor", ifa.  For safety,
                   1099:         * and to honor the TAILQ abstraction, I just restart the
                   1100:         * loop after each removal.  Note that the loop will exit
                   1101:         * when all of the remaining ifaddrs belong to the AF_LINK
                   1102:         * family.  I am counting on the historical fact that at
                   1103:         * least one pr_usrreq in each address domain removes at
                   1104:         * least one ifaddr.
1.53      thorpej  1105:         */
1.178     dyoung   1106: again:
1.204     dyoung   1107:        IFADDR_FOREACH(ifa, ifp) {
1.56      thorpej  1108:                family = ifa->ifa_addr->sa_family;
1.53      thorpej  1109: #ifdef IFAREF_DEBUG
                   1110:                printf("if_detach: ifaddr %p, family %d, refcnt %d\n",
1.56      thorpej  1111:                    ifa, family, ifa->ifa_refcnt);
1.53      thorpej  1112:                if (last_ifa != NULL && ifa == last_ifa)
1.56      thorpej  1113:                        panic("if_detach: loop detected");
1.53      thorpej  1114:                last_ifa = ifa;
                   1115: #endif
1.178     dyoung   1116:                if (family == AF_LINK)
1.118     itojun   1117:                        continue;
                   1118:                dp = pffinddomain(family);
1.56      thorpej  1119: #ifdef DIAGNOSTIC
1.118     itojun   1120:                if (dp == NULL)
                   1121:                        panic("if_detach: no domain for AF %d",
                   1122:                            family);
1.56      thorpej  1123: #endif
1.160     gdt      1124:                /*
                   1125:                 * XXX These PURGEIF calls are redundant with the
                   1126:                 * purge-all-families calls below, but are left in for
                   1127:                 * now both to make a smaller change, and to avoid
                   1128:                 * unplanned interactions with clearing of
                   1129:                 * ifp->if_addrlist.
                   1130:                 */
1.118     itojun   1131:                purged = 0;
1.322     riastrad 1132:                for (pr = dp->dom_protosw;
                   1133:                     pr < dp->dom_protoswNPROTOSW; pr++) {
1.118     itojun   1134:                        so.so_proto = pr;
1.275     rmind    1135:                        if (pr->pr_usrreqs) {
1.290     rtr      1136:                                (void) (*pr->pr_usrreqs->pr_purgeif)(&so, ifp);
1.118     itojun   1137:                                purged = 1;
1.53      thorpej  1138:                        }
1.118     itojun   1139:                }
                   1140:                if (purged == 0) {
                   1141:                        /*
                   1142:                         * XXX What's really the best thing to do
1.135     keihan   1143:                         * XXX here?  --thorpej@NetBSD.org
1.118     itojun   1144:                         */
                   1145:                        printf("if_detach: WARNING: AF %d not purged\n",
                   1146:                            family);
1.207     dyoung   1147:                        ifa_remove(ifp, ifa);
1.53      thorpej  1148:                }
1.178     dyoung   1149:                goto again;
1.53      thorpej  1150:        }
1.118     itojun   1151:
                   1152:        if_free_sadl(ifp);
1.53      thorpej  1153:
1.180     dyoung   1154:        /* Walk the routing table looking for stragglers. */
1.243     dyoung   1155:        for (i = 0; i <= AF_MAX; i++) {
                   1156:                while (rt_walktree(i, if_rt_walktree, ifp) == ERESTART)
1.260     christos 1157:                        continue;
1.243     dyoung   1158:        }
1.106     itojun   1159:
1.152     matt     1160:        DOMAIN_FOREACH(dp) {
1.185     dyoung   1161:                if (dp->dom_ifdetach != NULL && ifp->if_afdata[dp->dom_family])
1.260     christos 1162:                {
                   1163:                        void *p = ifp->if_afdata[dp->dom_family];
                   1164:                        if (p) {
                   1165:                                ifp->if_afdata[dp->dom_family] = NULL;
                   1166:                                (*dp->dom_ifdetach)(ifp, p);
                   1167:                        }
                   1168:                }
1.160     gdt      1169:
                   1170:                /*
                   1171:                 * One would expect multicast memberships (INET and
                   1172:                 * INET6) on UDP sockets to be purged by the PURGEIF
                   1173:                 * calls above, but if all addresses were removed from
                   1174:                 * the interface prior to destruction, the calls will
                   1175:                 * not be made (e.g. ppp, for which pppd(8) generally
                   1176:                 * removes addresses before destroying the interface).
                   1177:                 * Because there is no invariant that multicast
                   1178:                 * memberships only exist for interfaces with IPv4
                   1179:                 * addresses, we must call PURGEIF regardless of
                   1180:                 * addresses.  (Protocols which might store ifnet
                   1181:                 * pointers are marked with PR_PURGEIF.)
                   1182:                 */
1.185     dyoung   1183:                for (pr = dp->dom_protosw; pr < dp->dom_protoswNPROTOSW; pr++) {
1.160     gdt      1184:                        so.so_proto = pr;
1.275     rmind    1185:                        if (pr->pr_usrreqs && pr->pr_flags & PR_PURGEIF)
1.290     rtr      1186:                                (void)(*pr->pr_usrreqs->pr_purgeif)(&so, ifp);
1.160     gdt      1187:                }
1.53      thorpej  1188:        }
1.57      thorpej  1189:
1.265     rmind    1190:        (void)pfil_run_hooks(if_pfil,
1.184     dyoung   1191:            (struct mbuf **)PFIL_IFNET_DETACH, ifp, PFIL_IFNET);
1.265     rmind    1192:        (void)pfil_head_destroy(ifp->if_pfil);
1.184     dyoung   1193:
1.57      thorpej  1194:        /* Announce that the interface is gone. */
                   1195:        rt_ifannouncemsg(ifp, IFAN_DEPARTURE);
1.93      itojun   1196:
1.320     ozaki-r  1197:        IF_AFDATA_LOCK_DESTROY(ifp);
                   1198:
1.324     ozaki-r  1199:        softint_disestablish(ifp->if_link_si);
                   1200:        ifp->if_link_si = NULL;
                   1201:
1.95      itojun   1202:        /*
1.168     matt     1203:         * remove packets that came from ifp, from software interrupt queues.
1.95      itojun   1204:         */
1.168     matt     1205:        DOMAIN_FOREACH(dp) {
                   1206:                for (i = 0; i < __arraycount(dp->dom_ifqueues); i++) {
1.260     christos 1207:                        struct ifqueue *iq = dp->dom_ifqueues[i];
                   1208:                        if (iq == NULL)
1.168     matt     1209:                                break;
1.260     christos 1210:                        dp->dom_ifqueues[i] = NULL;
                   1211:                        if_detach_queues(ifp, iq);
1.168     matt     1212:                }
                   1213:        }
1.95      itojun   1214:
1.276     rmind    1215:        /*
                   1216:         * IP queues have to be processed separately: net-queue barrier
                   1217:         * ensures that the packets are dequeued while a cross-call will
                   1218:         * ensure that the interrupts have completed. FIXME: not quite..
                   1219:         */
1.278     he       1220: #ifdef INET
1.276     rmind    1221:        pktq_barrier(ip_pktq);
1.278     he       1222: #endif
1.281     rmind    1223: #ifdef INET6
1.293     pooka    1224:        if (in6_present)
                   1225:                pktq_barrier(ip6_pktq);
1.281     rmind    1226: #endif
1.276     rmind    1227:        xc = xc_broadcast(0, (xcfunc_t)nullop, NULL, NULL);
                   1228:        xc_wait(xc);
                   1229:
1.323     ozaki-r  1230:        if (ifp->if_percpuq != NULL) {
                   1231:                if_percpuq_destroy(ifp->if_percpuq);
                   1232:                ifp->if_percpuq = NULL;
                   1233:        }
                   1234:
1.53      thorpej  1235:        splx(s);
1.95      itojun   1236: }
                   1237:
                   1238: static void
1.163     thorpej  1239: if_detach_queues(struct ifnet *ifp, struct ifqueue *q)
1.95      itojun   1240: {
                   1241:        struct mbuf *m, *prev, *next;
                   1242:
                   1243:        prev = NULL;
1.185     dyoung   1244:        for (m = q->ifq_head; m != NULL; m = next) {
1.274     rmind    1245:                KASSERT((m->m_flags & M_PKTHDR) != 0);
                   1246:
1.95      itojun   1247:                next = m->m_nextpkt;
1.96      itojun   1248:                if (m->m_pkthdr.rcvif != ifp) {
                   1249:                        prev = m;
1.95      itojun   1250:                        continue;
1.96      itojun   1251:                }
1.95      itojun   1252:
1.185     dyoung   1253:                if (prev != NULL)
1.95      itojun   1254:                        prev->m_nextpkt = m->m_nextpkt;
                   1255:                else
                   1256:                        q->ifq_head = m->m_nextpkt;
                   1257:                if (q->ifq_tail == m)
                   1258:                        q->ifq_tail = prev;
                   1259:                q->ifq_len--;
                   1260:
                   1261:                m->m_nextpkt = NULL;
                   1262:                m_freem(m);
                   1263:                IF_DROP(q);
                   1264:        }
1.53      thorpej  1265: }
                   1266:
                   1267: /*
                   1268:  * Callback for a radix tree walk to delete all references to an
                   1269:  * ifnet.
                   1270:  */
1.163     thorpej  1271: static int
1.192     dyoung   1272: if_rt_walktree(struct rtentry *rt, void *v)
1.53      thorpej  1273: {
1.55      itojun   1274:        struct ifnet *ifp = (struct ifnet *)v;
1.53      thorpej  1275:        int error;
1.317     ozaki-r  1276:        struct rtentry *retrt;
1.53      thorpej  1277:
1.185     dyoung   1278:        if (rt->rt_ifp != ifp)
                   1279:                return 0;
                   1280:
                   1281:        /* Delete the entry. */
1.194     dyoung   1282:        error = rtrequest(RTM_DELETE, rt_getkey(rt), rt->rt_gateway,
1.317     ozaki-r  1283:            rt_mask(rt), rt->rt_flags, &retrt);
                   1284:        if (error == 0) {
                   1285:                KASSERT(retrt == rt);
                   1286:                KASSERT((retrt->rt_flags & RTF_UP) == 0);
                   1287:                retrt->rt_ifp = NULL;
                   1288:                rtfree(retrt);
                   1289:        } else {
1.185     dyoung   1290:                printf("%s: warning: unable to delete rtentry @ %p, "
                   1291:                    "error = %d\n", ifp->if_xname, rt, error);
1.317     ozaki-r  1292:        }
1.243     dyoung   1293:        return ERESTART;
1.53      thorpej  1294: }
                   1295:
1.1       cgd      1296: /*
1.63      thorpej  1297:  * Create a clone network interface.
                   1298:  */
1.302     ozaki-r  1299: static int
1.163     thorpej  1300: if_clone_create(const char *name)
1.63      thorpej  1301: {
                   1302:        struct if_clone *ifc;
                   1303:        int unit;
1.336     ozaki-r  1304:        struct ifnet *ifp;
                   1305:        struct psref psref;
1.63      thorpej  1306:
                   1307:        ifc = if_clone_lookup(name, &unit);
                   1308:        if (ifc == NULL)
1.185     dyoung   1309:                return EINVAL;
1.63      thorpej  1310:
1.336     ozaki-r  1311:        ifp = if_get(name, &psref);
                   1312:        if (ifp != NULL) {
                   1313:                if_put(ifp, &psref);
1.185     dyoung   1314:                return EEXIST;
1.336     ozaki-r  1315:        }
1.63      thorpej  1316:
1.185     dyoung   1317:        return (*ifc->ifc_create)(ifc, unit);
1.63      thorpej  1318: }
                   1319:
                   1320: /*
                   1321:  * Destroy a clone network interface.
                   1322:  */
1.302     ozaki-r  1323: static int
1.163     thorpej  1324: if_clone_destroy(const char *name)
1.63      thorpej  1325: {
                   1326:        struct if_clone *ifc;
                   1327:        struct ifnet *ifp;
1.336     ozaki-r  1328:        struct psref psref;
1.63      thorpej  1329:
                   1330:        ifc = if_clone_lookup(name, NULL);
                   1331:        if (ifc == NULL)
1.185     dyoung   1332:                return EINVAL;
1.63      thorpej  1333:
1.336     ozaki-r  1334:        if (ifc->ifc_destroy == NULL)
                   1335:                return EOPNOTSUPP;
                   1336:
                   1337:        ifp = if_get(name, &psref);
1.63      thorpej  1338:        if (ifp == NULL)
1.185     dyoung   1339:                return ENXIO;
1.63      thorpej  1340:
1.336     ozaki-r  1341:        /* We have to disable ioctls here */
                   1342:        mutex_enter(ifp->if_ioctl_lock);
                   1343:        ifp->if_ioctl = if_nullioctl;
                   1344:        mutex_exit(ifp->if_ioctl_lock);
                   1345:
                   1346:        /*
                   1347:         * We cannot call ifc_destroy with holding ifp.
                   1348:         * Releasing ifp here is safe thanks to if_clone_mtx.
                   1349:         */
                   1350:        if_put(ifp, &psref);
1.63      thorpej  1351:
1.185     dyoung   1352:        return (*ifc->ifc_destroy)(ifp);
1.63      thorpej  1353: }
                   1354:
                   1355: /*
                   1356:  * Look up a network interface cloner.
                   1357:  */
1.163     thorpej  1358: static struct if_clone *
                   1359: if_clone_lookup(const char *name, int *unitp)
1.63      thorpej  1360: {
                   1361:        struct if_clone *ifc;
                   1362:        const char *cp;
1.262     christos 1363:        char *dp, ifname[IFNAMSIZ + 3];
1.128     itojun   1364:        int unit;
1.63      thorpej  1365:
1.262     christos 1366:        strcpy(ifname, "if_");
1.128     itojun   1367:        /* separate interface name from unit */
1.262     christos 1368:        for (dp = ifname + 3, cp = name; cp - name < IFNAMSIZ &&
                   1369:            *cp && (*cp < '0' || *cp > '9');)
                   1370:                *dp++ = *cp++;
1.128     itojun   1371:
                   1372:        if (cp == name || cp - name == IFNAMSIZ || !*cp)
1.185     dyoung   1373:                return NULL;    /* No name or unit number */
1.262     christos 1374:        *dp++ = '\0';
1.128     itojun   1375:
1.262     christos 1376: again:
1.128     itojun   1377:        LIST_FOREACH(ifc, &if_cloners, ifc_list) {
1.262     christos 1378:                if (strcmp(ifname + 3, ifc->ifc_name) == 0)
1.128     itojun   1379:                        break;
1.63      thorpej  1380:        }
                   1381:
1.262     christos 1382:        if (ifc == NULL) {
                   1383:                if (*ifname == '\0' ||
1.267     christos 1384:                    module_autoload(ifname, MODULE_CLASS_DRIVER))
1.262     christos 1385:                        return NULL;
                   1386:                *ifname = '\0';
                   1387:                goto again;
                   1388:        }
1.63      thorpej  1389:
1.128     itojun   1390:        unit = 0;
1.129     itojun   1391:        while (cp - name < IFNAMSIZ && *cp) {
1.245     christos 1392:                if (*cp < '0' || *cp > '9' || unit >= INT_MAX / 10) {
1.63      thorpej  1393:                        /* Bogus unit number. */
1.185     dyoung   1394:                        return NULL;
1.63      thorpej  1395:                }
1.128     itojun   1396:                unit = (unit * 10) + (*cp++ - '0');
1.63      thorpej  1397:        }
                   1398:
                   1399:        if (unitp != NULL)
1.128     itojun   1400:                *unitp = unit;
1.185     dyoung   1401:        return ifc;
1.63      thorpej  1402: }
                   1403:
                   1404: /*
                   1405:  * Register a network interface cloner.
                   1406:  */
                   1407: void
1.163     thorpej  1408: if_clone_attach(struct if_clone *ifc)
1.63      thorpej  1409: {
                   1410:
                   1411:        LIST_INSERT_HEAD(&if_cloners, ifc, ifc_list);
1.67      thorpej  1412:        if_cloners_count++;
1.63      thorpej  1413: }
                   1414:
                   1415: /*
                   1416:  * Unregister a network interface cloner.
                   1417:  */
                   1418: void
1.163     thorpej  1419: if_clone_detach(struct if_clone *ifc)
1.63      thorpej  1420: {
                   1421:
                   1422:        LIST_REMOVE(ifc, ifc_list);
1.67      thorpej  1423:        if_cloners_count--;
                   1424: }
                   1425:
                   1426: /*
                   1427:  * Provide list of interface cloners to userspace.
                   1428:  */
1.315     martin   1429: int
                   1430: if_clone_list(int buf_count, char *buffer, int *total)
1.67      thorpej  1431: {
                   1432:        char outbuf[IFNAMSIZ], *dst;
                   1433:        struct if_clone *ifc;
                   1434:        int count, error = 0;
                   1435:
1.315     martin   1436:        *total = if_cloners_count;
                   1437:        if ((dst = buffer) == NULL) {
1.67      thorpej  1438:                /* Just asking how many there are. */
1.185     dyoung   1439:                return 0;
1.67      thorpej  1440:        }
                   1441:
1.315     martin   1442:        if (buf_count < 0)
1.185     dyoung   1443:                return EINVAL;
1.67      thorpej  1444:
1.315     martin   1445:        count = (if_cloners_count < buf_count) ?
                   1446:            if_cloners_count : buf_count;
1.67      thorpej  1447:
                   1448:        for (ifc = LIST_FIRST(&if_cloners); ifc != NULL && count != 0;
                   1449:             ifc = LIST_NEXT(ifc, ifc_list), count--, dst += IFNAMSIZ) {
1.175     christos 1450:                (void)strncpy(outbuf, ifc->ifc_name, sizeof(outbuf));
                   1451:                if (outbuf[sizeof(outbuf) - 1] != '\0')
                   1452:                        return ENAMETOOLONG;
1.172     christos 1453:                error = copyout(outbuf, dst, sizeof(outbuf));
1.185     dyoung   1454:                if (error != 0)
1.67      thorpej  1455:                        break;
                   1456:        }
                   1457:
1.185     dyoung   1458:        return error;
1.63      thorpej  1459: }
                   1460:
1.207     dyoung   1461: void
1.291     rmind    1462: ifaref(struct ifaddr *ifa)
                   1463: {
                   1464:        ifa->ifa_refcnt++;
                   1465: }
                   1466:
                   1467: void
                   1468: ifafree(struct ifaddr *ifa)
                   1469: {
                   1470:        KASSERT(ifa != NULL);
                   1471:        KASSERT(ifa->ifa_refcnt > 0);
                   1472:
                   1473:        if (--ifa->ifa_refcnt == 0) {
                   1474:                free(ifa, M_IFADDR);
                   1475:        }
                   1476: }
                   1477:
                   1478: void
1.207     dyoung   1479: ifa_insert(struct ifnet *ifp, struct ifaddr *ifa)
                   1480: {
                   1481:        ifa->ifa_ifp = ifp;
1.208     dyoung   1482:        TAILQ_INSERT_TAIL(&ifp->if_addrlist, ifa, ifa_list);
1.291     rmind    1483:        ifaref(ifa);
1.207     dyoung   1484: }
                   1485:
                   1486: void
                   1487: ifa_remove(struct ifnet *ifp, struct ifaddr *ifa)
                   1488: {
                   1489:        KASSERT(ifa->ifa_ifp == ifp);
                   1490:        TAILQ_REMOVE(&ifp->if_addrlist, ifa, ifa_list);
1.291     rmind    1491:        ifafree(ifa);
1.207     dyoung   1492: }
                   1493:
1.194     dyoung   1494: static inline int
                   1495: equal(const struct sockaddr *sa1, const struct sockaddr *sa2)
                   1496: {
                   1497:        return sockaddr_cmp(sa1, sa2) == 0;
                   1498: }
                   1499:
1.63      thorpej  1500: /*
1.1       cgd      1501:  * Locate an interface based on a complete address.
                   1502:  */
                   1503: /*ARGSUSED*/
                   1504: struct ifaddr *
1.163     thorpej  1505: ifa_ifwithaddr(const struct sockaddr *addr)
1.1       cgd      1506: {
1.61      augustss 1507:        struct ifnet *ifp;
                   1508:        struct ifaddr *ifa;
1.334     ozaki-r  1509:        int s;
1.1       cgd      1510:
1.334     ozaki-r  1511:        s = pserialize_read_enter();
                   1512:        IFNET_READER_FOREACH(ifp) {
1.53      thorpej  1513:                if (ifp->if_output == if_nulloutput)
1.1       cgd      1514:                        continue;
1.204     dyoung   1515:                IFADDR_FOREACH(ifa, ifp) {
1.53      thorpej  1516:                        if (ifa->ifa_addr->sa_family != addr->sa_family)
                   1517:                                continue;
                   1518:                        if (equal(addr, ifa->ifa_addr))
1.185     dyoung   1519:                                return ifa;
1.53      thorpej  1520:                        if ((ifp->if_flags & IFF_BROADCAST) &&
                   1521:                            ifa->ifa_broadaddr &&
                   1522:                            /* IP6 doesn't have broadcast */
                   1523:                            ifa->ifa_broadaddr->sa_len != 0 &&
                   1524:                            equal(ifa->ifa_broadaddr, addr))
1.185     dyoung   1525:                                return ifa;
1.53      thorpej  1526:                }
1.1       cgd      1527:        }
1.334     ozaki-r  1528:        pserialize_read_exit(s);
1.185     dyoung   1529:        return NULL;
1.1       cgd      1530: }
1.49      itojun   1531:
1.1       cgd      1532: /*
                   1533:  * Locate the point to point interface with a given destination address.
                   1534:  */
                   1535: /*ARGSUSED*/
                   1536: struct ifaddr *
1.163     thorpej  1537: ifa_ifwithdstaddr(const struct sockaddr *addr)
1.1       cgd      1538: {
1.61      augustss 1539:        struct ifnet *ifp;
                   1540:        struct ifaddr *ifa;
1.334     ozaki-r  1541:        int s;
1.1       cgd      1542:
1.334     ozaki-r  1543:        s = pserialize_read_enter();
                   1544:        IFNET_READER_FOREACH(ifp) {
1.53      thorpej  1545:                if (ifp->if_output == if_nulloutput)
                   1546:                        continue;
1.185     dyoung   1547:                if ((ifp->if_flags & IFF_POINTOPOINT) == 0)
                   1548:                        continue;
1.204     dyoung   1549:                IFADDR_FOREACH(ifa, ifp) {
1.185     dyoung   1550:                        if (ifa->ifa_addr->sa_family != addr->sa_family ||
                   1551:                            ifa->ifa_dstaddr == NULL)
                   1552:                                continue;
                   1553:                        if (equal(addr, ifa->ifa_dstaddr))
                   1554:                                return ifa;
1.53      thorpej  1555:                }
1.1       cgd      1556:        }
1.334     ozaki-r  1557:        pserialize_read_exit(s);
1.185     dyoung   1558:        return NULL;
1.1       cgd      1559: }
                   1560:
                   1561: /*
                   1562:  * Find an interface on a specific network.  If many, choice
1.15      mycroft  1563:  * is most specific found.
1.1       cgd      1564:  */
                   1565: struct ifaddr *
1.163     thorpej  1566: ifa_ifwithnet(const struct sockaddr *addr)
1.1       cgd      1567: {
1.61      augustss 1568:        struct ifnet *ifp;
                   1569:        struct ifaddr *ifa;
1.140     matt     1570:        const struct sockaddr_dl *sdl;
1.15      mycroft  1571:        struct ifaddr *ifa_maybe = 0;
1.1       cgd      1572:        u_int af = addr->sa_family;
1.171     pooka    1573:        const char *addr_data = addr->sa_data, *cplim;
1.334     ozaki-r  1574:        int s;
1.1       cgd      1575:
                   1576:        if (af == AF_LINK) {
1.195     dyoung   1577:                sdl = satocsdl(addr);
1.137     itojun   1578:                if (sdl->sdl_index && sdl->sdl_index < if_indexlim &&
                   1579:                    ifindex2ifnet[sdl->sdl_index] &&
1.316     ozaki-r  1580:                    ifindex2ifnet[sdl->sdl_index]->if_output != if_nulloutput) {
                   1581:                        return ifindex2ifnet[sdl->sdl_index]->if_dl;
                   1582:                }
1.1       cgd      1583:        }
1.51      bouyer   1584: #ifdef NETATALK
                   1585:        if (af == AF_APPLETALK) {
1.140     matt     1586:                const struct sockaddr_at *sat, *sat2;
1.158     christos 1587:                sat = (const struct sockaddr_at *)addr;
1.334     ozaki-r  1588:                s = pserialize_read_enter();
                   1589:                IFNET_READER_FOREACH(ifp) {
1.53      thorpej  1590:                        if (ifp->if_output == if_nulloutput)
                   1591:                                continue;
1.158     christos 1592:                        ifa = at_ifawithnet((const struct sockaddr_at *)addr, ifp);
1.62      bouyer   1593:                        if (ifa == NULL)
                   1594:                                continue;
                   1595:                        sat2 = (struct sockaddr_at *)ifa->ifa_addr;
                   1596:                        if (sat2->sat_addr.s_net == sat->sat_addr.s_net)
1.185     dyoung   1597:                                return ifa; /* exact match */
1.62      bouyer   1598:                        if (ifa_maybe == NULL) {
1.112     wiz      1599:                                /* else keep the if with the right range */
1.62      bouyer   1600:                                ifa_maybe = ifa;
                   1601:                        }
1.51      bouyer   1602:                }
1.334     ozaki-r  1603:                pserialize_read_exit(s);
1.185     dyoung   1604:                return ifa_maybe;
1.51      bouyer   1605:        }
                   1606: #endif
1.334     ozaki-r  1607:        s = pserialize_read_enter();
                   1608:        IFNET_READER_FOREACH(ifp) {
1.53      thorpej  1609:                if (ifp->if_output == if_nulloutput)
                   1610:                        continue;
1.204     dyoung   1611:                IFADDR_FOREACH(ifa, ifp) {
1.171     pooka    1612:                        const char *cp, *cp2, *cp3;
1.15      mycroft  1613:
                   1614:                        if (ifa->ifa_addr->sa_family != af ||
1.185     dyoung   1615:                            ifa->ifa_netmask == NULL)
1.53      thorpej  1616:  next:                         continue;
1.15      mycroft  1617:                        cp = addr_data;
                   1618:                        cp2 = ifa->ifa_addr->sa_data;
                   1619:                        cp3 = ifa->ifa_netmask->sa_data;
1.171     pooka    1620:                        cplim = (const char *)ifa->ifa_netmask +
1.53      thorpej  1621:                            ifa->ifa_netmask->sa_len;
                   1622:                        while (cp3 < cplim) {
                   1623:                                if ((*cp++ ^ *cp2++) & *cp3++) {
                   1624:                                        /* want to continue for() loop */
1.32      mrg      1625:                                        goto next;
1.53      thorpej  1626:                                }
                   1627:                        }
1.185     dyoung   1628:                        if (ifa_maybe == NULL ||
1.329     ozaki-r  1629:                            rt_refines(ifa->ifa_netmask,
                   1630:                                       ifa_maybe->ifa_netmask))
1.15      mycroft  1631:                                ifa_maybe = ifa;
                   1632:                }
1.53      thorpej  1633:        }
1.334     ozaki-r  1634:        pserialize_read_exit(s);
1.185     dyoung   1635:        return ifa_maybe;
1.26      mrg      1636: }
1.53      thorpej  1637:
1.26      mrg      1638: /*
                   1639:  * Find the interface of the addresss.
                   1640:  */
                   1641: struct ifaddr *
1.163     thorpej  1642: ifa_ifwithladdr(const struct sockaddr *addr)
1.26      mrg      1643: {
                   1644:        struct ifaddr *ia;
                   1645:
1.53      thorpej  1646:        if ((ia = ifa_ifwithaddr(addr)) || (ia = ifa_ifwithdstaddr(addr)) ||
                   1647:            (ia = ifa_ifwithnet(addr)))
1.185     dyoung   1648:                return ia;
                   1649:        return NULL;
1.1       cgd      1650: }
                   1651:
                   1652: /*
                   1653:  * Find an interface using a specific address family
                   1654:  */
                   1655: struct ifaddr *
1.163     thorpej  1656: ifa_ifwithaf(int af)
1.1       cgd      1657: {
1.61      augustss 1658:        struct ifnet *ifp;
1.334     ozaki-r  1659:        struct ifaddr *ifa = NULL;
                   1660:        int s;
1.1       cgd      1661:
1.334     ozaki-r  1662:        s = pserialize_read_enter();
                   1663:        IFNET_READER_FOREACH(ifp) {
1.53      thorpej  1664:                if (ifp->if_output == if_nulloutput)
                   1665:                        continue;
1.204     dyoung   1666:                IFADDR_FOREACH(ifa, ifp) {
1.21      mycroft  1667:                        if (ifa->ifa_addr->sa_family == af)
1.334     ozaki-r  1668:                                goto out;
1.53      thorpej  1669:                }
                   1670:        }
1.334     ozaki-r  1671: out:
                   1672:        pserialize_read_exit(s);
                   1673:        return ifa;
1.1       cgd      1674: }
                   1675:
                   1676: /*
                   1677:  * Find an interface address specific to an interface best matching
                   1678:  * a given address.
                   1679:  */
                   1680: struct ifaddr *
1.163     thorpej  1681: ifaof_ifpforaddr(const struct sockaddr *addr, struct ifnet *ifp)
1.1       cgd      1682: {
1.61      augustss 1683:        struct ifaddr *ifa;
1.140     matt     1684:        const char *cp, *cp2, *cp3;
                   1685:        const char *cplim;
1.1       cgd      1686:        struct ifaddr *ifa_maybe = 0;
                   1687:        u_int af = addr->sa_family;
                   1688:
1.53      thorpej  1689:        if (ifp->if_output == if_nulloutput)
1.185     dyoung   1690:                return NULL;
1.53      thorpej  1691:
1.1       cgd      1692:        if (af >= AF_MAX)
1.185     dyoung   1693:                return NULL;
1.53      thorpej  1694:
1.204     dyoung   1695:        IFADDR_FOREACH(ifa, ifp) {
1.1       cgd      1696:                if (ifa->ifa_addr->sa_family != af)
                   1697:                        continue;
                   1698:                ifa_maybe = ifa;
1.185     dyoung   1699:                if (ifa->ifa_netmask == NULL) {
1.1       cgd      1700:                        if (equal(addr, ifa->ifa_addr) ||
1.53      thorpej  1701:                            (ifa->ifa_dstaddr &&
                   1702:                             equal(addr, ifa->ifa_dstaddr)))
1.185     dyoung   1703:                                return ifa;
1.1       cgd      1704:                        continue;
                   1705:                }
                   1706:                cp = addr->sa_data;
                   1707:                cp2 = ifa->ifa_addr->sa_data;
                   1708:                cp3 = ifa->ifa_netmask->sa_data;
                   1709:                cplim = ifa->ifa_netmask->sa_len + (char *)ifa->ifa_netmask;
1.53      thorpej  1710:                for (; cp3 < cplim; cp3++) {
1.1       cgd      1711:                        if ((*cp++ ^ *cp2++) & *cp3)
                   1712:                                break;
1.53      thorpej  1713:                }
1.1       cgd      1714:                if (cp3 == cplim)
1.185     dyoung   1715:                        return ifa;
1.1       cgd      1716:        }
1.185     dyoung   1717:        return ifa_maybe;
1.1       cgd      1718: }
1.9       mycroft  1719:
1.1       cgd      1720: /*
                   1721:  * Default action when installing a route with a Link Level gateway.
                   1722:  * Lookup an appropriate real ifa to point to.
                   1723:  * This should be moved to /sys/net/link.c eventually.
                   1724:  */
1.15      mycroft  1725: void
1.228     dyoung   1726: link_rtrequest(int cmd, struct rtentry *rt, const struct rt_addrinfo *info)
1.1       cgd      1727: {
1.61      augustss 1728:        struct ifaddr *ifa;
1.194     dyoung   1729:        const struct sockaddr *dst;
1.15      mycroft  1730:        struct ifnet *ifp;
1.1       cgd      1731:
1.225     dyoung   1732:        if (cmd != RTM_ADD || (ifa = rt->rt_ifa) == NULL ||
                   1733:            (ifp = ifa->ifa_ifp) == NULL || (dst = rt_getkey(rt)) == NULL)
1.1       cgd      1734:                return;
1.24      christos 1735:        if ((ifa = ifaof_ifpforaddr(dst, ifp)) != NULL) {
1.176     dyoung   1736:                rt_replace_ifa(rt, ifa);
1.1       cgd      1737:                if (ifa->ifa_rtrequest && ifa->ifa_rtrequest != link_rtrequest)
1.82      itojun   1738:                        ifa->ifa_rtrequest(cmd, rt, info);
1.1       cgd      1739:        }
                   1740: }
                   1741:
                   1742: /*
1.325     roy      1743:  * bitmask macros to manage a densely packed link_state change queue.
                   1744:  * Because we need to store LINK_STATE_UNKNOWN(0), LINK_STATE_DOWN(1) and
                   1745:  * LINK_STATE_UP(2) we need 2 bits for each state change.
                   1746:  * As a state change to store is 0, treat all bits set as an unset item.
                   1747:  */
                   1748: #define LQ_ITEM_BITS           2
                   1749: #define LQ_ITEM_MASK           ((1 << LQ_ITEM_BITS) - 1)
                   1750: #define LQ_MASK(i)             (LQ_ITEM_MASK << (i) * LQ_ITEM_BITS)
                   1751: #define LINK_STATE_UNSET       LQ_ITEM_MASK
                   1752: #define LQ_ITEM(q, i)          (((q) & LQ_MASK((i))) >> (i) * LQ_ITEM_BITS)
                   1753: #define LQ_STORE(q, i, v)                                                    \
                   1754:        do {                                                                  \
                   1755:                (q) &= ~LQ_MASK((i));                                         \
                   1756:                (q) |= (v) << (i) * LQ_ITEM_BITS;                             \
                   1757:        } while (0 /* CONSTCOND */)
                   1758: #define LQ_MAX(q)              ((sizeof((q)) * NBBY) / LQ_ITEM_BITS)
                   1759: #define LQ_POP(q, v)                                                         \
                   1760:        do {                                                                  \
                   1761:                (v) = LQ_ITEM((q), 0);                                        \
                   1762:                (q) >>= LQ_ITEM_BITS;                                         \
                   1763:                (q) |= LINK_STATE_UNSET << (LQ_MAX((q)) - 1) * LQ_ITEM_BITS;  \
                   1764:        } while (0 /* CONSTCOND */)
                   1765: #define LQ_PUSH(q, v)                                                        \
                   1766:        do {                                                                  \
                   1767:                (q) >>= LQ_ITEM_BITS;                                         \
                   1768:                (q) |= (v) << (LQ_MAX((q)) - 1) * LQ_ITEM_BITS;               \
                   1769:        } while (0 /* CONSTCOND */)
                   1770: #define LQ_FIND_UNSET(q, i)                                                  \
                   1771:        for ((i) = 0; i < LQ_MAX((q)); (i)++) {                               \
                   1772:                if (LQ_ITEM((q), (i)) == LINK_STATE_UNSET)                    \
                   1773:                        break;                                                \
                   1774:        }
                   1775: /*
                   1776:  * Handle a change in the interface link state and
                   1777:  * queue notifications.
1.159     dyoung   1778:  */
                   1779: void
                   1780: if_link_state_change(struct ifnet *ifp, int link_state)
                   1781: {
1.325     roy      1782:        int s, idx;
1.263     roy      1783:
1.325     roy      1784:        /* Ensure change is to a valid state */
                   1785:        switch (link_state) {
                   1786:        case LINK_STATE_UNKNOWN:        /* FALLTHROUGH */
                   1787:        case LINK_STATE_DOWN:           /* FALLTHROUGH */
                   1788:        case LINK_STATE_UP:
                   1789:                break;
                   1790:        default:
                   1791: #ifdef DEBUG
                   1792:                printf("%s: invalid link state %d\n",
                   1793:                    ifp->if_xname, link_state);
                   1794: #endif
1.185     dyoung   1795:                return;
1.264     roy      1796:        }
1.263     roy      1797:
1.325     roy      1798:        s = splnet();
                   1799:
                   1800:        /* Find the last unset event in the queue. */
                   1801:        LQ_FIND_UNSET(ifp->if_link_queue, idx);
                   1802:
                   1803:        /*
                   1804:         * Ensure link_state doesn't match the last event in the queue.
                   1805:         * ifp->if_link_state is not checked and set here because
                   1806:         * that would present an inconsistent picture to the system.
                   1807:         */
                   1808:        if (idx != 0 &&
                   1809:            LQ_ITEM(ifp->if_link_queue, idx - 1) == (uint8_t)link_state)
                   1810:                goto out;
                   1811:
                   1812:        /* Handle queue overflow. */
                   1813:        if (idx == LQ_MAX(ifp->if_link_queue)) {
                   1814:                uint8_t lost;
                   1815:
                   1816:                /*
                   1817:                 * The DOWN state must be protected from being pushed off
                   1818:                 * the queue to ensure that userland will always be
                   1819:                 * in a sane state.
                   1820:                 * Because DOWN is protected, there is no need to protect
                   1821:                 * UNKNOWN.
                   1822:                 * It should be invalid to change from any other state to
                   1823:                 * UNKNOWN anyway ...
                   1824:                 */
                   1825:                lost = LQ_ITEM(ifp->if_link_queue, 0);
                   1826:                LQ_PUSH(ifp->if_link_queue, (uint8_t)link_state);
                   1827:                if (lost == LINK_STATE_DOWN) {
                   1828:                        lost = LQ_ITEM(ifp->if_link_queue, 0);
                   1829:                        LQ_STORE(ifp->if_link_queue, 0, LINK_STATE_DOWN);
                   1830:                }
                   1831:                printf("%s: lost link state change %s\n",
                   1832:                    ifp->if_xname,
                   1833:                    lost == LINK_STATE_UP ? "UP" :
                   1834:                    lost == LINK_STATE_DOWN ? "DOWN" :
                   1835:                    "UNKNOWN");
                   1836:        } else
                   1837:                LQ_STORE(ifp->if_link_queue, idx, (uint8_t)link_state);
                   1838:
1.324     ozaki-r  1839:        softint_schedule(ifp->if_link_si);
                   1840:
1.325     roy      1841: out:
1.324     ozaki-r  1842:        splx(s);
                   1843: }
                   1844:
1.325     roy      1845: /*
                   1846:  * Handle interface link state change notifications.
                   1847:  * Must be called at splnet().
                   1848:  */
1.324     ozaki-r  1849: static void
1.325     roy      1850: if_link_state_change0(struct ifnet *ifp, int link_state)
1.324     ozaki-r  1851: {
                   1852:        struct domain *dp;
                   1853:
1.325     roy      1854:        /* Ensure the change is still valid. */
                   1855:        if (ifp->if_link_state == link_state)
                   1856:                return;
1.324     ozaki-r  1857:
1.263     roy      1858: #ifdef DEBUG
                   1859:        log(LOG_DEBUG, "%s: link state %s (was %s)\n", ifp->if_xname,
                   1860:                link_state == LINK_STATE_UP ? "UP" :
                   1861:                link_state == LINK_STATE_DOWN ? "DOWN" :
                   1862:                "UNKNOWN",
1.325     roy      1863:                ifp->if_link_state == LINK_STATE_UP ? "UP" :
                   1864:                ifp->if_link_state == LINK_STATE_DOWN ? "DOWN" :
1.263     roy      1865:                "UNKNOWN");
                   1866: #endif
                   1867:
                   1868:        /*
                   1869:         * When going from UNKNOWN to UP, we need to mark existing
1.314     roy      1870:         * addresses as tentative and restart DAD as we may have
1.263     roy      1871:         * erroneously not found a duplicate.
                   1872:         *
                   1873:         * This needs to happen before rt_ifmsg to avoid a race where
                   1874:         * listeners would have an address and expect it to work right
                   1875:         * away.
                   1876:         */
1.312     roy      1877:        if (link_state == LINK_STATE_UP &&
1.325     roy      1878:            ifp->if_link_state == LINK_STATE_UNKNOWN)
1.312     roy      1879:        {
                   1880:                DOMAIN_FOREACH(dp) {
                   1881:                        if (dp->dom_if_link_state_change != NULL)
                   1882:                                dp->dom_if_link_state_change(ifp,
                   1883:                                    LINK_STATE_DOWN);
                   1884:                }
                   1885:        }
1.263     roy      1886:
1.325     roy      1887:        ifp->if_link_state = link_state;
                   1888:
1.159     dyoung   1889:        /* Notify that the link state has changed. */
1.185     dyoung   1890:        rt_ifmsg(ifp);
1.263     roy      1891:
1.166     liamjfoy 1892: #if NCARP > 0
1.185     dyoung   1893:        if (ifp->if_carp)
                   1894:                carp_carpdev_state(ifp);
1.166     liamjfoy 1895: #endif
1.263     roy      1896:
1.312     roy      1897:        DOMAIN_FOREACH(dp) {
                   1898:                if (dp->dom_if_link_state_change != NULL)
                   1899:                        dp->dom_if_link_state_change(ifp, link_state);
1.270     pooka    1900:        }
1.325     roy      1901: }
                   1902:
                   1903: /*
                   1904:  * Process the interface link state change queue.
                   1905:  */
                   1906: static void
                   1907: if_link_state_change_si(void *arg)
                   1908: {
                   1909:        struct ifnet *ifp = arg;
                   1910:        int s;
                   1911:        uint8_t state;
                   1912:
                   1913:        s = splnet();
                   1914:
                   1915:        /* Pop a link state change from the queue and process it. */
                   1916:        LQ_POP(ifp->if_link_queue, state);
                   1917:        if_link_state_change0(ifp, state);
                   1918:
                   1919:        /* If there is a link state change to come, schedule it. */
                   1920:        if (LQ_ITEM(ifp->if_link_queue, 0) != LINK_STATE_UNSET)
                   1921:                softint_schedule(ifp->if_link_si);
1.264     roy      1922:
                   1923:        splx(s);
1.159     dyoung   1924: }
                   1925:
                   1926: /*
1.310     roy      1927:  * Default action when installing a local route on a point-to-point
                   1928:  * interface.
                   1929:  */
                   1930: void
                   1931: p2p_rtrequest(int req, struct rtentry *rt,
                   1932:     __unused const struct rt_addrinfo *info)
                   1933: {
                   1934:        struct ifnet *ifp = rt->rt_ifp;
                   1935:        struct ifaddr *ifa, *lo0ifa;
                   1936:
                   1937:        switch (req) {
                   1938:        case RTM_ADD:
                   1939:                if ((rt->rt_flags & RTF_LOCAL) == 0)
                   1940:                        break;
                   1941:
                   1942:                IFADDR_FOREACH(ifa, ifp) {
                   1943:                        if (equal(rt_getkey(rt), ifa->ifa_addr))
                   1944:                                break;
                   1945:                }
                   1946:                if (ifa == NULL)
                   1947:                        break;
                   1948:
                   1949:                /*
                   1950:                 * Ensure lo0 has an address of the same family.
                   1951:                 */
                   1952:                IFADDR_FOREACH(lo0ifa, lo0ifp) {
                   1953:                        if (lo0ifa->ifa_addr->sa_family ==
                   1954:                            ifa->ifa_addr->sa_family)
                   1955:                                break;
                   1956:                }
                   1957:                if (lo0ifa == NULL)
                   1958:                        break;
                   1959:
                   1960:                rt->rt_ifp = lo0ifp;
                   1961:
                   1962:                /*
                   1963:                 * Make sure to set rt->rt_ifa to the interface
                   1964:                 * address we are using, otherwise we will have trouble
                   1965:                 * with source address selection.
                   1966:                 */
                   1967:                if (ifa != rt->rt_ifa)
                   1968:                        rt_replace_ifa(rt, ifa);
                   1969:                break;
                   1970:        case RTM_DELETE:
                   1971:        default:
                   1972:                break;
                   1973:        }
                   1974: }
                   1975:
                   1976: /*
1.1       cgd      1977:  * Mark an interface down and notify protocols of
                   1978:  * the transition.
1.23      mycroft  1979:  * NOTE: must be called at splsoftnet or equivalent.
1.1       cgd      1980:  */
1.15      mycroft  1981: void
1.163     thorpej  1982: if_down(struct ifnet *ifp)
1.1       cgd      1983: {
1.61      augustss 1984:        struct ifaddr *ifa;
1.312     roy      1985:        struct domain *dp;
1.1       cgd      1986:
                   1987:        ifp->if_flags &= ~IFF_UP;
1.232     christos 1988:        nanotime(&ifp->if_lastchange);
1.204     dyoung   1989:        IFADDR_FOREACH(ifa, ifp)
1.1       cgd      1990:                pfctlinput(PRC_IFDOWN, ifa->ifa_addr);
1.78      thorpej  1991:        IFQ_PURGE(&ifp->if_snd);
1.166     liamjfoy 1992: #if NCARP > 0
                   1993:        if (ifp->if_carp)
                   1994:                carp_carpdev_state(ifp);
                   1995: #endif
1.15      mycroft  1996:        rt_ifmsg(ifp);
1.312     roy      1997:        DOMAIN_FOREACH(dp) {
                   1998:                if (dp->dom_if_down)
                   1999:                        dp->dom_if_down(ifp);
                   2000:        }
1.15      mycroft  2001: }
                   2002:
                   2003: /*
                   2004:  * Mark an interface up and notify protocols of
                   2005:  * the transition.
1.23      mycroft  2006:  * NOTE: must be called at splsoftnet or equivalent.
1.15      mycroft  2007:  */
                   2008: void
1.163     thorpej  2009: if_up(struct ifnet *ifp)
1.15      mycroft  2010: {
1.24      christos 2011: #ifdef notyet
1.61      augustss 2012:        struct ifaddr *ifa;
1.24      christos 2013: #endif
1.312     roy      2014:        struct domain *dp;
1.15      mycroft  2015:
                   2016:        ifp->if_flags |= IFF_UP;
1.232     christos 2017:        nanotime(&ifp->if_lastchange);
1.15      mycroft  2018: #ifdef notyet
                   2019:        /* this has no effect on IP, and will kill all ISO connections XXX */
1.204     dyoung   2020:        IFADDR_FOREACH(ifa, ifp)
1.15      mycroft  2021:                pfctlinput(PRC_IFUP, ifa->ifa_addr);
                   2022: #endif
1.166     liamjfoy 2023: #if NCARP > 0
                   2024:        if (ifp->if_carp)
                   2025:                carp_carpdev_state(ifp);
                   2026: #endif
1.15      mycroft  2027:        rt_ifmsg(ifp);
1.312     roy      2028:        DOMAIN_FOREACH(dp) {
1.313     roy      2029:                if (dp->dom_if_up)
                   2030:                        dp->dom_if_up(ifp);
1.312     roy      2031:        }
1.1       cgd      2032: }
                   2033:
                   2034: /*
1.296     ozaki-r  2035:  * Handle interface slowtimo timer routine.  Called
                   2036:  * from softclock, we decrement timer (if set) and
1.1       cgd      2037:  * call the appropriate interface routine on expiration.
                   2038:  */
1.294     ozaki-r  2039: static void
1.177     christos 2040: if_slowtimo(void *arg)
1.1       cgd      2041: {
1.306     martin   2042:        void (*slowtimo)(struct ifnet *);
1.296     ozaki-r  2043:        struct ifnet *ifp = arg;
1.305     martin   2044:        int s;
1.1       cgd      2045:
1.306     martin   2046:        slowtimo = ifp->if_slowtimo;
                   2047:        if (__predict_false(slowtimo == NULL))
1.305     martin   2048:                return;
1.296     ozaki-r  2049:
1.305     martin   2050:        s = splnet();
1.296     ozaki-r  2051:        if (ifp->if_timer != 0 && --ifp->if_timer == 0)
1.306     martin   2052:                (*slowtimo)(ifp);
1.296     ozaki-r  2053:
1.1       cgd      2054:        splx(s);
1.305     martin   2055:
                   2056:        if (__predict_true(ifp->if_slowtimo != NULL))
                   2057:                callout_schedule(ifp->if_slowtimo_ch, hz / IFNET_SLOWHZ);
1.65      thorpej  2058: }
                   2059:
                   2060: /*
                   2061:  * Set/clear promiscuous mode on interface ifp based on the truth value
                   2062:  * of pswitch.  The calls are reference counted so that only the first
                   2063:  * "on" request actually has an effect, as does the final "off" request.
                   2064:  * Results are undefined if the "off" and "on" requests are not matched.
                   2065:  */
                   2066: int
1.163     thorpej  2067: ifpromisc(struct ifnet *ifp, int pswitch)
1.65      thorpej  2068: {
                   2069:        int pcount, ret;
1.259     dyoung   2070:        short nflags;
1.65      thorpej  2071:
                   2072:        pcount = ifp->if_pcount;
                   2073:        if (pswitch) {
                   2074:                /*
1.71      thorpej  2075:                 * Allow the device to be "placed" into promiscuous
                   2076:                 * mode even if it is not configured up.  It will
1.242     mbalmer  2077:                 * consult IFF_PROMISC when it is brought up.
1.65      thorpej  2078:                 */
1.68      pk       2079:                if (ifp->if_pcount++ != 0)
1.185     dyoung   2080:                        return 0;
1.252     dyoung   2081:                nflags = ifp->if_flags | IFF_PROMISC;
1.65      thorpej  2082:        } else {
                   2083:                if (--ifp->if_pcount > 0)
1.185     dyoung   2084:                        return 0;
1.252     dyoung   2085:                nflags = ifp->if_flags & ~IFF_PROMISC;
1.65      thorpej  2086:        }
1.252     dyoung   2087:        ret = if_flags_set(ifp, nflags);
1.65      thorpej  2088:        /* Restore interface state if not successful. */
                   2089:        if (ret != 0) {
                   2090:                ifp->if_pcount = pcount;
                   2091:        }
1.185     dyoung   2092:        return ret;
1.1       cgd      2093: }
                   2094:
                   2095: /*
                   2096:  * Map interface name to
                   2097:  * interface structure pointer.
                   2098:  */
                   2099: struct ifnet *
1.163     thorpej  2100: ifunit(const char *name)
1.1       cgd      2101: {
1.61      augustss 2102:        struct ifnet *ifp;
1.105     matt     2103:        const char *cp = name;
                   2104:        u_int unit = 0;
                   2105:        u_int i;
1.334     ozaki-r  2106:        int s;
1.105     matt     2107:
                   2108:        /*
                   2109:         * If the entire name is a number, treat it as an ifindex.
                   2110:         */
                   2111:        for (i = 0; i < IFNAMSIZ && *cp >= '0' && *cp <= '9'; i++, cp++) {
                   2112:                unit = unit * 10 + (*cp - '0');
                   2113:        }
                   2114:
                   2115:        /*
                   2116:         * If the number took all of the name, then it's a valid ifindex.
                   2117:         */
                   2118:        if (i == IFNAMSIZ || (cp != name && *cp == '\0')) {
1.137     itojun   2119:                if (unit >= if_indexlim)
1.185     dyoung   2120:                        return NULL;
1.105     matt     2121:                ifp = ifindex2ifnet[unit];
                   2122:                if (ifp == NULL || ifp->if_output == if_nulloutput)
1.185     dyoung   2123:                        return NULL;
                   2124:                return ifp;
1.105     matt     2125:        }
1.34      thorpej  2126:
1.334     ozaki-r  2127:        ifp = NULL;
                   2128:        s = pserialize_read_enter();
                   2129:        IFNET_READER_FOREACH(ifp) {
1.53      thorpej  2130:                if (ifp->if_output == if_nulloutput)
                   2131:                        continue;
                   2132:                if (strcmp(ifp->if_xname, name) == 0)
1.334     ozaki-r  2133:                        goto out;
1.53      thorpej  2134:        }
1.334     ozaki-r  2135: out:
                   2136:        pserialize_read_exit(s);
                   2137:        return ifp;
1.1       cgd      2138: }
1.49      itojun   2139:
1.335     ozaki-r  2140: /*
                   2141:  * Get a reference of an ifnet object by an interface name.
                   2142:  * The returned reference is protected by psref(9). The caller
                   2143:  * must release a returned reference by if_put after use.
                   2144:  */
                   2145: struct ifnet *
                   2146: if_get(const char *name, struct psref *psref)
                   2147: {
                   2148:        struct ifnet *ifp;
                   2149:        const char *cp = name;
                   2150:        u_int unit = 0;
                   2151:        u_int i;
                   2152:        int s;
                   2153:
                   2154:        /*
                   2155:         * If the entire name is a number, treat it as an ifindex.
                   2156:         */
                   2157:        for (i = 0; i < IFNAMSIZ && *cp >= '0' && *cp <= '9'; i++, cp++) {
                   2158:                unit = unit * 10 + (*cp - '0');
                   2159:        }
                   2160:
                   2161:        /*
                   2162:         * If the number took all of the name, then it's a valid ifindex.
                   2163:         */
                   2164:        if (i == IFNAMSIZ || (cp != name && *cp == '\0')) {
                   2165:                if (unit >= if_indexlim)
                   2166:                        return NULL;
                   2167:                ifp = ifindex2ifnet[unit];
                   2168:                if (ifp == NULL || ifp->if_output == if_nulloutput)
                   2169:                        return NULL;
                   2170:                return ifp;
                   2171:        }
                   2172:
                   2173:        ifp = NULL;
                   2174:        s = pserialize_read_enter();
                   2175:        IFNET_READER_FOREACH(ifp) {
                   2176:                if (ifp->if_output == if_nulloutput)
                   2177:                        continue;
                   2178:                if (strcmp(ifp->if_xname, name) == 0) {
                   2179:                        psref_acquire(psref, &ifp->if_psref,
                   2180:                            ifnet_psref_class);
                   2181:                        goto out;
                   2182:                }
                   2183:        }
                   2184: out:
                   2185:        pserialize_read_exit(s);
                   2186:        return ifp;
                   2187: }
                   2188:
                   2189: /*
                   2190:  * Release a reference of an ifnet object given by if_get or
                   2191:  * if_get_byindex.
                   2192:  */
                   2193: void
                   2194: if_put(const struct ifnet *ifp, struct psref *psref)
                   2195: {
                   2196:
                   2197:        psref_release(psref, &ifp->if_psref, ifnet_psref_class);
                   2198: }
                   2199:
1.250     rmind    2200: ifnet_t *
                   2201: if_byindex(u_int idx)
                   2202: {
                   2203:        return (idx < if_indexlim) ? ifindex2ifnet[idx] : NULL;
                   2204: }
                   2205:
1.335     ozaki-r  2206: /*
                   2207:  * Get a reference of an ifnet object by an interface index.
                   2208:  * The returned reference is protected by psref(9). The caller
                   2209:  * must release a returned reference by if_put after use.
                   2210:  */
                   2211: ifnet_t *
                   2212: if_get_byindex(u_int idx, struct psref *psref)
                   2213: {
                   2214:        ifnet_t *ifp;
                   2215:        int s;
                   2216:
                   2217:        s = pserialize_read_enter();
1.337   ! ozaki-r  2218:        ifp = (__predict_true(idx < if_indexlim)) ? ifindex2ifnet[idx] : NULL;
        !          2219:        if (__predict_true(ifp != NULL))
1.335     ozaki-r  2220:                psref_acquire(psref, &ifp->if_psref, ifnet_psref_class);
                   2221:        pserialize_read_exit(s);
                   2222:
                   2223:        return ifp;
                   2224: }
                   2225:
1.211     dyoung   2226: /* common */
1.215     dyoung   2227: int
                   2228: ifioctl_common(struct ifnet *ifp, u_long cmd, void *data)
1.211     dyoung   2229: {
1.224     dyoung   2230:        int s;
1.215     dyoung   2231:        struct ifreq *ifr;
                   2232:        struct ifcapreq *ifcr;
                   2233:        struct ifdatareq *ifdr;
1.211     dyoung   2234:
                   2235:        switch (cmd) {
                   2236:        case SIOCSIFCAP:
1.215     dyoung   2237:                ifcr = data;
1.211     dyoung   2238:                if ((ifcr->ifcr_capenable & ~ifp->if_capabilities) != 0)
                   2239:                        return EINVAL;
                   2240:
1.213     dyoung   2241:                if (ifcr->ifcr_capenable == ifp->if_capenable)
                   2242:                        return 0;
1.211     dyoung   2243:
1.213     dyoung   2244:                ifp->if_capenable = ifcr->ifcr_capenable;
1.211     dyoung   2245:
1.213     dyoung   2246:                /* Pre-compute the checksum flags mask. */
                   2247:                ifp->if_csum_flags_tx = 0;
                   2248:                ifp->if_csum_flags_rx = 0;
                   2249:                if (ifp->if_capenable & IFCAP_CSUM_IPv4_Tx) {
                   2250:                        ifp->if_csum_flags_tx |= M_CSUM_IPv4;
                   2251:                }
                   2252:                if (ifp->if_capenable & IFCAP_CSUM_IPv4_Rx) {
                   2253:                        ifp->if_csum_flags_rx |= M_CSUM_IPv4;
                   2254:                }
1.211     dyoung   2255:
1.213     dyoung   2256:                if (ifp->if_capenable & IFCAP_CSUM_TCPv4_Tx) {
                   2257:                        ifp->if_csum_flags_tx |= M_CSUM_TCPv4;
                   2258:                }
                   2259:                if (ifp->if_capenable & IFCAP_CSUM_TCPv4_Rx) {
                   2260:                        ifp->if_csum_flags_rx |= M_CSUM_TCPv4;
                   2261:                }
1.211     dyoung   2262:
1.213     dyoung   2263:                if (ifp->if_capenable & IFCAP_CSUM_UDPv4_Tx) {
                   2264:                        ifp->if_csum_flags_tx |= M_CSUM_UDPv4;
                   2265:                }
                   2266:                if (ifp->if_capenable & IFCAP_CSUM_UDPv4_Rx) {
                   2267:                        ifp->if_csum_flags_rx |= M_CSUM_UDPv4;
                   2268:                }
1.211     dyoung   2269:
1.213     dyoung   2270:                if (ifp->if_capenable & IFCAP_CSUM_TCPv6_Tx) {
                   2271:                        ifp->if_csum_flags_tx |= M_CSUM_TCPv6;
                   2272:                }
                   2273:                if (ifp->if_capenable & IFCAP_CSUM_TCPv6_Rx) {
                   2274:                        ifp->if_csum_flags_rx |= M_CSUM_TCPv6;
                   2275:                }
1.211     dyoung   2276:
1.213     dyoung   2277:                if (ifp->if_capenable & IFCAP_CSUM_UDPv6_Tx) {
                   2278:                        ifp->if_csum_flags_tx |= M_CSUM_UDPv6;
1.211     dyoung   2279:                }
1.213     dyoung   2280:                if (ifp->if_capenable & IFCAP_CSUM_UDPv6_Rx) {
                   2281:                        ifp->if_csum_flags_rx |= M_CSUM_UDPv6;
                   2282:                }
1.215     dyoung   2283:                if (ifp->if_flags & IFF_UP)
                   2284:                        return ENETRESET;
                   2285:                return 0;
1.211     dyoung   2286:        case SIOCSIFFLAGS:
1.215     dyoung   2287:                ifr = data;
1.211     dyoung   2288:                if (ifp->if_flags & IFF_UP && (ifr->ifr_flags & IFF_UP) == 0) {
                   2289:                        s = splnet();
                   2290:                        if_down(ifp);
                   2291:                        splx(s);
                   2292:                }
                   2293:                if (ifr->ifr_flags & IFF_UP && (ifp->if_flags & IFF_UP) == 0) {
                   2294:                        s = splnet();
                   2295:                        if_up(ifp);
                   2296:                        splx(s);
                   2297:                }
                   2298:                ifp->if_flags = (ifp->if_flags & IFF_CANTCHANGE) |
                   2299:                        (ifr->ifr_flags &~ IFF_CANTCHANGE);
                   2300:                break;
                   2301:        case SIOCGIFFLAGS:
1.215     dyoung   2302:                ifr = data;
1.211     dyoung   2303:                ifr->ifr_flags = ifp->if_flags;
                   2304:                break;
                   2305:
                   2306:        case SIOCGIFMETRIC:
1.215     dyoung   2307:                ifr = data;
1.211     dyoung   2308:                ifr->ifr_metric = ifp->if_metric;
                   2309:                break;
                   2310:
                   2311:        case SIOCGIFMTU:
1.215     dyoung   2312:                ifr = data;
1.211     dyoung   2313:                ifr->ifr_mtu = ifp->if_mtu;
                   2314:                break;
                   2315:
                   2316:        case SIOCGIFDLT:
1.215     dyoung   2317:                ifr = data;
1.211     dyoung   2318:                ifr->ifr_dlt = ifp->if_dlt;
                   2319:                break;
                   2320:
                   2321:        case SIOCGIFCAP:
1.215     dyoung   2322:                ifcr = data;
1.211     dyoung   2323:                ifcr->ifcr_capabilities = ifp->if_capabilities;
                   2324:                ifcr->ifcr_capenable = ifp->if_capenable;
                   2325:                break;
                   2326:
                   2327:        case SIOCSIFMETRIC:
1.215     dyoung   2328:                ifr = data;
1.211     dyoung   2329:                ifp->if_metric = ifr->ifr_metric;
                   2330:                break;
                   2331:
                   2332:        case SIOCGIFDATA:
1.215     dyoung   2333:                ifdr = data;
1.211     dyoung   2334:                ifdr->ifdr_data = ifp->if_data;
                   2335:                break;
                   2336:
1.266     christos 2337:        case SIOCGIFINDEX:
                   2338:                ifr = data;
                   2339:                ifr->ifr_index = ifp->if_index;
                   2340:                break;
                   2341:
1.211     dyoung   2342:        case SIOCZIFDATA:
1.215     dyoung   2343:                ifdr = data;
1.211     dyoung   2344:                ifdr->ifdr_data = ifp->if_data;
                   2345:                /*
                   2346:                 * Assumes that the volatile counters that can be
                   2347:                 * zero'ed are at the end of if_data.
                   2348:                 */
                   2349:                memset(&ifp->if_data.ifi_ipackets, 0, sizeof(ifp->if_data) -
                   2350:                    offsetof(struct if_data, ifi_ipackets));
1.261     msaitoh  2351:                /*
                   2352:                 * The memset() clears to the bottm of if_data. In the area,
                   2353:                 * if_lastchange is included. Please be careful if new entry
                   2354:                 * will be added into if_data or rewite this.
                   2355:                 *
                   2356:                 * And also, update if_lastchnage.
                   2357:                 */
                   2358:                getnanotime(&ifp->if_lastchange);
1.211     dyoung   2359:                break;
1.215     dyoung   2360:        case SIOCSIFMTU:
                   2361:                ifr = data;
                   2362:                if (ifp->if_mtu == ifr->ifr_mtu)
                   2363:                        break;
                   2364:                ifp->if_mtu = ifr->ifr_mtu;
                   2365:                /*
                   2366:                 * If the link MTU changed, do network layer specific procedure.
                   2367:                 */
                   2368: #ifdef INET6
1.271     pooka    2369:                if (in6_present)
                   2370:                        nd6_setmtu(ifp);
1.215     dyoung   2371: #endif
                   2372:                return ENETRESET;
1.211     dyoung   2373:        default:
1.223     dyoung   2374:                return ENOTTY;
1.211     dyoung   2375:        }
                   2376:        return 0;
                   2377: }
                   2378:
1.235     dyoung   2379: int
1.284     rtr      2380: ifaddrpref_ioctl(struct socket *so, u_long cmd, void *data, struct ifnet *ifp)
1.235     dyoung   2381: {
                   2382:        struct if_addrprefreq *ifap = (struct if_addrprefreq *)data;
                   2383:        struct ifaddr *ifa;
                   2384:        const struct sockaddr *any, *sa;
                   2385:        union {
                   2386:                struct sockaddr sa;
                   2387:                struct sockaddr_storage ss;
1.236     jakllsch 2388:        } u, v;
1.235     dyoung   2389:
                   2390:        switch (cmd) {
                   2391:        case SIOCSIFADDRPREF:
1.284     rtr      2392:                if (kauth_authorize_network(curlwp->l_cred, KAUTH_NETWORK_INTERFACE,
1.235     dyoung   2393:                    KAUTH_REQ_NETWORK_INTERFACE_SETPRIV, ifp, (void *)cmd,
                   2394:                    NULL) != 0)
                   2395:                        return EPERM;
                   2396:        case SIOCGIFADDRPREF:
                   2397:                break;
                   2398:        default:
                   2399:                return EOPNOTSUPP;
                   2400:        }
                   2401:
                   2402:        /* sanity checks */
                   2403:        if (data == NULL || ifp == NULL) {
                   2404:                panic("invalid argument to %s", __func__);
                   2405:                /*NOTREACHED*/
                   2406:        }
                   2407:
                   2408:        /* address must be specified on ADD and DELETE */
                   2409:        sa = sstocsa(&ifap->ifap_addr);
                   2410:        if (sa->sa_family != sofamily(so))
                   2411:                return EINVAL;
                   2412:        if ((any = sockaddr_any(sa)) == NULL || sa->sa_len != any->sa_len)
                   2413:                return EINVAL;
                   2414:
1.236     jakllsch 2415:        sockaddr_externalize(&v.sa, sizeof(v.ss), sa);
                   2416:
1.235     dyoung   2417:        IFADDR_FOREACH(ifa, ifp) {
                   2418:                if (ifa->ifa_addr->sa_family != sa->sa_family)
                   2419:                        continue;
                   2420:                sockaddr_externalize(&u.sa, sizeof(u.ss), ifa->ifa_addr);
1.236     jakllsch 2421:                if (sockaddr_cmp(&u.sa, &v.sa) == 0)
1.235     dyoung   2422:                        break;
                   2423:        }
                   2424:        if (ifa == NULL)
                   2425:                return EADDRNOTAVAIL;
                   2426:
                   2427:        switch (cmd) {
                   2428:        case SIOCSIFADDRPREF:
                   2429:                ifa->ifa_preference = ifap->ifap_preference;
                   2430:                return 0;
                   2431:        case SIOCGIFADDRPREF:
                   2432:                /* fill in the if_laddrreq structure */
                   2433:                (void)sockaddr_copy(sstosa(&ifap->ifap_addr),
                   2434:                    sizeof(ifap->ifap_addr), ifa->ifa_addr);
                   2435:                ifap->ifap_preference = ifa->ifa_preference;
                   2436:                return 0;
                   2437:        default:
                   2438:                return EOPNOTSUPP;
                   2439:        }
                   2440: }
                   2441:
1.1       cgd      2442: /*
                   2443:  * Interface ioctls.
                   2444:  */
1.273     pooka    2445: static int
                   2446: doifioctl(struct socket *so, u_long cmd, void *data, struct lwp *l)
1.1       cgd      2447: {
1.61      augustss 2448:        struct ifnet *ifp;
                   2449:        struct ifreq *ifr;
1.217     martin   2450:        int error = 0;
1.191     christos 2451: #if defined(COMPAT_OSOCK) || defined(COMPAT_OIFREQ)
                   2452:        u_long ocmd = cmd;
                   2453: #endif
1.49      itojun   2454:        short oif_flags;
1.186     christos 2455: #ifdef COMPAT_OIFREQ
                   2456:        struct ifreq ifrb;
1.187     xtraeme  2457:        struct oifreq *oifr = NULL;
1.186     christos 2458: #endif
1.292     christos 2459:        int r;
1.336     ozaki-r  2460:        struct psref psref;
                   2461:        int bound = curlwp->l_pflag & LP_BOUND;
1.1       cgd      2462:
                   2463:        switch (cmd) {
1.186     christos 2464: #ifdef COMPAT_OIFREQ
                   2465:        case OSIOCGIFCONF:
                   2466:        case OOSIOCGIFCONF:
                   2467:                return compat_ifconf(cmd, data);
                   2468: #endif
1.232     christos 2469: #ifdef COMPAT_OIFDATA
                   2470:        case OSIOCGIFDATA:
                   2471:        case OSIOCZIFDATA:
                   2472:                return compat_ifdatareq(l, cmd, data);
                   2473: #endif
1.1       cgd      2474:        case SIOCGIFCONF:
1.185     dyoung   2475:                return ifconf(cmd, data);
1.231     dyoung   2476:        case SIOCINITIFADDR:
                   2477:                return EPERM;
1.1       cgd      2478:        }
1.191     christos 2479:
1.186     christos 2480: #ifdef COMPAT_OIFREQ
1.196     skd      2481:        cmd = compat_cvtcmd(cmd);
1.186     christos 2482:        if (cmd != ocmd) {
                   2483:                oifr = data;
                   2484:                data = ifr = &ifrb;
                   2485:                ifreqo2n(oifr, ifr);
                   2486:        } else
                   2487: #endif
                   2488:                ifr = data;
1.63      thorpej  2489:
                   2490:        switch (cmd) {
                   2491:        case SIOCIFCREATE:
                   2492:        case SIOCIFDESTROY:
1.336     ozaki-r  2493:                curlwp->l_pflag |= LP_BOUND;
1.185     dyoung   2494:                if (l != NULL) {
1.336     ozaki-r  2495:                        ifp = if_get(ifr->ifr_name, &psref);
1.174     elad     2496:                        error = kauth_authorize_network(l->l_cred,
                   2497:                            KAUTH_NETWORK_INTERFACE,
                   2498:                            KAUTH_REQ_NETWORK_INTERFACE_SETPRIV, ifp,
                   2499:                            (void *)cmd, NULL);
1.336     ozaki-r  2500:                        if (ifp != NULL)
                   2501:                                if_put(ifp, &psref);
                   2502:                        if (error != 0) {
                   2503:                                curlwp->l_pflag ^= bound ^ LP_BOUND;
1.151     yamt     2504:                                return error;
1.336     ozaki-r  2505:                        }
1.151     yamt     2506:                }
1.292     christos 2507:                mutex_enter(&if_clone_mtx);
                   2508:                r = (cmd == SIOCIFCREATE) ?
1.64      thorpej  2509:                        if_clone_create(ifr->ifr_name) :
1.185     dyoung   2510:                        if_clone_destroy(ifr->ifr_name);
1.292     christos 2511:                mutex_exit(&if_clone_mtx);
1.336     ozaki-r  2512:                curlwp->l_pflag ^= bound ^ LP_BOUND;
1.292     christos 2513:                return r;
1.67      thorpej  2514:
                   2515:        case SIOCIFGCLONERS:
1.315     martin   2516:                {
                   2517:                        struct if_clonereq *req = (struct if_clonereq *)data;
                   2518:                        return if_clone_list(req->ifcr_count, req->ifcr_buffer,
                   2519:                            &req->ifcr_total);
                   2520:                }
1.63      thorpej  2521:        }
                   2522:
1.336     ozaki-r  2523:        curlwp->l_pflag |= LP_BOUND;
                   2524:        ifp = if_get(ifr->ifr_name, &psref);
                   2525:        if (ifp == NULL) {
                   2526:                curlwp->l_pflag ^= bound ^ LP_BOUND;
1.185     dyoung   2527:                return ENXIO;
1.336     ozaki-r  2528:        }
1.151     yamt     2529:
                   2530:        switch (cmd) {
1.233     christos 2531:        case SIOCALIFADDR:
                   2532:        case SIOCDLIFADDR:
                   2533:        case SIOCSIFADDRPREF:
1.151     yamt     2534:        case SIOCSIFFLAGS:
                   2535:        case SIOCSIFCAP:
                   2536:        case SIOCSIFMETRIC:
                   2537:        case SIOCZIFDATA:
                   2538:        case SIOCSIFMTU:
                   2539:        case SIOCSIFPHYADDR:
                   2540:        case SIOCDIFPHYADDR:
                   2541: #ifdef INET6
                   2542:        case SIOCSIFPHYADDR_IN6:
                   2543: #endif
                   2544:        case SIOCSLIFPHYADDR:
                   2545:        case SIOCADDMULTI:
                   2546:        case SIOCDELMULTI:
                   2547:        case SIOCSIFMEDIA:
1.154     perry    2548:        case SIOCSDRVSPEC:
1.196     skd      2549:        case SIOCG80211:
                   2550:        case SIOCS80211:
1.151     yamt     2551:        case SIOCS80211NWID:
                   2552:        case SIOCS80211NWKEY:
                   2553:        case SIOCS80211POWER:
                   2554:        case SIOCS80211BSSID:
                   2555:        case SIOCS80211CHANNEL:
1.249     pooka    2556:        case SIOCSLINKSTR:
1.185     dyoung   2557:                if (l != NULL) {
1.174     elad     2558:                        error = kauth_authorize_network(l->l_cred,
                   2559:                            KAUTH_NETWORK_INTERFACE,
                   2560:                            KAUTH_REQ_NETWORK_INTERFACE_SETPRIV, ifp,
                   2561:                            (void *)cmd, NULL);
1.185     dyoung   2562:                        if (error != 0)
1.336     ozaki-r  2563:                                goto out;
1.151     yamt     2564:                }
                   2565:        }
                   2566:
1.49      itojun   2567:        oif_flags = ifp->if_flags;
1.1       cgd      2568:
1.336     ozaki-r  2569:        mutex_enter(ifp->if_ioctl_lock);
                   2570:
1.231     dyoung   2571:        error = (*ifp->if_ioctl)(ifp, cmd, data);
                   2572:        if (error != ENOTTY)
                   2573:                ;
                   2574:        else if (so->so_proto == NULL)
1.252     dyoung   2575:                error = EOPNOTSUPP;
1.231     dyoung   2576:        else {
1.161     christos 2577: #ifdef COMPAT_OSOCK
1.186     christos 2578:                error = compat_ifioctl(so, ocmd, cmd, data, l);
1.161     christos 2579: #else
1.283     rtr      2580:                error = (*so->so_proto->pr_usrreqs->pr_ioctl)(so,
1.284     rtr      2581:                    cmd, data, ifp);
1.1       cgd      2582: #endif
1.49      itojun   2583:        }
1.1       cgd      2584:
1.49      itojun   2585:        if (((oif_flags ^ ifp->if_flags) & IFF_UP) != 0) {
1.312     roy      2586:                if ((ifp->if_flags & IFF_UP) != 0) {
1.217     martin   2587:                        int s = splnet();
1.312     roy      2588:                        if_up(ifp);
1.49      itojun   2589:                        splx(s);
                   2590:                }
1.1       cgd      2591:        }
1.186     christos 2592: #ifdef COMPAT_OIFREQ
                   2593:        if (cmd != ocmd)
1.246     christos 2594:                ifreqn2o(oifr, ifr);
1.186     christos 2595: #endif
1.49      itojun   2596:
1.336     ozaki-r  2597:        mutex_exit(ifp->if_ioctl_lock);
                   2598: out:
                   2599:        if_put(ifp, &psref);
                   2600:        curlwp->l_pflag ^= bound ^ LP_BOUND;
1.185     dyoung   2601:        return error;
1.1       cgd      2602: }
                   2603:
                   2604: /*
                   2605:  * Return interface configuration
                   2606:  * of system.  List may be used
                   2607:  * in later ioctl's (above) to get
                   2608:  * other information.
1.200     gdt      2609:  *
                   2610:  * Each record is a struct ifreq.  Before the addition of
                   2611:  * sockaddr_storage, the API rule was that sockaddr flavors that did
                   2612:  * not fit would extend beyond the struct ifreq, with the next struct
                   2613:  * ifreq starting sa_len beyond the struct sockaddr.  Because the
                   2614:  * union in struct ifreq includes struct sockaddr_storage, every kind
                   2615:  * of sockaddr must fit.  Thus, there are no longer any overlength
                   2616:  * records.
                   2617:  *
                   2618:  * Records are added to the user buffer if they fit, and ifc_len is
                   2619:  * adjusted to the length that was written.  Thus, the user is only
                   2620:  * assured of getting the complete list if ifc_len on return is at
                   2621:  * least sizeof(struct ifreq) less than it was on entry.
                   2622:  *
                   2623:  * If the user buffer pointer is NULL, this routine copies no data and
                   2624:  * returns the amount of space that would be needed.
                   2625:  *
                   2626:  * Invariants:
                   2627:  * ifrp points to the next part of the user's buffer to be used.  If
                   2628:  * ifrp != NULL, space holds the number of bytes remaining that we may
                   2629:  * write at ifrp.  Otherwise, space holds the number of bytes that
                   2630:  * would have been written had there been adequate space.
1.1       cgd      2631:  */
                   2632: /*ARGSUSED*/
1.302     ozaki-r  2633: static int
1.183     christos 2634: ifconf(u_long cmd, void *data)
1.1       cgd      2635: {
1.61      augustss 2636:        struct ifconf *ifc = (struct ifconf *)data;
                   2637:        struct ifnet *ifp;
                   2638:        struct ifaddr *ifa;
1.304     ozaki-r  2639:        struct ifreq ifr, *ifrp = NULL;
                   2640:        int space = 0, error = 0;
1.200     gdt      2641:        const int sz = (int)sizeof(struct ifreq);
1.304     ozaki-r  2642:        const bool docopy = ifc->ifc_req != NULL;
1.334     ozaki-r  2643:        int s;
                   2644:        int bound = curlwp->l_pflag & LP_BOUND;
                   2645:        struct psref psref;
1.1       cgd      2646:
1.304     ozaki-r  2647:        if (docopy) {
1.190     enami    2648:                space = ifc->ifc_len;
1.304     ozaki-r  2649:                ifrp = ifc->ifc_req;
                   2650:        }
                   2651:
1.334     ozaki-r  2652:        curlwp->l_pflag |= LP_BOUND;
                   2653:        s = pserialize_read_enter();
                   2654:        IFNET_READER_FOREACH(ifp) {
                   2655:                psref_acquire(&psref, &ifp->if_psref, ifnet_psref_class);
                   2656:                pserialize_read_exit(s);
                   2657:
1.175     christos 2658:                (void)strncpy(ifr.ifr_name, ifp->if_xname,
1.173     christos 2659:                    sizeof(ifr.ifr_name));
1.334     ozaki-r  2660:                if (ifr.ifr_name[sizeof(ifr.ifr_name) - 1] != '\0') {
                   2661:                        error = ENAMETOOLONG;
                   2662:                        goto release_exit;
                   2663:                }
1.205     dyoung   2664:                if (IFADDR_EMPTY(ifp)) {
1.200     gdt      2665:                        /* Interface with no addresses - send zero sockaddr. */
1.127     christos 2666:                        memset(&ifr.ifr_addr, 0, sizeof(ifr.ifr_addr));
1.304     ozaki-r  2667:                        if (!docopy) {
1.218     dyoung   2668:                                space += sz;
                   2669:                                continue;
                   2670:                        }
                   2671:                        if (space >= sz) {
                   2672:                                error = copyout(&ifr, ifrp, sz);
                   2673:                                if (error != 0)
1.334     ozaki-r  2674:                                        goto release_exit;
1.218     dyoung   2675:                                ifrp++;
                   2676:                                space -= sz;
1.70      mellon   2677:                        }
1.127     christos 2678:                }
                   2679:
1.204     dyoung   2680:                IFADDR_FOREACH(ifa, ifp) {
1.61      augustss 2681:                        struct sockaddr *sa = ifa->ifa_addr;
1.200     gdt      2682:                        /* all sockaddrs must fit in sockaddr_storage */
                   2683:                        KASSERT(sa->sa_len <= sizeof(ifr.ifr_ifru));
                   2684:
1.304     ozaki-r  2685:                        if (!docopy) {
1.218     dyoung   2686:                                space += sz;
                   2687:                                continue;
                   2688:                        }
                   2689:                        memcpy(&ifr.ifr_space, sa, sa->sa_len);
                   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++; space -= sz;
1.1       cgd      2695:                        }
                   2696:                }
1.334     ozaki-r  2697:
                   2698:                s = pserialize_read_enter();
                   2699:                psref_release(&psref, &ifp->if_psref, ifnet_psref_class);
1.1       cgd      2700:        }
1.334     ozaki-r  2701:        pserialize_read_exit(s);
                   2702:        curlwp->l_pflag ^= bound ^ LP_BOUND;
                   2703:
1.304     ozaki-r  2704:        if (docopy) {
1.200     gdt      2705:                KASSERT(0 <= space && space <= ifc->ifc_len);
1.127     christos 2706:                ifc->ifc_len -= space;
1.218     dyoung   2707:        } else {
1.200     gdt      2708:                KASSERT(space >= 0);
                   2709:                ifc->ifc_len = space;
                   2710:        }
1.190     enami    2711:        return (0);
1.334     ozaki-r  2712:
                   2713: release_exit:
                   2714:        psref_release(&psref, &ifp->if_psref, ifnet_psref_class);
                   2715:        curlwp->l_pflag ^= bound ^ LP_BOUND;
                   2716:        return error;
1.1       cgd      2717: }
1.133     jonathan 2718:
1.198     dyoung   2719: int
1.247     christos 2720: ifreq_setaddr(u_long cmd, struct ifreq *ifr, const struct sockaddr *sa)
1.198     dyoung   2721: {
                   2722:        uint8_t len;
1.247     christos 2723: #ifdef COMPAT_OIFREQ
                   2724:        struct ifreq ifrb;
                   2725:        struct oifreq *oifr = NULL;
                   2726:        u_long ocmd = cmd;
                   2727:        cmd = compat_cvtcmd(cmd);
                   2728:        if (cmd != ocmd) {
                   2729:                oifr = (struct oifreq *)(void *)ifr;
                   2730:                ifr = &ifrb;
                   2731:                ifreqo2n(oifr, ifr);
                   2732:                len = sizeof(oifr->ifr_addr);
                   2733:        } else
                   2734: #endif
                   2735:                len = sizeof(ifr->ifr_ifru.ifru_space);
1.198     dyoung   2736:
                   2737:        if (len < sa->sa_len)
                   2738:                return EFBIG;
1.247     christos 2739:
1.241     joerg    2740:        memset(&ifr->ifr_addr, 0, len);
1.202     dyoung   2741:        sockaddr_copy(&ifr->ifr_addr, len, sa);
1.247     christos 2742:
                   2743: #ifdef COMPAT_OIFREQ
                   2744:        if (cmd != ocmd)
                   2745:                ifreqn2o(oifr, ifr);
                   2746: #endif
1.198     dyoung   2747:        return 0;
                   2748: }
                   2749:
1.155     christos 2750: /*
1.332     knakahar 2751:  * wrapper function for the drivers which doesn't have if_transmit().
1.155     christos 2752:  */
                   2753: int
1.332     knakahar 2754: if_transmit(struct ifnet *ifp, struct mbuf *m)
1.155     christos 2755: {
1.332     knakahar 2756:        int s, error;
                   2757:
                   2758:        s = splnet();
1.155     christos 2759:
1.330     knakahar 2760:        IFQ_ENQUEUE(&ifp->if_snd, m, error);
1.332     knakahar 2761:        if (error != 0) {
                   2762:                /* mbuf is already freed */
1.185     dyoung   2763:                goto out;
1.332     knakahar 2764:        }
                   2765:
                   2766:        ifp->if_obytes += m->m_pkthdr.len;;
                   2767:        if (m->m_flags & M_MCAST)
1.155     christos 2768:                ifp->if_omcasts++;
1.332     knakahar 2769:
1.155     christos 2770:        if ((ifp->if_flags & IFF_OACTIVE) == 0)
                   2771:                (*ifp->if_start)(ifp);
1.185     dyoung   2772: out:
1.155     christos 2773:        splx(s);
1.332     knakahar 2774:
1.155     christos 2775:        return error;
                   2776: }
                   2777:
                   2778: /*
1.332     knakahar 2779:  * Queue message on interface, and start output if interface
                   2780:  * not yet active.
                   2781:  */
                   2782: int
                   2783: ifq_enqueue(struct ifnet *ifp, struct mbuf *m)
                   2784: {
                   2785:
                   2786:        return (*ifp->if_transmit)(ifp, m);
                   2787: }
                   2788:
                   2789: /*
1.155     christos 2790:  * Queue message on interface, possibly using a second fast queue
                   2791:  */
                   2792: int
1.330     knakahar 2793: ifq_enqueue2(struct ifnet *ifp, struct ifqueue *ifq, struct mbuf *m)
1.155     christos 2794: {
                   2795:        int error = 0;
                   2796:
                   2797:        if (ifq != NULL
                   2798: #ifdef ALTQ
                   2799:            && ALTQ_IS_ENABLED(&ifp->if_snd) == 0
                   2800: #endif
                   2801:            ) {
                   2802:                if (IF_QFULL(ifq)) {
                   2803:                        IF_DROP(&ifp->if_snd);
                   2804:                        m_freem(m);
                   2805:                        if (error == 0)
                   2806:                                error = ENOBUFS;
1.185     dyoung   2807:                } else
1.155     christos 2808:                        IF_ENQUEUE(ifq, m);
                   2809:        } else
1.330     knakahar 2810:                IFQ_ENQUEUE(&ifp->if_snd, m, error);
1.155     christos 2811:        if (error != 0) {
                   2812:                ++ifp->if_oerrors;
                   2813:                return error;
                   2814:        }
                   2815:        return 0;
                   2816: }
                   2817:
1.252     dyoung   2818: int
                   2819: if_addr_init(ifnet_t *ifp, struct ifaddr *ifa, const bool src)
                   2820: {
                   2821:        int rc;
                   2822:
                   2823:        if (ifp->if_initaddr != NULL)
                   2824:                rc = (*ifp->if_initaddr)(ifp, ifa, src);
                   2825:        else if (src ||
1.336     ozaki-r  2826:                /* FIXME: may not hold if_ioctl_lock */
1.252     dyoung   2827:                 (rc = (*ifp->if_ioctl)(ifp, SIOCSIFDSTADDR, ifa)) == ENOTTY)
                   2828:                rc = (*ifp->if_ioctl)(ifp, SIOCINITIFADDR, ifa);
                   2829:
                   2830:        return rc;
                   2831: }
                   2832:
                   2833: int
1.309     roy      2834: if_do_dad(struct ifnet *ifp)
                   2835: {
                   2836:        if ((ifp->if_flags & IFF_LOOPBACK) != 0)
                   2837:                return 0;
                   2838:
                   2839:        switch (ifp->if_type) {
                   2840:        case IFT_FAITH:
                   2841:                /*
                   2842:                 * These interfaces do not have the IFF_LOOPBACK flag,
                   2843:                 * but loop packets back.  We do not have to do DAD on such
                   2844:                 * interfaces.  We should even omit it, because loop-backed
                   2845:                 * responses would confuse the DAD procedure.
                   2846:                 */
                   2847:                return 0;
                   2848:        default:
                   2849:                /*
                   2850:                 * Our DAD routine requires the interface up and running.
                   2851:                 * However, some interfaces can be up before the RUNNING
                   2852:                 * status.  Additionaly, users may try to assign addresses
                   2853:                 * before the interface becomes up (or running).
                   2854:                 * We simply skip DAD in such a case as a work around.
                   2855:                 * XXX: we should rather mark "tentative" on such addresses,
                   2856:                 * and do DAD after the interface becomes ready.
                   2857:                 */
                   2858:                if ((ifp->if_flags & (IFF_UP|IFF_RUNNING)) !=
                   2859:                    (IFF_UP|IFF_RUNNING))
                   2860:                        return 0;
                   2861:
                   2862:                return 1;
                   2863:        }
                   2864: }
                   2865:
                   2866: int
1.252     dyoung   2867: if_flags_set(ifnet_t *ifp, const short flags)
                   2868: {
                   2869:        int rc;
                   2870:
                   2871:        if (ifp->if_setflags != NULL)
                   2872:                rc = (*ifp->if_setflags)(ifp, flags);
                   2873:        else {
1.259     dyoung   2874:                short cantflags, chgdflags;
1.256     dyoung   2875:                struct ifreq ifr;
                   2876:
1.259     dyoung   2877:                chgdflags = ifp->if_flags ^ flags;
                   2878:                cantflags = chgdflags & IFF_CANTCHANGE;
1.256     dyoung   2879:
                   2880:                if (cantflags != 0)
                   2881:                        ifp->if_flags ^= cantflags;
                   2882:
1.259     dyoung   2883:                 /* Traditionally, we do not call if_ioctl after
                   2884:                  * setting/clearing only IFF_PROMISC if the interface
                   2885:                  * isn't IFF_UP.  Uphold that tradition.
                   2886:                 */
                   2887:                if (chgdflags == IFF_PROMISC && (ifp->if_flags & IFF_UP) == 0)
                   2888:                        return 0;
                   2889:
                   2890:                memset(&ifr, 0, sizeof(ifr));
                   2891:
1.256     dyoung   2892:                ifr.ifr_flags = flags & ~IFF_CANTCHANGE;
1.336     ozaki-r  2893:                /* FIXME: may not hold if_ioctl_lock */
1.252     dyoung   2894:                rc = (*ifp->if_ioctl)(ifp, SIOCSIFFLAGS, &ifr);
1.256     dyoung   2895:
                   2896:                if (rc != 0 && cantflags != 0)
                   2897:                        ifp->if_flags ^= cantflags;
1.252     dyoung   2898:        }
                   2899:
                   2900:        return rc;
                   2901: }
                   2902:
                   2903: int
                   2904: if_mcast_op(ifnet_t *ifp, const unsigned long cmd, const struct sockaddr *sa)
                   2905: {
                   2906:        int rc;
                   2907:        struct ifreq ifr;
                   2908:
                   2909:        if (ifp->if_mcastop != NULL)
                   2910:                rc = (*ifp->if_mcastop)(ifp, cmd, sa);
                   2911:        else {
                   2912:                ifreq_setaddr(cmd, &ifr, sa);
                   2913:                rc = (*ifp->if_ioctl)(ifp, cmd, &ifr);
                   2914:        }
                   2915:
                   2916:        return rc;
                   2917: }
1.155     christos 2918:
1.234     dyoung   2919: static void
                   2920: sysctl_sndq_setup(struct sysctllog **clog, const char *ifname,
                   2921:     struct ifaltq *ifq)
                   2922: {
                   2923:        const struct sysctlnode *cnode, *rnode;
                   2924:
                   2925:        if (sysctl_createv(clog, 0, NULL, &rnode,
                   2926:                       CTLFLAG_PERMANENT,
                   2927:                       CTLTYPE_NODE, "interfaces",
                   2928:                       SYSCTL_DESCR("Per-interface controls"),
                   2929:                       NULL, 0, NULL, 0,
1.272     pooka    2930:                       CTL_NET, CTL_CREATE, CTL_EOL) != 0)
1.234     dyoung   2931:                goto bad;
                   2932:
                   2933:        if (sysctl_createv(clog, 0, &rnode, &rnode,
                   2934:                       CTLFLAG_PERMANENT,
                   2935:                       CTLTYPE_NODE, ifname,
                   2936:                       SYSCTL_DESCR("Interface controls"),
                   2937:                       NULL, 0, NULL, 0,
                   2938:                       CTL_CREATE, CTL_EOL) != 0)
                   2939:                goto bad;
                   2940:
                   2941:        if (sysctl_createv(clog, 0, &rnode, &rnode,
                   2942:                       CTLFLAG_PERMANENT,
                   2943:                       CTLTYPE_NODE, "sndq",
                   2944:                       SYSCTL_DESCR("Interface output queue controls"),
                   2945:                       NULL, 0, NULL, 0,
                   2946:                       CTL_CREATE, CTL_EOL) != 0)
                   2947:                goto bad;
                   2948:
                   2949:        if (sysctl_createv(clog, 0, &rnode, &cnode,
                   2950:                       CTLFLAG_PERMANENT,
                   2951:                       CTLTYPE_INT, "len",
                   2952:                       SYSCTL_DESCR("Current output queue length"),
                   2953:                       NULL, 0, &ifq->ifq_len, 0,
                   2954:                       CTL_CREATE, CTL_EOL) != 0)
                   2955:                goto bad;
                   2956:
                   2957:        if (sysctl_createv(clog, 0, &rnode, &cnode,
                   2958:                       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
                   2959:                       CTLTYPE_INT, "maxlen",
                   2960:                       SYSCTL_DESCR("Maximum allowed output queue length"),
                   2961:                       NULL, 0, &ifq->ifq_maxlen, 0,
                   2962:                       CTL_CREATE, CTL_EOL) != 0)
                   2963:                goto bad;
                   2964:
                   2965:        if (sysctl_createv(clog, 0, &rnode, &cnode,
                   2966:                       CTLFLAG_PERMANENT,
                   2967:                       CTLTYPE_INT, "drops",
                   2968:                       SYSCTL_DESCR("Packets dropped due to full output queue"),
                   2969:                       NULL, 0, &ifq->ifq_drops, 0,
                   2970:                       CTL_CREATE, CTL_EOL) != 0)
                   2971:                goto bad;
                   2972:
                   2973:        return;
                   2974: bad:
                   2975:        printf("%s: could not attach sysctl nodes\n", ifname);
                   2976:        return;
                   2977: }
                   2978:
1.138     drochner 2979: #if defined(INET) || defined(INET6)
1.276     rmind    2980:
                   2981: #define        SYSCTL_NET_PKTQ(q, cn, c)                                       \
                   2982:        static int                                                      \
                   2983:        sysctl_net_##q##_##cn(SYSCTLFN_ARGS)                            \
                   2984:        {                                                               \
                   2985:                return sysctl_pktq_count(SYSCTLFN_CALL(rnode), q, c);   \
                   2986:        }
                   2987:
                   2988: #if defined(INET)
1.279     rmind    2989: static int
                   2990: sysctl_net_ip_pktq_maxlen(SYSCTLFN_ARGS)
                   2991: {
                   2992:        return sysctl_pktq_maxlen(SYSCTLFN_CALL(rnode), ip_pktq);
                   2993: }
1.276     rmind    2994: SYSCTL_NET_PKTQ(ip_pktq, items, PKTQ_NITEMS)
                   2995: SYSCTL_NET_PKTQ(ip_pktq, drops, PKTQ_DROPS)
                   2996: #endif
1.279     rmind    2997:
1.276     rmind    2998: #if defined(INET6)
1.279     rmind    2999: static int
                   3000: sysctl_net_ip6_pktq_maxlen(SYSCTLFN_ARGS)
                   3001: {
                   3002:        return sysctl_pktq_maxlen(SYSCTLFN_CALL(rnode), ip6_pktq);
                   3003: }
1.276     rmind    3004: SYSCTL_NET_PKTQ(ip6_pktq, items, PKTQ_NITEMS)
                   3005: SYSCTL_NET_PKTQ(ip6_pktq, drops, PKTQ_DROPS)
                   3006: #endif
                   3007:
1.136     atatat   3008: static void
1.276     rmind    3009: sysctl_net_pktq_setup(struct sysctllog **clog, int pf)
1.136     atatat   3010: {
1.276     rmind    3011:        sysctlfn len_func = NULL, maxlen_func = NULL, drops_func = NULL;
                   3012:        const char *pfname = NULL, *ipname = NULL;
                   3013:        int ipn = 0, qid = 0;
                   3014:
                   3015:        switch (pf) {
                   3016: #if defined(INET)
                   3017:        case PF_INET:
                   3018:                len_func = sysctl_net_ip_pktq_items;
                   3019:                maxlen_func = sysctl_net_ip_pktq_maxlen;
                   3020:                drops_func = sysctl_net_ip_pktq_drops;
                   3021:                pfname = "inet", ipn = IPPROTO_IP;
                   3022:                ipname = "ip", qid = IPCTL_IFQ;
                   3023:                break;
                   3024: #endif
                   3025: #if defined(INET6)
                   3026:        case PF_INET6:
                   3027:                len_func = sysctl_net_ip6_pktq_items;
                   3028:                maxlen_func = sysctl_net_ip6_pktq_maxlen;
                   3029:                drops_func = sysctl_net_ip6_pktq_drops;
                   3030:                pfname = "inet6", ipn = IPPROTO_IPV6;
                   3031:                ipname = "ip6", qid = IPV6CTL_IFQ;
                   3032:                break;
                   3033: #endif
                   3034:        default:
                   3035:                KASSERT(false);
                   3036:        }
1.136     atatat   3037:
1.139     atatat   3038:        sysctl_createv(clog, 0, NULL, NULL,
                   3039:                       CTLFLAG_PERMANENT,
1.136     atatat   3040:                       CTLTYPE_NODE, pfname, NULL,
                   3041:                       NULL, 0, NULL, 0,
                   3042:                       CTL_NET, pf, CTL_EOL);
1.139     atatat   3043:        sysctl_createv(clog, 0, NULL, NULL,
                   3044:                       CTLFLAG_PERMANENT,
1.136     atatat   3045:                       CTLTYPE_NODE, ipname, NULL,
                   3046:                       NULL, 0, NULL, 0,
                   3047:                       CTL_NET, pf, ipn, CTL_EOL);
1.139     atatat   3048:        sysctl_createv(clog, 0, NULL, NULL,
                   3049:                       CTLFLAG_PERMANENT,
1.142     atatat   3050:                       CTLTYPE_NODE, "ifq",
                   3051:                       SYSCTL_DESCR("Protocol input queue controls"),
1.139     atatat   3052:                       NULL, 0, NULL, 0,
                   3053:                       CTL_NET, pf, ipn, qid, CTL_EOL);
1.136     atatat   3054:
1.139     atatat   3055:        sysctl_createv(clog, 0, NULL, NULL,
                   3056:                       CTLFLAG_PERMANENT,
1.142     atatat   3057:                       CTLTYPE_INT, "len",
                   3058:                       SYSCTL_DESCR("Current input queue length"),
1.276     rmind    3059:                       len_func, 0, NULL, 0,
1.136     atatat   3060:                       CTL_NET, pf, ipn, qid, IFQCTL_LEN, CTL_EOL);
1.139     atatat   3061:        sysctl_createv(clog, 0, NULL, NULL,
                   3062:                       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
1.142     atatat   3063:                       CTLTYPE_INT, "maxlen",
                   3064:                       SYSCTL_DESCR("Maximum allowed input queue length"),
1.276     rmind    3065:                       maxlen_func, 0, NULL, 0,
1.136     atatat   3066:                       CTL_NET, pf, ipn, qid, IFQCTL_MAXLEN, CTL_EOL);
1.139     atatat   3067:        sysctl_createv(clog, 0, NULL, NULL,
                   3068:                       CTLFLAG_PERMANENT,
1.142     atatat   3069:                       CTLTYPE_INT, "drops",
                   3070:                       SYSCTL_DESCR("Packets dropped due to full input queue"),
1.276     rmind    3071:                       drops_func, 0, NULL, 0,
1.136     atatat   3072:                       CTL_NET, pf, ipn, qid, IFQCTL_DROPS, CTL_EOL);
                   3073: }
1.138     drochner 3074: #endif /* INET || INET6 */
1.280     joerg    3075:
                   3076: static int
                   3077: if_sdl_sysctl(SYSCTLFN_ARGS)
                   3078: {
                   3079:        struct ifnet *ifp;
                   3080:        const struct sockaddr_dl *sdl;
                   3081:
                   3082:        if (namelen != 1)
                   3083:                return EINVAL;
                   3084:
                   3085:        ifp = if_byindex(name[0]);
                   3086:        if (ifp == NULL)
                   3087:                return ENODEV;
                   3088:
                   3089:        sdl = ifp->if_sadl;
                   3090:        if (sdl == NULL) {
                   3091:                *oldlenp = 0;
                   3092:                return 0;
                   3093:        }
                   3094:
                   3095:        if (oldp == NULL) {
                   3096:                *oldlenp = sdl->sdl_alen;
                   3097:                return 0;
                   3098:        }
                   3099:
                   3100:        if (*oldlenp >= sdl->sdl_alen)
                   3101:                *oldlenp = sdl->sdl_alen;
                   3102:        return sysctl_copyout(l, &sdl->sdl_data[sdl->sdl_nlen], oldp, *oldlenp);
                   3103: }
                   3104:
                   3105: SYSCTL_SETUP(sysctl_net_sdl_setup, "sysctl net.sdl subtree setup")
                   3106: {
                   3107:        const struct sysctlnode *rnode = NULL;
                   3108:
                   3109:        sysctl_createv(clog, 0, NULL, &rnode,
                   3110:                       CTLFLAG_PERMANENT,
                   3111:                       CTLTYPE_NODE, "sdl",
                   3112:                       SYSCTL_DESCR("Get active link-layer address"),
                   3113:                       if_sdl_sysctl, 0, NULL, 0,
                   3114:                       CTL_NET, CTL_CREATE, CTL_EOL);
                   3115: }

CVSweb <webmaster@jp.NetBSD.org>