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

1.145   ! itojun      1: /*     $NetBSD: if.c,v 1.144 2004/07/27 12:22:59 yamt Exp $    */
1.53      thorpej     2:
                      3: /*-
1.89      thorpej     4:  * Copyright (c) 1999, 2000, 2001 The NetBSD Foundation, Inc.
1.53      thorpej     5:  * All rights reserved.
                      6:  *
                      7:  * This code is derived from software contributed to The NetBSD Foundation
                      8:  * by William Studnemund and Jason R. Thorpe.
                      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:  * 3. All advertising materials mentioning features or use of this software
                     19:  *    must display the following acknowledgement:
                     20:  *     This product includes software developed by the NetBSD
                     21:  *     Foundation, Inc. and its contributors.
                     22:  * 4. Neither the name of The NetBSD Foundation nor the names of its
                     23:  *    contributors may be used to endorse or promote products derived
                     24:  *    from this software without specific prior written permission.
                     25:  *
                     26:  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
                     27:  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
                     28:  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
                     29:  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
                     30:  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
                     31:  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
                     32:  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
                     33:  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
                     34:  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
                     35:  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
                     36:  * POSSIBILITY OF SUCH DAMAGE.
                     37:  */
1.49      itojun     38:
                     39: /*
                     40:  * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
                     41:  * All rights reserved.
                     42:  *
                     43:  * Redistribution and use in source and binary forms, with or without
                     44:  * modification, are permitted provided that the following conditions
                     45:  * are met:
                     46:  * 1. Redistributions of source code must retain the above copyright
                     47:  *    notice, this list of conditions and the following disclaimer.
                     48:  * 2. Redistributions in binary form must reproduce the above copyright
                     49:  *    notice, this list of conditions and the following disclaimer in the
                     50:  *    documentation and/or other materials provided with the distribution.
                     51:  * 3. Neither the name of the project nor the names of its contributors
                     52:  *    may be used to endorse or promote products derived from this software
                     53:  *    without specific prior written permission.
                     54:  *
                     55:  * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
                     56:  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
                     57:  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
                     58:  * ARE DISCLAIMED.  IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
                     59:  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
                     60:  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
                     61:  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
                     62:  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
                     63:  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
                     64:  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
                     65:  * SUCH DAMAGE.
                     66:  */
1.16      cgd        67:
1.1       cgd        68: /*
1.15      mycroft    69:  * Copyright (c) 1980, 1986, 1993
                     70:  *     The Regents of the University of California.  All rights reserved.
1.1       cgd        71:  *
                     72:  * Redistribution and use in source and binary forms, with or without
                     73:  * modification, are permitted provided that the following conditions
                     74:  * are met:
                     75:  * 1. Redistributions of source code must retain the above copyright
                     76:  *    notice, this list of conditions and the following disclaimer.
                     77:  * 2. Redistributions in binary form must reproduce the above copyright
                     78:  *    notice, this list of conditions and the following disclaimer in the
                     79:  *    documentation and/or other materials provided with the distribution.
1.126     agc        80:  * 3. Neither the name of the University nor the names of its contributors
1.1       cgd        81:  *    may be used to endorse or promote products derived from this software
                     82:  *    without specific prior written permission.
                     83:  *
                     84:  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
                     85:  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
                     86:  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
                     87:  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
                     88:  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
                     89:  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
                     90:  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
                     91:  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
                     92:  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
                     93:  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
                     94:  * SUCH DAMAGE.
                     95:  *
1.44      fvdl       96:  *     @(#)if.c        8.5 (Berkeley) 1/9/95
1.1       cgd        97:  */
1.99      lukem      98:
                     99: #include <sys/cdefs.h>
1.145   ! itojun    100: __KERNEL_RCSID(0, "$NetBSD: if.c,v 1.144 2004/07/27 12:22:59 yamt Exp $");
1.50      thorpej   101:
                    102: #include "opt_inet.h"
1.46      thorpej   103:
                    104: #include "opt_compat_linux.h"
1.47      thorpej   105: #include "opt_compat_svr4.h"
1.119     christos  106: #include "opt_compat_ultrix.h"
1.48      christos  107: #include "opt_compat_43.h"
1.51      bouyer    108: #include "opt_atalk.h"
1.120     martin    109: #include "opt_ccitt.h"
                    110: #include "opt_natm.h"
1.87      thorpej   111: #include "opt_pfil_hooks.h"
1.1       cgd       112:
1.8       mycroft   113: #include <sys/param.h>
                    114: #include <sys/mbuf.h>
                    115: #include <sys/systm.h>
1.59      thorpej   116: #include <sys/callout.h>
1.15      mycroft   117: #include <sys/proc.h>
1.8       mycroft   118: #include <sys/socket.h>
                    119: #include <sys/socketvar.h>
1.56      thorpej   120: #include <sys/domain.h>
1.8       mycroft   121: #include <sys/protosw.h>
                    122: #include <sys/kernel.h>
                    123: #include <sys/ioctl.h>
1.133     jonathan  124: #include <sys/sysctl.h>
1.1       cgd       125:
1.8       mycroft   126: #include <net/if.h>
                    127: #include <net/if_dl.h>
1.66      onoe      128: #include <net/if_ether.h>
1.124     dyoung    129: #include <net/if_media.h>
1.132     dyoung    130: #include <net80211/ieee80211.h>
                    131: #include <net80211/ieee80211_ioctl.h>
1.8       mycroft   132: #include <net/if_types.h>
1.24      christos  133: #include <net/radix.h>
1.53      thorpej   134: #include <net/route.h>
1.95      itojun    135: #include <net/netisr.h>
1.51      bouyer    136: #ifdef NETATALK
                    137: #include <netatalk/at_extern.h>
                    138: #include <netatalk/at.h>
                    139: #endif
1.143     itojun    140: #include <net/pfil.h>
1.1       cgd       141:
1.49      itojun    142: #ifdef INET6
                    143: #include <netinet/in.h>
1.72      thorpej   144: #include <netinet6/in6_var.h>
1.108     itojun    145: #include <netinet6/nd6.h>
1.49      itojun    146: #endif
1.117     thorpej   147:
                    148: MALLOC_DEFINE(M_IFADDR, "ifaddr", "interface address");
                    149: MALLOC_DEFINE(M_IFMADDR, "ether_multi", "link-level multicast address");
1.49      itojun    150:
1.1       cgd       151: int    ifqmaxlen = IFQ_MAXLEN;
1.59      thorpej   152: struct callout if_slowtimo_ch;
1.49      itojun    153:
1.104     matt      154: int netisr;                    /* scheduling bits for network */
                    155:
                    156: int if_rt_walktree __P((struct radix_node *, void *));
1.53      thorpej   157:
1.63      thorpej   158: struct if_clone *if_clone_lookup __P((const char *, int *));
1.67      thorpej   159: int if_clone_list __P((struct if_clonereq *));
1.63      thorpej   160:
                    161: LIST_HEAD(, if_clone) if_cloners = LIST_HEAD_INITIALIZER(if_cloners);
1.67      thorpej   162: int if_cloners_count;
1.63      thorpej   163:
1.143     itojun    164: #ifdef PFIL_HOOKS
                    165: struct pfil_head if_pfil;      /* packet filtering hook for interfaces */
                    166: #endif
                    167:
1.101     abs       168: #if defined(INET) || defined(INET6) || defined(NETATALK) || defined(NS) || \
                    169:     defined(ISO) || defined(CCITT) || defined(NATM)
1.95      itojun    170: static void if_detach_queues __P((struct ifnet *, struct ifqueue *));
1.101     abs       171: #endif
1.95      itojun    172:
1.1       cgd       173: /*
                    174:  * Network interface utility routines.
                    175:  *
                    176:  * Routines with ifa_ifwith* names take sockaddr *'s as
                    177:  * parameters.
                    178:  */
1.4       andrew    179: void
1.1       cgd       180: ifinit()
                    181: {
                    182:
1.59      thorpej   183:        callout_init(&if_slowtimo_ch);
1.4       andrew    184:        if_slowtimo(NULL);
1.143     itojun    185: #ifdef PFIL_HOOKS
                    186:        if_pfil.ph_type = PFIL_TYPE_IFNET;
                    187:        if_pfil.ph_ifnet = NULL;
                    188:        if (pfil_head_register(&if_pfil) != 0)
                    189:                printf("WARNING: unable to register pfil hook\n");
                    190: #endif
1.1       cgd       191: }
                    192:
1.53      thorpej   193: /*
                    194:  * Null routines used while an interface is going away.  These routines
                    195:  * just return an error.
                    196:  */
                    197:
                    198: int
                    199: if_nulloutput(ifp, m, so, rt)
                    200:        struct ifnet *ifp;
                    201:        struct mbuf *m;
                    202:        struct sockaddr *so;
                    203:        struct rtentry *rt;
                    204: {
                    205:
                    206:        return (ENXIO);
                    207: }
                    208:
                    209: void
                    210: if_nullinput(ifp, m)
                    211:        struct ifnet *ifp;
                    212:        struct mbuf *m;
                    213: {
                    214:
                    215:        /* Nothing. */
                    216: }
                    217:
                    218: void
                    219: if_nullstart(ifp)
                    220:        struct ifnet *ifp;
                    221: {
                    222:
                    223:        /* Nothing. */
                    224: }
                    225:
                    226: int
                    227: if_nullioctl(ifp, cmd, data)
                    228:        struct ifnet *ifp;
                    229:        u_long cmd;
                    230:        caddr_t data;
                    231: {
                    232:
                    233:        return (ENXIO);
                    234: }
                    235:
                    236: int
1.75      thorpej   237: if_nullinit(ifp)
1.53      thorpej   238:        struct ifnet *ifp;
                    239: {
                    240:
                    241:        return (ENXIO);
                    242: }
                    243:
                    244: void
1.75      thorpej   245: if_nullstop(ifp, disable)
                    246:        struct ifnet *ifp;
                    247:        int disable;
                    248: {
                    249:
                    250:        /* Nothing. */
                    251: }
                    252:
                    253: void
1.53      thorpej   254: if_nullwatchdog(ifp)
                    255:        struct ifnet *ifp;
                    256: {
                    257:
                    258:        /* Nothing. */
                    259: }
                    260:
                    261: void
                    262: if_nulldrain(ifp)
                    263:        struct ifnet *ifp;
                    264: {
                    265:
                    266:        /* Nothing. */
                    267: }
                    268:
1.137     itojun    269: static u_int if_index = 1;
1.103     simonb    270: struct ifnet_head ifnet;
1.137     itojun    271: size_t if_indexlim = 0;
1.49      itojun    272: struct ifaddr **ifnet_addrs = NULL;
                    273: struct ifnet **ifindex2ifnet = NULL;
1.1       cgd       274:
                    275: /*
1.81      thorpej   276:  * Allocate the link level name for the specified interface.  This
                    277:  * is an attachment helper.  It must be called after ifp->if_addrlen
                    278:  * is initialized, which may not be the case when if_attach() is
                    279:  * called.
                    280:  */
                    281: void
                    282: if_alloc_sadl(struct ifnet *ifp)
                    283: {
                    284:        unsigned socksize, ifasize;
                    285:        int namelen, masklen;
                    286:        struct sockaddr_dl *sdl;
                    287:        struct ifaddr *ifa;
1.84      thorpej   288:
                    289:        /*
                    290:         * If the interface already has a link name, release it
                    291:         * now.  This is useful for interfaces that can change
                    292:         * link types, and thus switch link names often.
                    293:         */
                    294:        if (ifp->if_sadl != NULL)
                    295:                if_free_sadl(ifp);
1.81      thorpej   296:
                    297:        namelen = strlen(ifp->if_xname);
                    298:        masklen = offsetof(struct sockaddr_dl, sdl_data[0]) + namelen;
                    299:        socksize = masklen + ifp->if_addrlen;
                    300: #define ROUNDUP(a) (1 + (((a) - 1) | (sizeof(long) - 1)))
                    301:        if (socksize < sizeof(*sdl))
                    302:                socksize = sizeof(*sdl);
                    303:        socksize = ROUNDUP(socksize);
                    304:        ifasize = sizeof(*ifa) + 2 * socksize;
                    305:        ifa = (struct ifaddr *)malloc(ifasize, M_IFADDR, M_WAITOK);
1.92      thorpej   306:        memset((caddr_t)ifa, 0, ifasize);
1.81      thorpej   307:        sdl = (struct sockaddr_dl *)(ifa + 1);
                    308:        sdl->sdl_len = socksize;
                    309:        sdl->sdl_family = AF_LINK;
                    310:        bcopy(ifp->if_xname, sdl->sdl_data, namelen);
                    311:        sdl->sdl_nlen = namelen;
1.83      itojun    312:        sdl->sdl_alen = ifp->if_addrlen;
1.81      thorpej   313:        sdl->sdl_index = ifp->if_index;
                    314:        sdl->sdl_type = ifp->if_type;
                    315:        ifnet_addrs[ifp->if_index] = ifa;
                    316:        IFAREF(ifa);
                    317:        ifa->ifa_ifp = ifp;
                    318:        ifa->ifa_rtrequest = link_rtrequest;
                    319:        TAILQ_INSERT_HEAD(&ifp->if_addrlist, ifa, ifa_list);
                    320:        IFAREF(ifa);
                    321:        ifa->ifa_addr = (struct sockaddr *)sdl;
                    322:        ifp->if_sadl = sdl;
                    323:        sdl = (struct sockaddr_dl *)(socksize + (caddr_t)sdl);
                    324:        ifa->ifa_netmask = (struct sockaddr *)sdl;
                    325:        sdl->sdl_len = masklen;
                    326:        while (namelen != 0)
                    327:                sdl->sdl_data[--namelen] = 0xff;
                    328: }
                    329:
                    330: /*
                    331:  * Free the link level name for the specified interface.  This is
                    332:  * a detach helper.  This is called from if_detach() or from
                    333:  * link layer type specific detach functions.
                    334:  */
                    335: void
                    336: if_free_sadl(struct ifnet *ifp)
                    337: {
                    338:        struct ifaddr *ifa;
                    339:        int s;
                    340:
                    341:        ifa = ifnet_addrs[ifp->if_index];
                    342:        if (ifa == NULL) {
                    343:                KASSERT(ifp->if_sadl == NULL);
                    344:                return;
                    345:        }
                    346:
                    347:        KASSERT(ifp->if_sadl != NULL);
                    348:
1.88      thorpej   349:        s = splnet();
1.81      thorpej   350:        rtinit(ifa, RTM_DELETE, 0);
                    351:        TAILQ_REMOVE(&ifp->if_addrlist, ifa, ifa_list);
                    352:        IFAFREE(ifa);
                    353:
                    354:        ifp->if_sadl = NULL;
                    355:
                    356:        ifnet_addrs[ifp->if_index] = NULL;
                    357:        IFAFREE(ifa);
                    358:        splx(s);
                    359: }
                    360:
                    361: /*
1.1       cgd       362:  * Attach an interface to the
                    363:  * list of "active" interfaces.
                    364:  */
1.15      mycroft   365: void
1.1       cgd       366: if_attach(ifp)
                    367:        struct ifnet *ifp;
                    368: {
1.102     atatat    369:        int indexlim = 0;
1.1       cgd       370:
1.102     atatat    371:        if (if_indexlim == 0) {
1.22      mycroft   372:                TAILQ_INIT(&ifnet);
1.102     atatat    373:                if_indexlim = 8;
                    374:        }
1.22      mycroft   375:        TAILQ_INIT(&ifp->if_addrlist);
1.21      mycroft   376:        TAILQ_INSERT_TAIL(&ifnet, ifp, if_list);
1.102     atatat    377:        ifp->if_index = if_index;
                    378:        if (ifindex2ifnet == 0)
                    379:                if_index++;
                    380:        else
1.131     itojun    381:                while (ifp->if_index < if_indexlim &&
                    382:                    ifindex2ifnet[ifp->if_index] != NULL) {
1.102     atatat    383:                        ++if_index;
                    384:                        if (if_index == 0)
                    385:                                if_index = 1;
                    386:                        /*
                    387:                         * If we hit USHRT_MAX, we skip back to 0 since
                    388:                         * there are a number of places where the value
                    389:                         * of if_index or if_index itself is compared
1.111     itojun    390:                         * to or stored in an unsigned short.  By
1.102     atatat    391:                         * jumping back, we won't botch those assignments
                    392:                         * or comparisons.
                    393:                         */
                    394:                        else if (if_index == USHRT_MAX) {
                    395:                                /*
                    396:                                 * However, if we have to jump back to
                    397:                                 * zero *twice* without finding an empty
                    398:                                 * slot in ifindex2ifnet[], then there
                    399:                                 * there are too many (>65535) interfaces.
                    400:                                 */
                    401:                                if (indexlim++)
                    402:                                        panic("too many interfaces");
                    403:                                else
                    404:                                        if_index = 1;
                    405:                        }
                    406:                        ifp->if_index = if_index;
                    407:                }
1.49      itojun    408:
                    409:        /*
                    410:         * We have some arrays that should be indexed by if_index.
                    411:         * since if_index will grow dynamically, they should grow too.
                    412:         *      struct ifadd **ifnet_addrs
                    413:         *      struct ifnet **ifindex2ifnet
                    414:         */
1.53      thorpej   415:        if (ifnet_addrs == 0 || ifindex2ifnet == 0 ||
                    416:            ifp->if_index >= if_indexlim) {
1.131     itojun    417:                size_t m, n, oldlim;
1.49      itojun    418:                caddr_t q;
                    419:
1.131     itojun    420:                oldlim = if_indexlim;
1.53      thorpej   421:                while (ifp->if_index >= if_indexlim)
1.49      itojun    422:                        if_indexlim <<= 1;
                    423:
                    424:                /* grow ifnet_addrs */
1.131     itojun    425:                m = oldlim * sizeof(struct ifaddr *);
1.81      thorpej   426:                n = if_indexlim * sizeof(struct ifaddr *);
1.49      itojun    427:                q = (caddr_t)malloc(n, M_IFADDR, M_WAITOK);
1.92      thorpej   428:                memset(q, 0, n);
1.1       cgd       429:                if (ifnet_addrs) {
1.131     itojun    430:                        bcopy((caddr_t)ifnet_addrs, q, m);
1.1       cgd       431:                        free((caddr_t)ifnet_addrs, M_IFADDR);
                    432:                }
1.49      itojun    433:                ifnet_addrs = (struct ifaddr **)q;
                    434:
                    435:                /* grow ifindex2ifnet */
1.131     itojun    436:                m = oldlim * sizeof(struct ifnet *);
1.49      itojun    437:                n = if_indexlim * sizeof(struct ifnet *);
                    438:                q = (caddr_t)malloc(n, M_IFADDR, M_WAITOK);
1.92      thorpej   439:                memset(q, 0, n);
1.49      itojun    440:                if (ifindex2ifnet) {
1.131     itojun    441:                        bcopy((caddr_t)ifindex2ifnet, q, m);
1.49      itojun    442:                        free((caddr_t)ifindex2ifnet, M_IFADDR);
                    443:                }
                    444:                ifindex2ifnet = (struct ifnet **)q;
1.1       cgd       445:        }
1.49      itojun    446:
1.53      thorpej   447:        ifindex2ifnet[ifp->if_index] = ifp;
1.49      itojun    448:
1.1       cgd       449:        /*
1.81      thorpej   450:         * Link level name is allocated later by a separate call to
                    451:         * if_alloc_sadl().
1.1       cgd       452:         */
1.81      thorpej   453:
1.40      thorpej   454:        if (ifp->if_snd.ifq_maxlen == 0)
1.94      itojun    455:                ifp->if_snd.ifq_maxlen = ifqmaxlen;
1.42      is        456:        ifp->if_broadcastaddr = 0; /* reliably crash if used uninitialized */
1.57      thorpej   457:
                    458:        ifp->if_link_state = LINK_STATE_UNKNOWN;
                    459:
1.89      thorpej   460:        ifp->if_capenable = 0;
1.97      thorpej   461:        ifp->if_csum_flags_tx = 0;
                    462:        ifp->if_csum_flags_rx = 0;
1.89      thorpej   463:
1.86      thorpej   464: #ifdef ALTQ
                    465:        ifp->if_snd.altq_type = 0;
                    466:        ifp->if_snd.altq_disc = NULL;
                    467:        ifp->if_snd.altq_flags &= ALTQF_CANTCHANGE;
                    468:        ifp->if_snd.altq_tbr  = NULL;
                    469:        ifp->if_snd.altq_ifp  = ifp;
                    470: #endif
                    471:
1.87      thorpej   472: #ifdef PFIL_HOOKS
                    473:        ifp->if_pfil.ph_type = PFIL_TYPE_IFNET;
                    474:        ifp->if_pfil.ph_ifnet = ifp;
                    475:        if (pfil_head_register(&ifp->if_pfil) != 0)
                    476:                printf("%s: WARNING: unable to register pfil hook\n",
                    477:                    ifp->if_xname);
1.144     yamt      478:        (void)pfil_run_hooks(&if_pfil,
                    479:            (struct mbuf **)PFIL_IFNET_ATTACH, ifp, PFIL_IFNET);
1.87      thorpej   480: #endif
                    481:
1.107     itojun    482:        /* Announce the interface. */
                    483:        rt_ifannouncemsg(ifp, IFAN_ARRIVAL);
                    484: }
                    485:
                    486: void
                    487: if_attachdomain()
                    488: {
                    489:        struct ifnet *ifp;
1.110     itojun    490:        int s;
1.107     itojun    491:
1.110     itojun    492:        s = splnet();
1.107     itojun    493:        for (ifp = TAILQ_FIRST(&ifnet); ifp; ifp = TAILQ_NEXT(ifp, if_list))
                    494:                if_attachdomain1(ifp);
1.110     itojun    495:        splx(s);
1.107     itojun    496: }
                    497:
                    498: void
                    499: if_attachdomain1(ifp)
                    500:        struct ifnet *ifp;
                    501: {
                    502:        struct domain *dp;
1.109     itojun    503:        int s;
                    504:
                    505:        s = splnet();
1.107     itojun    506:
1.106     itojun    507:        /* address family dependent data region */
                    508:        memset(ifp->if_afdata, 0, sizeof(ifp->if_afdata));
                    509:        for (dp = domains; dp; dp = dp->dom_next) {
                    510:                if (dp->dom_ifattach)
                    511:                        ifp->if_afdata[dp->dom_family] =
                    512:                            (*dp->dom_ifattach)(ifp);
                    513:        }
1.109     itojun    514:
                    515:        splx(s);
1.1       cgd       516: }
1.53      thorpej   517:
                    518: /*
                    519:  * Deactivate an interface.  This points all of the procedure
                    520:  * handles at error stubs.  May be called from interrupt context.
                    521:  */
                    522: void
                    523: if_deactivate(ifp)
                    524:        struct ifnet *ifp;
                    525: {
                    526:        int s;
                    527:
1.88      thorpej   528:        s = splnet();
1.53      thorpej   529:
                    530:        ifp->if_output   = if_nulloutput;
                    531:        ifp->if_input    = if_nullinput;
                    532:        ifp->if_start    = if_nullstart;
                    533:        ifp->if_ioctl    = if_nullioctl;
1.75      thorpej   534:        ifp->if_init     = if_nullinit;
                    535:        ifp->if_stop     = if_nullstop;
1.53      thorpej   536:        ifp->if_watchdog = if_nullwatchdog;
                    537:        ifp->if_drain    = if_nulldrain;
                    538:
                    539:        /* No more packets may be enqueued. */
                    540:        ifp->if_snd.ifq_maxlen = 0;
                    541:
                    542:        splx(s);
                    543: }
                    544:
                    545: /*
                    546:  * Detach an interface from the list of "active" interfaces,
                    547:  * freeing any resources as we go along.
                    548:  *
                    549:  * NOTE: This routine must be called with a valid thread context,
                    550:  * as it may block.
                    551:  */
                    552: void
                    553: if_detach(ifp)
                    554:        struct ifnet *ifp;
                    555: {
1.56      thorpej   556:        struct socket so;
1.134     drochner  557:        struct ifaddr *ifa, **ifap;
1.53      thorpej   558: #ifdef IFAREF_DEBUG
                    559:        struct ifaddr *last_ifa = NULL;
                    560: #endif
1.56      thorpej   561:        struct domain *dp;
1.141     matt      562:        const struct protosw *pr;
1.53      thorpej   563:        struct radix_node_head *rnh;
1.56      thorpej   564:        int s, i, family, purged;
1.53      thorpej   565:
1.56      thorpej   566:        /*
                    567:         * XXX It's kind of lame that we have to have the
                    568:         * XXX socket structure...
                    569:         */
                    570:        memset(&so, 0, sizeof(so));
1.53      thorpej   571:
1.88      thorpej   572:        s = splnet();
1.53      thorpej   573:
                    574:        /*
                    575:         * Do an if_down() to give protocols a chance to do something.
                    576:         */
                    577:        if_down(ifp);
1.86      thorpej   578:
                    579: #ifdef ALTQ
                    580:        if (ALTQ_IS_ENABLED(&ifp->if_snd))
                    581:                altq_disable(&ifp->if_snd);
                    582:        if (ALTQ_IS_ATTACHED(&ifp->if_snd))
                    583:                altq_detach(&ifp->if_snd);
1.87      thorpej   584: #endif
                    585:
                    586: #ifdef PFIL_HOOKS
1.144     yamt      587:        (void)pfil_run_hooks(&if_pfil,
                    588:            (struct mbuf **)PFIL_IFNET_DETACH, ifp, PFIL_IFNET);
                    589:        (void)pfil_head_unregister(&ifp->if_pfil);
1.86      thorpej   590: #endif
1.53      thorpej   591:
                    592:        /*
                    593:         * Rip all the addresses off the interface.  This should make
                    594:         * all of the routes go away.
                    595:         */
1.134     drochner  596:        ifap = &TAILQ_FIRST(&ifp->if_addrlist); /* XXX abstraction violation */
                    597:        while ((ifa = *ifap)) {
1.56      thorpej   598:                family = ifa->ifa_addr->sa_family;
1.53      thorpej   599: #ifdef IFAREF_DEBUG
                    600:                printf("if_detach: ifaddr %p, family %d, refcnt %d\n",
1.56      thorpej   601:                    ifa, family, ifa->ifa_refcnt);
1.53      thorpej   602:                if (last_ifa != NULL && ifa == last_ifa)
1.56      thorpej   603:                        panic("if_detach: loop detected");
1.53      thorpej   604:                last_ifa = ifa;
                    605: #endif
1.134     drochner  606:                if (family == AF_LINK) {
                    607:                        ifap = &TAILQ_NEXT(ifa, ifa_list);
1.118     itojun    608:                        continue;
1.134     drochner  609:                }
1.118     itojun    610:                dp = pffinddomain(family);
1.56      thorpej   611: #ifdef DIAGNOSTIC
1.118     itojun    612:                if (dp == NULL)
                    613:                        panic("if_detach: no domain for AF %d",
                    614:                            family);
1.56      thorpej   615: #endif
1.118     itojun    616:                purged = 0;
                    617:                for (pr = dp->dom_protosw;
                    618:                     pr < dp->dom_protoswNPROTOSW; pr++) {
                    619:                        so.so_proto = pr;
                    620:                        if (pr->pr_usrreq != NULL) {
                    621:                                (void) (*pr->pr_usrreq)(&so,
                    622:                                    PRU_PURGEIF, NULL, NULL,
1.123     fvdl      623:                                    (struct mbuf *) ifp, curproc);
1.118     itojun    624:                                purged = 1;
1.53      thorpej   625:                        }
1.118     itojun    626:                }
                    627:                if (purged == 0) {
                    628:                        /*
                    629:                         * XXX What's really the best thing to do
1.135     keihan    630:                         * XXX here?  --thorpej@NetBSD.org
1.118     itojun    631:                         */
                    632:                        printf("if_detach: WARNING: AF %d not purged\n",
                    633:                            family);
1.134     drochner  634:                        TAILQ_REMOVE(&ifp->if_addrlist, ifa, ifa_list);
1.53      thorpej   635:                }
                    636:        }
1.118     itojun    637:
                    638:        if_free_sadl(ifp);
1.53      thorpej   639:
                    640:        /* Walk the routing table looking for straglers. */
1.54      thorpej   641:        for (i = 0; i <= AF_MAX; i++) {
                    642:                if ((rnh = rt_tables[i]) != NULL)
                    643:                        (void) (*rnh->rnh_walktree)(rnh, if_rt_walktree, ifp);
1.106     itojun    644:        }
                    645:
                    646:        for (dp = domains; dp; dp = dp->dom_next) {
                    647:                if (dp->dom_ifdetach && ifp->if_afdata[dp->dom_family])
                    648:                        (*dp->dom_ifdetach)(ifp,
                    649:                            ifp->if_afdata[dp->dom_family]);
1.53      thorpej   650:        }
1.57      thorpej   651:
                    652:        /* Announce that the interface is gone. */
                    653:        rt_ifannouncemsg(ifp, IFAN_DEPARTURE);
1.93      itojun    654:
                    655:        ifindex2ifnet[ifp->if_index] = NULL;
1.53      thorpej   656:
                    657:        TAILQ_REMOVE(&ifnet, ifp, if_list);
                    658:
1.95      itojun    659:        /*
                    660:         * remove packets came from ifp, from software interrupt queues.
                    661:         * net/netisr_dispatch.h is not usable, as some of them use
                    662:         * strange queue names.
                    663:         */
                    664: #define IF_DETACH_QUEUES(x) \
                    665: do { \
                    666:        extern struct ifqueue x; \
                    667:        if_detach_queues(ifp, & x); \
1.116     perry     668: } while (/*CONSTCOND*/ 0)
1.95      itojun    669: #ifdef INET
                    670: #if NARP > 0
                    671:        IF_DETACH_QUEUES(arpintrq);
                    672: #endif
                    673:        IF_DETACH_QUEUES(ipintrq);
                    674: #endif
                    675: #ifdef INET6
                    676:        IF_DETACH_QUEUES(ip6intrq);
                    677: #endif
                    678: #ifdef NETATALK
                    679:        IF_DETACH_QUEUES(atintrq1);
                    680:        IF_DETACH_QUEUES(atintrq2);
                    681: #endif
                    682: #ifdef NS
                    683:        IF_DETACH_QUEUES(nsintrq);
                    684: #endif
                    685: #ifdef ISO
                    686:        IF_DETACH_QUEUES(clnlintrq);
                    687: #endif
                    688: #ifdef CCITT
                    689:        IF_DETACH_QUEUES(llcintrq);
                    690:        IF_DETACH_QUEUES(hdintrq);
                    691: #endif
                    692: #ifdef NATM
                    693:        IF_DETACH_QUEUES(natmintrq);
                    694: #endif
1.125     itojun    695: #ifdef DECNET
                    696:        IF_DETACH_QUEUES(decnetintrq);
                    697: #endif
1.95      itojun    698: #undef IF_DETACH_QUEUES
                    699:
1.53      thorpej   700:        splx(s);
1.95      itojun    701: }
                    702:
1.100     augustss  703: #if defined(INET) || defined(INET6) || defined(NETATALK) || defined(NS) || \
1.125     itojun    704:     defined(ISO) || defined(CCITT) || defined(NATM) || defined(DECNET)
1.95      itojun    705: static void
                    706: if_detach_queues(ifp, q)
                    707:        struct ifnet *ifp;
                    708:        struct ifqueue *q;
                    709: {
                    710:        struct mbuf *m, *prev, *next;
                    711:
                    712:        prev = NULL;
1.96      itojun    713:        for (m = q->ifq_head; m; m = next) {
1.95      itojun    714:                next = m->m_nextpkt;
                    715: #ifdef DIAGNOSTIC
1.96      itojun    716:                if ((m->m_flags & M_PKTHDR) == 0) {
                    717:                        prev = m;
1.95      itojun    718:                        continue;
1.96      itojun    719:                }
1.95      itojun    720: #endif
1.96      itojun    721:                if (m->m_pkthdr.rcvif != ifp) {
                    722:                        prev = m;
1.95      itojun    723:                        continue;
1.96      itojun    724:                }
1.95      itojun    725:
                    726:                if (prev)
                    727:                        prev->m_nextpkt = m->m_nextpkt;
                    728:                else
                    729:                        q->ifq_head = m->m_nextpkt;
                    730:                if (q->ifq_tail == m)
                    731:                        q->ifq_tail = prev;
                    732:                q->ifq_len--;
                    733:
                    734:                m->m_nextpkt = NULL;
                    735:                m_freem(m);
                    736:                IF_DROP(q);
                    737:        }
1.53      thorpej   738: }
1.100     augustss  739: #endif /* defined(INET) || ... */
1.53      thorpej   740:
                    741: /*
                    742:  * Callback for a radix tree walk to delete all references to an
                    743:  * ifnet.
                    744:  */
                    745: int
                    746: if_rt_walktree(rn, v)
                    747:        struct radix_node *rn;
                    748:        void *v;
                    749: {
1.55      itojun    750:        struct ifnet *ifp = (struct ifnet *)v;
1.53      thorpej   751:        struct rtentry *rt = (struct rtentry *)rn;
                    752:        int error;
                    753:
                    754:        if (rt->rt_ifp == ifp) {
                    755:                /* Delete the entry. */
                    756:                error = rtrequest(RTM_DELETE, rt_key(rt), rt->rt_gateway,
                    757:                    rt_mask(rt), rt->rt_flags, NULL);
                    758:                if (error)
                    759:                        printf("%s: warning: unable to delete rtentry @ %p, "
                    760:                            "error = %d\n", ifp->if_xname, rt, error);
                    761:        }
                    762:        return (0);
                    763: }
                    764:
1.1       cgd       765: /*
1.63      thorpej   766:  * Create a clone network interface.
                    767:  */
                    768: int
                    769: if_clone_create(name)
                    770:        const char *name;
                    771: {
                    772:        struct if_clone *ifc;
1.145   ! itojun    773:        struct ifnet *ifp;
1.63      thorpej   774:        int unit;
1.145   ! itojun    775:        int error;
        !           776:        int s;
1.63      thorpej   777:
                    778:        ifc = if_clone_lookup(name, &unit);
                    779:        if (ifc == NULL)
                    780:                return (EINVAL);
                    781:
                    782:        if (ifunit(name) != NULL)
                    783:                return (EEXIST);
                    784:
1.145   ! itojun    785:        error = (*ifc->ifc_create)(ifc, unit);
        !           786:        if (error)
        !           787:                return (error);
        !           788:
        !           789:        s = splnet();
        !           790:        for (ifp = TAILQ_FIRST(&ifnet); ifp; ifp = TAILQ_NEXT(ifp, if_list)) {
        !           791:                if (strcmp(name, ifp->if_xname) == 0) {
        !           792:                        if_attachdomain1(ifp);
        !           793:                        break;
        !           794:                }
        !           795:        }
        !           796:        splx(s);
        !           797:
        !           798:        return (error);
1.63      thorpej   799: }
                    800:
                    801: /*
                    802:  * Destroy a clone network interface.
                    803:  */
                    804: int
                    805: if_clone_destroy(name)
                    806:        const char *name;
                    807: {
                    808:        struct if_clone *ifc;
                    809:        struct ifnet *ifp;
                    810:
                    811:        ifc = if_clone_lookup(name, NULL);
                    812:        if (ifc == NULL)
                    813:                return (EINVAL);
                    814:
                    815:        ifp = ifunit(name);
                    816:        if (ifp == NULL)
                    817:                return (ENXIO);
                    818:
                    819:        if (ifc->ifc_destroy == NULL)
                    820:                return (EOPNOTSUPP);
                    821:
                    822:        (*ifc->ifc_destroy)(ifp);
                    823:        return (0);
                    824: }
                    825:
                    826: /*
                    827:  * Look up a network interface cloner.
                    828:  */
                    829: struct if_clone *
                    830: if_clone_lookup(name, unitp)
                    831:        const char *name;
                    832:        int *unitp;
                    833: {
                    834:        struct if_clone *ifc;
                    835:        const char *cp;
1.128     itojun    836:        int unit;
1.63      thorpej   837:
1.128     itojun    838:        /* separate interface name from unit */
                    839:        for (cp = name;
1.130     itojun    840:            cp - name < IFNAMSIZ && *cp && (*cp < '0' || *cp > '9');
1.128     itojun    841:            cp++)
                    842:                continue;
                    843:
                    844:        if (cp == name || cp - name == IFNAMSIZ || !*cp)
                    845:                return (NULL);  /* No name or unit number */
                    846:
                    847:        LIST_FOREACH(ifc, &if_cloners, ifc_list) {
                    848:                if (strlen(ifc->ifc_name) == cp - name &&
                    849:                    !strncmp(name, ifc->ifc_name, cp - name))
                    850:                        break;
1.63      thorpej   851:        }
                    852:
1.128     itojun    853:        if (ifc == NULL)
                    854:                return (NULL);
1.63      thorpej   855:
1.128     itojun    856:        unit = 0;
1.129     itojun    857:        while (cp - name < IFNAMSIZ && *cp) {
                    858:                if (*cp < '0' || *cp > '9' || unit > INT_MAX / 10) {
1.63      thorpej   859:                        /* Bogus unit number. */
                    860:                        return (NULL);
                    861:                }
1.128     itojun    862:                unit = (unit * 10) + (*cp++ - '0');
1.63      thorpej   863:        }
                    864:
                    865:        if (unitp != NULL)
1.128     itojun    866:                *unitp = unit;
1.63      thorpej   867:        return (ifc);
                    868: }
                    869:
                    870: /*
                    871:  * Register a network interface cloner.
                    872:  */
                    873: void
                    874: if_clone_attach(ifc)
                    875:        struct if_clone *ifc;
                    876: {
                    877:
                    878:        LIST_INSERT_HEAD(&if_cloners, ifc, ifc_list);
1.67      thorpej   879:        if_cloners_count++;
1.63      thorpej   880: }
                    881:
                    882: /*
                    883:  * Unregister a network interface cloner.
                    884:  */
                    885: void
                    886: if_clone_detach(ifc)
                    887:        struct if_clone *ifc;
                    888: {
                    889:
                    890:        LIST_REMOVE(ifc, ifc_list);
1.67      thorpej   891:        if_cloners_count--;
                    892: }
                    893:
                    894: /*
                    895:  * Provide list of interface cloners to userspace.
                    896:  */
                    897: int
                    898: if_clone_list(ifcr)
                    899:        struct if_clonereq *ifcr;
                    900: {
                    901:        char outbuf[IFNAMSIZ], *dst;
                    902:        struct if_clone *ifc;
                    903:        int count, error = 0;
                    904:
                    905:        ifcr->ifcr_total = if_cloners_count;
                    906:        if ((dst = ifcr->ifcr_buffer) == NULL) {
                    907:                /* Just asking how many there are. */
                    908:                return (0);
                    909:        }
                    910:
                    911:        if (ifcr->ifcr_count < 0)
                    912:                return (EINVAL);
                    913:
                    914:        count = (if_cloners_count < ifcr->ifcr_count) ?
                    915:            if_cloners_count : ifcr->ifcr_count;
                    916:
                    917:        for (ifc = LIST_FIRST(&if_cloners); ifc != NULL && count != 0;
                    918:             ifc = LIST_NEXT(ifc, ifc_list), count--, dst += IFNAMSIZ) {
                    919:                strncpy(outbuf, ifc->ifc_name, IFNAMSIZ);
                    920:                outbuf[IFNAMSIZ - 1] = '\0';    /* sanity */
                    921:                error = copyout(outbuf, dst, IFNAMSIZ);
                    922:                if (error)
                    923:                        break;
                    924:        }
                    925:
                    926:        return (error);
1.63      thorpej   927: }
                    928:
                    929: /*
1.1       cgd       930:  * Locate an interface based on a complete address.
                    931:  */
                    932: /*ARGSUSED*/
                    933: struct ifaddr *
                    934: ifa_ifwithaddr(addr)
1.140     matt      935:        const struct sockaddr *addr;
1.1       cgd       936: {
1.61      augustss  937:        struct ifnet *ifp;
                    938:        struct ifaddr *ifa;
1.1       cgd       939:
                    940: #define        equal(a1, a2) \
                    941:   (bcmp((caddr_t)(a1), (caddr_t)(a2), ((struct sockaddr *)(a1))->sa_len) == 0)
1.53      thorpej   942:
                    943:        for (ifp = TAILQ_FIRST(&ifnet); ifp != NULL;
                    944:             ifp = TAILQ_NEXT(ifp, if_list)) {
                    945:                if (ifp->if_output == if_nulloutput)
1.1       cgd       946:                        continue;
1.53      thorpej   947:                for (ifa = TAILQ_FIRST(&ifp->if_addrlist); ifa != NULL;
                    948:                     ifa = TAILQ_NEXT(ifa, ifa_list)) {
                    949:                        if (ifa->ifa_addr->sa_family != addr->sa_family)
                    950:                                continue;
                    951:                        if (equal(addr, ifa->ifa_addr))
                    952:                                return (ifa);
                    953:                        if ((ifp->if_flags & IFF_BROADCAST) &&
                    954:                            ifa->ifa_broadaddr &&
                    955:                            /* IP6 doesn't have broadcast */
                    956:                            ifa->ifa_broadaddr->sa_len != 0 &&
                    957:                            equal(ifa->ifa_broadaddr, addr))
                    958:                                return (ifa);
                    959:                }
1.1       cgd       960:        }
1.53      thorpej   961:        return (NULL);
1.1       cgd       962: }
1.49      itojun    963:
1.1       cgd       964: /*
                    965:  * Locate the point to point interface with a given destination address.
                    966:  */
                    967: /*ARGSUSED*/
                    968: struct ifaddr *
                    969: ifa_ifwithdstaddr(addr)
1.140     matt      970:        const struct sockaddr *addr;
1.1       cgd       971: {
1.61      augustss  972:        struct ifnet *ifp;
                    973:        struct ifaddr *ifa;
1.1       cgd       974:
1.53      thorpej   975:        for (ifp = TAILQ_FIRST(&ifnet); ifp != NULL;
                    976:             ifp = TAILQ_NEXT(ifp, if_list)) {
                    977:                if (ifp->if_output == if_nulloutput)
                    978:                        continue;
                    979:                if (ifp->if_flags & IFF_POINTOPOINT) {
                    980:                        for (ifa = TAILQ_FIRST(&ifp->if_addrlist); ifa != NULL;
                    981:                             ifa = TAILQ_NEXT(ifa, ifa_list)) {
                    982:                                if (ifa->ifa_addr->sa_family !=
                    983:                                      addr->sa_family ||
                    984:                                    ifa->ifa_dstaddr == NULL)
                    985:                                        continue;
                    986:                                if (equal(addr, ifa->ifa_dstaddr))
                    987:                                        return (ifa);
                    988:                        }
                    989:                }
1.1       cgd       990:        }
1.53      thorpej   991:        return (NULL);
1.1       cgd       992: }
                    993:
                    994: /*
                    995:  * Find an interface on a specific network.  If many, choice
1.15      mycroft   996:  * is most specific found.
1.1       cgd       997:  */
                    998: struct ifaddr *
                    999: ifa_ifwithnet(addr)
1.140     matt     1000:        const struct sockaddr *addr;
1.1       cgd      1001: {
1.61      augustss 1002:        struct ifnet *ifp;
                   1003:        struct ifaddr *ifa;
1.140     matt     1004:        const struct sockaddr_dl *sdl;
1.15      mycroft  1005:        struct ifaddr *ifa_maybe = 0;
1.1       cgd      1006:        u_int af = addr->sa_family;
1.15      mycroft  1007:        char *addr_data = addr->sa_data, *cplim;
1.1       cgd      1008:
                   1009:        if (af == AF_LINK) {
1.53      thorpej  1010:                sdl = (struct sockaddr_dl *)addr;
1.137     itojun   1011:                if (sdl->sdl_index && sdl->sdl_index < if_indexlim &&
                   1012:                    ifindex2ifnet[sdl->sdl_index] &&
1.53      thorpej  1013:                    ifindex2ifnet[sdl->sdl_index]->if_output != if_nulloutput)
                   1014:                        return (ifnet_addrs[sdl->sdl_index]);
1.1       cgd      1015:        }
1.51      bouyer   1016: #ifdef NETATALK
                   1017:        if (af == AF_APPLETALK) {
1.140     matt     1018:                const struct sockaddr_at *sat, *sat2;
1.62      bouyer   1019:                sat = (struct sockaddr_at *)addr;
1.53      thorpej  1020:                for (ifp = TAILQ_FIRST(&ifnet); ifp != NULL;
                   1021:                     ifp = TAILQ_NEXT(ifp, if_list)) {
                   1022:                        if (ifp->if_output == if_nulloutput)
                   1023:                                continue;
1.51      bouyer   1024:                        ifa = at_ifawithnet((struct sockaddr_at *)addr, ifp);
1.62      bouyer   1025:                        if (ifa == NULL)
                   1026:                                continue;
                   1027:                        sat2 = (struct sockaddr_at *)ifa->ifa_addr;
                   1028:                        if (sat2->sat_addr.s_net == sat->sat_addr.s_net)
                   1029:                                return (ifa); /* exact match */
                   1030:                        if (ifa_maybe == NULL) {
1.112     wiz      1031:                                /* else keep the if with the right range */
1.62      bouyer   1032:                                ifa_maybe = ifa;
                   1033:                        }
1.51      bouyer   1034:                }
1.62      bouyer   1035:                return (ifa_maybe);
1.51      bouyer   1036:        }
                   1037: #endif
1.53      thorpej  1038:        for (ifp = TAILQ_FIRST(&ifnet); ifp != NULL;
                   1039:             ifp = TAILQ_NEXT(ifp, if_list)) {
                   1040:                if (ifp->if_output == if_nulloutput)
                   1041:                        continue;
                   1042:                for (ifa = TAILQ_FIRST(&ifp->if_addrlist); ifa != NULL;
                   1043:                     ifa = TAILQ_NEXT(ifa, ifa_list)) {
1.61      augustss 1044:                        char *cp, *cp2, *cp3;
1.15      mycroft  1045:
                   1046:                        if (ifa->ifa_addr->sa_family != af ||
                   1047:                            ifa->ifa_netmask == 0)
1.53      thorpej  1048:  next:                         continue;
1.15      mycroft  1049:                        cp = addr_data;
                   1050:                        cp2 = ifa->ifa_addr->sa_data;
                   1051:                        cp3 = ifa->ifa_netmask->sa_data;
                   1052:                        cplim = (char *)ifa->ifa_netmask +
1.53      thorpej  1053:                            ifa->ifa_netmask->sa_len;
                   1054:                        while (cp3 < cplim) {
                   1055:                                if ((*cp++ ^ *cp2++) & *cp3++) {
                   1056:                                        /* want to continue for() loop */
1.32      mrg      1057:                                        goto next;
1.53      thorpej  1058:                                }
                   1059:                        }
1.15      mycroft  1060:                        if (ifa_maybe == 0 ||
                   1061:                            rn_refines((caddr_t)ifa->ifa_netmask,
                   1062:                            (caddr_t)ifa_maybe->ifa_netmask))
                   1063:                                ifa_maybe = ifa;
                   1064:                }
1.53      thorpej  1065:        }
1.15      mycroft  1066:        return (ifa_maybe);
1.26      mrg      1067: }
1.53      thorpej  1068:
1.26      mrg      1069: /*
                   1070:  * Find the interface of the addresss.
                   1071:  */
                   1072: struct ifaddr *
                   1073: ifa_ifwithladdr(addr)
1.140     matt     1074:        const struct sockaddr *addr;
1.26      mrg      1075: {
                   1076:        struct ifaddr *ia;
                   1077:
1.53      thorpej  1078:        if ((ia = ifa_ifwithaddr(addr)) || (ia = ifa_ifwithdstaddr(addr)) ||
                   1079:            (ia = ifa_ifwithnet(addr)))
1.26      mrg      1080:                return (ia);
                   1081:        return (NULL);
1.1       cgd      1082: }
                   1083:
                   1084: /*
                   1085:  * Find an interface using a specific address family
                   1086:  */
                   1087: struct ifaddr *
                   1088: ifa_ifwithaf(af)
1.61      augustss 1089:        int af;
1.1       cgd      1090: {
1.61      augustss 1091:        struct ifnet *ifp;
                   1092:        struct ifaddr *ifa;
1.1       cgd      1093:
1.53      thorpej  1094:        for (ifp = TAILQ_FIRST(&ifnet); ifp != NULL;
                   1095:             ifp = TAILQ_NEXT(ifp, if_list)) {
                   1096:                if (ifp->if_output == if_nulloutput)
                   1097:                        continue;
                   1098:                for (ifa = TAILQ_FIRST(&ifp->if_addrlist); ifa != NULL;
                   1099:                     ifa = TAILQ_NEXT(ifa, ifa_list)) {
1.21      mycroft  1100:                        if (ifa->ifa_addr->sa_family == af)
                   1101:                                return (ifa);
1.53      thorpej  1102:                }
                   1103:        }
                   1104:        return (NULL);
1.1       cgd      1105: }
                   1106:
                   1107: /*
                   1108:  * Find an interface address specific to an interface best matching
                   1109:  * a given address.
                   1110:  */
                   1111: struct ifaddr *
                   1112: ifaof_ifpforaddr(addr, ifp)
1.140     matt     1113:        const struct sockaddr *addr;
1.61      augustss 1114:        struct ifnet *ifp;
1.1       cgd      1115: {
1.61      augustss 1116:        struct ifaddr *ifa;
1.140     matt     1117:        const char *cp, *cp2, *cp3;
                   1118:        const char *cplim;
1.1       cgd      1119:        struct ifaddr *ifa_maybe = 0;
                   1120:        u_int af = addr->sa_family;
                   1121:
1.53      thorpej  1122:        if (ifp->if_output == if_nulloutput)
                   1123:                return (NULL);
                   1124:
1.1       cgd      1125:        if (af >= AF_MAX)
1.53      thorpej  1126:                return (NULL);
                   1127:
                   1128:        for (ifa = TAILQ_FIRST(&ifp->if_addrlist); ifa != NULL;
                   1129:             ifa = TAILQ_NEXT(ifa, ifa_list)) {
1.1       cgd      1130:                if (ifa->ifa_addr->sa_family != af)
                   1131:                        continue;
                   1132:                ifa_maybe = ifa;
                   1133:                if (ifa->ifa_netmask == 0) {
                   1134:                        if (equal(addr, ifa->ifa_addr) ||
1.53      thorpej  1135:                            (ifa->ifa_dstaddr &&
                   1136:                             equal(addr, ifa->ifa_dstaddr)))
1.1       cgd      1137:                                return (ifa);
                   1138:                        continue;
                   1139:                }
                   1140:                cp = addr->sa_data;
                   1141:                cp2 = ifa->ifa_addr->sa_data;
                   1142:                cp3 = ifa->ifa_netmask->sa_data;
                   1143:                cplim = ifa->ifa_netmask->sa_len + (char *)ifa->ifa_netmask;
1.53      thorpej  1144:                for (; cp3 < cplim; cp3++) {
1.1       cgd      1145:                        if ((*cp++ ^ *cp2++) & *cp3)
                   1146:                                break;
1.53      thorpej  1147:                }
1.1       cgd      1148:                if (cp3 == cplim)
                   1149:                        return (ifa);
                   1150:        }
                   1151:        return (ifa_maybe);
                   1152: }
1.9       mycroft  1153:
1.1       cgd      1154: /*
                   1155:  * Default action when installing a route with a Link Level gateway.
                   1156:  * Lookup an appropriate real ifa to point to.
                   1157:  * This should be moved to /sys/net/link.c eventually.
                   1158:  */
1.15      mycroft  1159: void
1.82      itojun   1160: link_rtrequest(cmd, rt, info)
1.15      mycroft  1161:        int cmd;
1.61      augustss 1162:        struct rtentry *rt;
1.82      itojun   1163:        struct rt_addrinfo *info;
1.1       cgd      1164: {
1.61      augustss 1165:        struct ifaddr *ifa;
1.1       cgd      1166:        struct sockaddr *dst;
1.15      mycroft  1167:        struct ifnet *ifp;
1.1       cgd      1168:
                   1169:        if (cmd != RTM_ADD || ((ifa = rt->rt_ifa) == 0) ||
                   1170:            ((ifp = ifa->ifa_ifp) == 0) || ((dst = rt_key(rt)) == 0))
                   1171:                return;
1.24      christos 1172:        if ((ifa = ifaof_ifpforaddr(dst, ifp)) != NULL) {
1.15      mycroft  1173:                IFAFREE(rt->rt_ifa);
1.1       cgd      1174:                rt->rt_ifa = ifa;
1.53      thorpej  1175:                IFAREF(ifa);
1.1       cgd      1176:                if (ifa->ifa_rtrequest && ifa->ifa_rtrequest != link_rtrequest)
1.82      itojun   1177:                        ifa->ifa_rtrequest(cmd, rt, info);
1.1       cgd      1178:        }
                   1179: }
                   1180:
                   1181: /*
                   1182:  * Mark an interface down and notify protocols of
                   1183:  * the transition.
1.23      mycroft  1184:  * NOTE: must be called at splsoftnet or equivalent.
1.1       cgd      1185:  */
1.15      mycroft  1186: void
1.1       cgd      1187: if_down(ifp)
1.61      augustss 1188:        struct ifnet *ifp;
1.1       cgd      1189: {
1.61      augustss 1190:        struct ifaddr *ifa;
1.1       cgd      1191:
                   1192:        ifp->if_flags &= ~IFF_UP;
1.91      itojun   1193:        microtime(&ifp->if_lastchange);
1.53      thorpej  1194:        for (ifa = TAILQ_FIRST(&ifp->if_addrlist); ifa != NULL;
                   1195:             ifa = TAILQ_NEXT(ifa, ifa_list))
1.1       cgd      1196:                pfctlinput(PRC_IFDOWN, ifa->ifa_addr);
1.78      thorpej  1197:        IFQ_PURGE(&ifp->if_snd);
1.15      mycroft  1198:        rt_ifmsg(ifp);
                   1199: }
                   1200:
                   1201: /*
                   1202:  * Mark an interface up and notify protocols of
                   1203:  * the transition.
1.23      mycroft  1204:  * NOTE: must be called at splsoftnet or equivalent.
1.15      mycroft  1205:  */
                   1206: void
                   1207: if_up(ifp)
1.61      augustss 1208:        struct ifnet *ifp;
1.15      mycroft  1209: {
1.24      christos 1210: #ifdef notyet
1.61      augustss 1211:        struct ifaddr *ifa;
1.24      christos 1212: #endif
1.15      mycroft  1213:
                   1214:        ifp->if_flags |= IFF_UP;
1.91      itojun   1215:        microtime(&ifp->if_lastchange);
1.15      mycroft  1216: #ifdef notyet
                   1217:        /* this has no effect on IP, and will kill all ISO connections XXX */
1.53      thorpej  1218:        for (ifa = TAILQ_FIRST(&ifp->if_addrlist); ifa != NULL;
                   1219:             ifa = TAILQ_NEXT(ifa, ifa_list))
1.15      mycroft  1220:                pfctlinput(PRC_IFUP, ifa->ifa_addr);
                   1221: #endif
                   1222:        rt_ifmsg(ifp);
1.49      itojun   1223: #ifdef INET6
                   1224:        in6_if_up(ifp);
                   1225: #endif
1.1       cgd      1226: }
                   1227:
                   1228: /*
                   1229:  * Handle interface watchdog timer routines.  Called
                   1230:  * from softclock, we decrement timers (if set) and
                   1231:  * call the appropriate interface routine on expiration.
                   1232:  */
1.4       andrew   1233: void
1.13      cgd      1234: if_slowtimo(arg)
                   1235:        void *arg;
1.1       cgd      1236: {
1.61      augustss 1237:        struct ifnet *ifp;
1.88      thorpej  1238:        int s = splnet();
1.1       cgd      1239:
1.53      thorpej  1240:        for (ifp = TAILQ_FIRST(&ifnet); ifp != NULL;
                   1241:             ifp = TAILQ_NEXT(ifp, if_list)) {
1.1       cgd      1242:                if (ifp->if_timer == 0 || --ifp->if_timer)
                   1243:                        continue;
                   1244:                if (ifp->if_watchdog)
1.34      thorpej  1245:                        (*ifp->if_watchdog)(ifp);
1.1       cgd      1246:        }
                   1247:        splx(s);
1.59      thorpej  1248:        callout_reset(&if_slowtimo_ch, hz / IFNET_SLOWHZ,
                   1249:            if_slowtimo, NULL);
1.65      thorpej  1250: }
                   1251:
                   1252: /*
                   1253:  * Set/clear promiscuous mode on interface ifp based on the truth value
                   1254:  * of pswitch.  The calls are reference counted so that only the first
                   1255:  * "on" request actually has an effect, as does the final "off" request.
                   1256:  * Results are undefined if the "off" and "on" requests are not matched.
                   1257:  */
                   1258: int
                   1259: ifpromisc(ifp, pswitch)
                   1260:        struct ifnet *ifp;
                   1261:        int pswitch;
                   1262: {
                   1263:        int pcount, ret;
                   1264:        short flags;
                   1265:        struct ifreq ifr;
                   1266:
                   1267:        pcount = ifp->if_pcount;
                   1268:        flags = ifp->if_flags;
                   1269:        if (pswitch) {
                   1270:                /*
1.71      thorpej  1271:                 * Allow the device to be "placed" into promiscuous
                   1272:                 * mode even if it is not configured up.  It will
                   1273:                 * consult IFF_PROMISC when it is is brought up.
1.65      thorpej  1274:                 */
1.68      pk       1275:                if (ifp->if_pcount++ != 0)
1.65      thorpej  1276:                        return (0);
                   1277:                ifp->if_flags |= IFF_PROMISC;
1.71      thorpej  1278:                if ((ifp->if_flags & IFF_UP) == 0)
                   1279:                        return (0);
1.65      thorpej  1280:        } else {
                   1281:                if (--ifp->if_pcount > 0)
                   1282:                        return (0);
                   1283:                ifp->if_flags &= ~IFF_PROMISC;
                   1284:                /*
                   1285:                 * If the device is not configured up, we should not need to
                   1286:                 * turn off promiscuous mode (device should have turned it
                   1287:                 * off when interface went down; and will look at IFF_PROMISC
                   1288:                 * again next time interface comes up).
                   1289:                 */
                   1290:                if ((ifp->if_flags & IFF_UP) == 0)
                   1291:                        return (0);
                   1292:        }
                   1293:        memset(&ifr, 0, sizeof(ifr));
                   1294:        ifr.ifr_flags = ifp->if_flags;
                   1295:        ret = (*ifp->if_ioctl)(ifp, SIOCSIFFLAGS, (caddr_t) &ifr);
                   1296:        /* Restore interface state if not successful. */
                   1297:        if (ret != 0) {
                   1298:                ifp->if_pcount = pcount;
                   1299:                ifp->if_flags = flags;
                   1300:        }
                   1301:        return (ret);
1.1       cgd      1302: }
                   1303:
                   1304: /*
                   1305:  * Map interface name to
                   1306:  * interface structure pointer.
                   1307:  */
                   1308: struct ifnet *
                   1309: ifunit(name)
1.52      thorpej  1310:        const char *name;
1.1       cgd      1311: {
1.61      augustss 1312:        struct ifnet *ifp;
1.105     matt     1313:        const char *cp = name;
                   1314:        u_int unit = 0;
                   1315:        u_int i;
                   1316:
                   1317:        /*
                   1318:         * If the entire name is a number, treat it as an ifindex.
                   1319:         */
                   1320:        for (i = 0; i < IFNAMSIZ && *cp >= '0' && *cp <= '9'; i++, cp++) {
                   1321:                unit = unit * 10 + (*cp - '0');
                   1322:        }
                   1323:
                   1324:        /*
                   1325:         * If the number took all of the name, then it's a valid ifindex.
                   1326:         */
                   1327:        if (i == IFNAMSIZ || (cp != name && *cp == '\0')) {
1.137     itojun   1328:                if (unit >= if_indexlim)
1.105     matt     1329:                        return (NULL);
                   1330:                ifp = ifindex2ifnet[unit];
                   1331:                if (ifp == NULL || ifp->if_output == if_nulloutput)
                   1332:                        return (NULL);
                   1333:                return (ifp);
                   1334:        }
1.34      thorpej  1335:
1.53      thorpej  1336:        for (ifp = TAILQ_FIRST(&ifnet); ifp != NULL;
                   1337:             ifp = TAILQ_NEXT(ifp, if_list)) {
                   1338:                if (ifp->if_output == if_nulloutput)
                   1339:                        continue;
                   1340:                if (strcmp(ifp->if_xname, name) == 0)
1.34      thorpej  1341:                        return (ifp);
1.53      thorpej  1342:        }
1.34      thorpej  1343:        return (NULL);
1.1       cgd      1344: }
1.49      itojun   1345:
1.1       cgd      1346: /*
                   1347:  * Interface ioctls.
                   1348:  */
1.15      mycroft  1349: int
1.123     fvdl     1350: ifioctl(so, cmd, data, p)
1.1       cgd      1351:        struct socket *so;
1.18      cgd      1352:        u_long cmd;
1.1       cgd      1353:        caddr_t data;
1.123     fvdl     1354:        struct proc *p;
1.1       cgd      1355: {
1.61      augustss 1356:        struct ifnet *ifp;
                   1357:        struct ifreq *ifr;
1.89      thorpej  1358:        struct ifcapreq *ifcr;
1.105     matt     1359:        struct ifdatareq *ifdr;
1.89      thorpej  1360:        int s, error = 0;
1.49      itojun   1361:        short oif_flags;
1.140     matt     1362:        int prived_error;
                   1363:
                   1364:        if (p)
                   1365:                prived_error = suser(p->p_ucred, &p->p_acflag);
                   1366:        else
                   1367:                prived_error = 0;
1.1       cgd      1368:
                   1369:        switch (cmd) {
                   1370:
                   1371:        case SIOCGIFCONF:
                   1372:        case OSIOCGIFCONF:
                   1373:                return (ifconf(cmd, data));
                   1374:        }
                   1375:        ifr = (struct ifreq *)data;
1.89      thorpej  1376:        ifcr = (struct ifcapreq *)data;
1.105     matt     1377:        ifdr = (struct ifdatareq *)data;
1.63      thorpej  1378:
                   1379:        switch (cmd) {
                   1380:        case SIOCIFCREATE:
                   1381:        case SIOCIFDESTROY:
1.140     matt     1382:                if (prived_error)
                   1383:                        return (prived_error);
1.64      thorpej  1384:                return ((cmd == SIOCIFCREATE) ?
                   1385:                        if_clone_create(ifr->ifr_name) :
                   1386:                        if_clone_destroy(ifr->ifr_name));
1.67      thorpej  1387:
                   1388:        case SIOCIFGCLONERS:
                   1389:                return (if_clone_list((struct if_clonereq *)data));
1.63      thorpej  1390:        }
                   1391:
1.1       cgd      1392:        ifp = ifunit(ifr->ifr_name);
                   1393:        if (ifp == 0)
                   1394:                return (ENXIO);
1.49      itojun   1395:        oif_flags = ifp->if_flags;
1.1       cgd      1396:        switch (cmd) {
                   1397:
                   1398:        case SIOCGIFFLAGS:
                   1399:                ifr->ifr_flags = ifp->if_flags;
                   1400:                break;
                   1401:
                   1402:        case SIOCGIFMETRIC:
                   1403:                ifr->ifr_metric = ifp->if_metric;
                   1404:                break;
1.7       hpeyerl  1405:
1.36      mycroft  1406:        case SIOCGIFMTU:
1.37      cgd      1407:                ifr->ifr_mtu = ifp->if_mtu;
1.80      thorpej  1408:                break;
                   1409:
                   1410:        case SIOCGIFDLT:
                   1411:                ifr->ifr_dlt = ifp->if_dlt;
1.37      cgd      1412:                break;
1.36      mycroft  1413:
1.1       cgd      1414:        case SIOCSIFFLAGS:
1.140     matt     1415:                if (prived_error != 0)
                   1416:                        return (prived_error);
1.1       cgd      1417:                if (ifp->if_flags & IFF_UP && (ifr->ifr_flags & IFF_UP) == 0) {
1.89      thorpej  1418:                        s = splnet();
1.1       cgd      1419:                        if_down(ifp);
                   1420:                        splx(s);
                   1421:                }
1.15      mycroft  1422:                if (ifr->ifr_flags & IFF_UP && (ifp->if_flags & IFF_UP) == 0) {
1.89      thorpej  1423:                        s = splnet();
1.15      mycroft  1424:                        if_up(ifp);
                   1425:                        splx(s);
                   1426:                }
1.1       cgd      1427:                ifp->if_flags = (ifp->if_flags & IFF_CANTCHANGE) |
                   1428:                        (ifr->ifr_flags &~ IFF_CANTCHANGE);
                   1429:                if (ifp->if_ioctl)
                   1430:                        (void) (*ifp->if_ioctl)(ifp, cmd, data);
                   1431:                break;
                   1432:
1.89      thorpej  1433:        case SIOCGIFCAP:
                   1434:                ifcr->ifcr_capabilities = ifp->if_capabilities;
                   1435:                ifcr->ifcr_capenable = ifp->if_capenable;
                   1436:                break;
                   1437:
                   1438:        case SIOCSIFCAP:
1.140     matt     1439:                if (prived_error != 0)
                   1440:                        return (prived_error);
1.89      thorpej  1441:                if ((ifcr->ifcr_capenable & ~ifp->if_capabilities) != 0)
                   1442:                        return (EINVAL);
                   1443:                if (ifp->if_ioctl == NULL)
                   1444:                        return (EOPNOTSUPP);
                   1445:
                   1446:                /* Must prevent race with packet reception here. */
                   1447:                s = splnet();
                   1448:                if (ifcr->ifcr_capenable != ifp->if_capenable) {
                   1449:                        struct ifreq ifrq;
                   1450:
                   1451:                        ifrq.ifr_flags = ifp->if_flags;
                   1452:                        ifp->if_capenable = ifcr->ifcr_capenable;
                   1453:
                   1454:                        /* Pre-compute the checksum flags mask. */
1.97      thorpej  1455:                        ifp->if_csum_flags_tx = 0;
                   1456:                        ifp->if_csum_flags_rx = 0;
                   1457:                        if (ifp->if_capenable & IFCAP_CSUM_IPv4) {
                   1458:                                ifp->if_csum_flags_tx |= M_CSUM_IPv4;
                   1459:                                ifp->if_csum_flags_rx |= M_CSUM_IPv4;
                   1460:                        }
                   1461:
                   1462:                        if (ifp->if_capenable & IFCAP_CSUM_TCPv4) {
                   1463:                                ifp->if_csum_flags_tx |= M_CSUM_TCPv4;
                   1464:                                ifp->if_csum_flags_rx |= M_CSUM_TCPv4;
                   1465:                        } else if (ifp->if_capenable & IFCAP_CSUM_TCPv4_Rx)
                   1466:                                ifp->if_csum_flags_rx |= M_CSUM_TCPv4;
                   1467:
                   1468:                        if (ifp->if_capenable & IFCAP_CSUM_UDPv4) {
                   1469:                                ifp->if_csum_flags_tx |= M_CSUM_UDPv4;
                   1470:                                ifp->if_csum_flags_rx |= M_CSUM_UDPv4;
                   1471:                        } else if (ifp->if_capenable & IFCAP_CSUM_UDPv4_Rx)
                   1472:                                ifp->if_csum_flags_rx |= M_CSUM_UDPv4;
                   1473:
                   1474:                        if (ifp->if_capenable & IFCAP_CSUM_TCPv6) {
                   1475:                                ifp->if_csum_flags_tx |= M_CSUM_TCPv6;
                   1476:                                ifp->if_csum_flags_rx |= M_CSUM_TCPv6;
                   1477:                        }
                   1478:
                   1479:                        if (ifp->if_capenable & IFCAP_CSUM_UDPv6) {
                   1480:                                ifp->if_csum_flags_tx |= M_CSUM_UDPv6;
                   1481:                                ifp->if_csum_flags_rx |= M_CSUM_UDPv6;
                   1482:                        }
1.89      thorpej  1483:
                   1484:                        /*
                   1485:                         * Only kick the interface if it's up.  If it's
                   1486:                         * not up now, it will notice the cap enables
                   1487:                         * when it is brought up later.
                   1488:                         */
                   1489:                        if (ifp->if_flags & IFF_UP)
                   1490:                                (void) (*ifp->if_ioctl)(ifp, SIOCSIFFLAGS,
                   1491:                                    (caddr_t) &ifrq);
                   1492:                }
                   1493:                splx(s);
                   1494:                break;
                   1495:
1.1       cgd      1496:        case SIOCSIFMETRIC:
1.140     matt     1497:                if (prived_error != 0)
                   1498:                        return (prived_error);
1.1       cgd      1499:                ifp->if_metric = ifr->ifr_metric;
1.105     matt     1500:                break;
                   1501:
                   1502:        case SIOCGIFDATA:
                   1503:                ifdr->ifdr_data = ifp->if_data;
                   1504:                break;
                   1505:
                   1506:        case SIOCZIFDATA:
1.140     matt     1507:                if (prived_error != 0)
                   1508:                        return (prived_error);
1.105     matt     1509:                ifdr->ifdr_data = ifp->if_data;
                   1510:                /*
                   1511:                 * Assumes that the volatile counters that can be
                   1512:                 * zero'ed are at the end of if_data.
                   1513:                 */
                   1514:                memset(&ifp->if_data.ifi_ipackets, 0, sizeof(ifp->if_data) -
                   1515:                    offsetof(struct if_data, ifi_ipackets));
1.5       deraadt  1516:                break;
                   1517:
1.36      mycroft  1518:        case SIOCSIFMTU:
1.49      itojun   1519:        {
                   1520:                u_long oldmtu = ifp->if_mtu;
                   1521:
1.140     matt     1522:                if (prived_error)
                   1523:                        return (prived_error);
1.49      itojun   1524:                if (ifp->if_ioctl == NULL)
                   1525:                        return (EOPNOTSUPP);
                   1526:                error = (*ifp->if_ioctl)(ifp, cmd, data);
                   1527:
                   1528:                /*
                   1529:                 * If the link MTU changed, do network layer specific procedure.
                   1530:                 */
                   1531:                if (ifp->if_mtu != oldmtu) {
                   1532: #ifdef INET6
                   1533:                        nd6_setmtu(ifp);
                   1534: #endif
                   1535:                }
                   1536:                break;
                   1537:        }
1.72      thorpej  1538:        case SIOCSIFPHYADDR:
                   1539:        case SIOCDIFPHYADDR:
                   1540: #ifdef INET6
                   1541:        case SIOCSIFPHYADDR_IN6:
                   1542: #endif
1.85      itojun   1543:        case SIOCSLIFPHYADDR:
1.15      mycroft  1544:        case SIOCADDMULTI:
                   1545:        case SIOCDELMULTI:
1.39      thorpej  1546:        case SIOCSIFMEDIA:
1.140     matt     1547:                if (prived_error != 0)
                   1548:                        return (prived_error);
1.39      thorpej  1549:                /* FALLTHROUGH */
1.74      itojun   1550:        case SIOCGIFPSRCADDR:
                   1551:        case SIOCGIFPDSTADDR:
1.85      itojun   1552:        case SIOCGLIFPHYADDR:
1.39      thorpej  1553:        case SIOCGIFMEDIA:
1.15      mycroft  1554:                if (ifp->if_ioctl == 0)
1.5       deraadt  1555:                        return (EOPNOTSUPP);
1.49      itojun   1556:                error = (*ifp->if_ioctl)(ifp, cmd, data);
                   1557:                break;
1.1       cgd      1558:
1.45      kml      1559:        case SIOCSDRVSPEC:
1.66      onoe     1560:        case SIOCS80211NWID:
1.69      onoe     1561:        case SIOCS80211NWKEY:
1.76      thorpej  1562:        case SIOCS80211POWER:
1.114     onoe     1563:        case SIOCS80211BSSID:
                   1564:        case SIOCS80211CHANNEL:
1.45      kml      1565:                /* XXX:  need to pass proc pointer through to driver... */
1.140     matt     1566:                if (prived_error != 0)
                   1567:                        return (prived_error);
1.45      kml      1568:        /* FALLTHROUGH */
1.1       cgd      1569:        default:
                   1570:                if (so->so_proto == 0)
                   1571:                        return (EOPNOTSUPP);
1.119     christos 1572: #if !defined(COMPAT_43) && !defined(COMPAT_LINUX) && !defined(COMPAT_SVR4) && !defined(COMPAT_ULTRIX) && !defined(LKM)
1.49      itojun   1573:                error = ((*so->so_proto->pr_usrreq)(so, PRU_CONTROL,
1.36      mycroft  1574:                    (struct mbuf *)cmd, (struct mbuf *)data,
1.123     fvdl     1575:                    (struct mbuf *)ifp, p));
1.1       cgd      1576: #else
                   1577:            {
                   1578:                int ocmd = cmd;
                   1579:
                   1580:                switch (cmd) {
                   1581:
1.28      mycroft  1582:                case SIOCSIFADDR:
1.1       cgd      1583:                case SIOCSIFDSTADDR:
                   1584:                case SIOCSIFBRDADDR:
                   1585:                case SIOCSIFNETMASK:
                   1586: #if BYTE_ORDER != BIG_ENDIAN
                   1587:                        if (ifr->ifr_addr.sa_family == 0 &&
                   1588:                            ifr->ifr_addr.sa_len < 16) {
                   1589:                                ifr->ifr_addr.sa_family = ifr->ifr_addr.sa_len;
                   1590:                                ifr->ifr_addr.sa_len = 16;
                   1591:                        }
                   1592: #else
                   1593:                        if (ifr->ifr_addr.sa_len == 0)
                   1594:                                ifr->ifr_addr.sa_len = 16;
                   1595: #endif
                   1596:                        break;
                   1597:
                   1598:                case OSIOCGIFADDR:
                   1599:                        cmd = SIOCGIFADDR;
                   1600:                        break;
                   1601:
                   1602:                case OSIOCGIFDSTADDR:
                   1603:                        cmd = SIOCGIFDSTADDR;
                   1604:                        break;
                   1605:
                   1606:                case OSIOCGIFBRDADDR:
                   1607:                        cmd = SIOCGIFBRDADDR;
                   1608:                        break;
                   1609:
                   1610:                case OSIOCGIFNETMASK:
                   1611:                        cmd = SIOCGIFNETMASK;
                   1612:                }
1.49      itojun   1613:
1.24      christos 1614:                error = ((*so->so_proto->pr_usrreq)(so, PRU_CONTROL,
1.36      mycroft  1615:                    (struct mbuf *)cmd, (struct mbuf *)data,
1.123     fvdl     1616:                    (struct mbuf *)ifp, p));
1.49      itojun   1617:
1.1       cgd      1618:                switch (ocmd) {
                   1619:                case OSIOCGIFADDR:
                   1620:                case OSIOCGIFDSTADDR:
                   1621:                case OSIOCGIFBRDADDR:
                   1622:                case OSIOCGIFNETMASK:
1.20      cgd      1623:                        *(u_int16_t *)&ifr->ifr_addr = ifr->ifr_addr.sa_family;
1.1       cgd      1624:                }
1.49      itojun   1625:            }
                   1626: #endif /* COMPAT_43 */
                   1627:                break;
                   1628:        }
1.1       cgd      1629:
1.49      itojun   1630:        if (((oif_flags ^ ifp->if_flags) & IFF_UP) != 0) {
                   1631: #ifdef INET6
                   1632:                if ((ifp->if_flags & IFF_UP) != 0) {
1.89      thorpej  1633:                        s = splnet();
1.49      itojun   1634:                        in6_if_up(ifp);
                   1635:                        splx(s);
                   1636:                }
1.1       cgd      1637: #endif
                   1638:        }
1.49      itojun   1639:
                   1640:        return (error);
1.1       cgd      1641: }
                   1642:
                   1643: /*
                   1644:  * Return interface configuration
                   1645:  * of system.  List may be used
                   1646:  * in later ioctl's (above) to get
                   1647:  * other information.
                   1648:  */
                   1649: /*ARGSUSED*/
1.15      mycroft  1650: int
1.1       cgd      1651: ifconf(cmd, data)
1.19      mycroft  1652:        u_long cmd;
1.1       cgd      1653:        caddr_t data;
                   1654: {
1.61      augustss 1655:        struct ifconf *ifc = (struct ifconf *)data;
                   1656:        struct ifnet *ifp;
                   1657:        struct ifaddr *ifa;
1.1       cgd      1658:        struct ifreq ifr, *ifrp;
                   1659:        int space = ifc->ifc_len, error = 0;
1.127     christos 1660:        const int sz = (int)sizeof(ifr);
                   1661:        int sign;
1.1       cgd      1662:
1.127     christos 1663:        if ((ifrp = ifc->ifc_req) == NULL) {
                   1664:                space = 0;
                   1665:                sign = -1;
                   1666:        } else {
                   1667:                sign = 1;
                   1668:        }
1.98      matt     1669:        TAILQ_FOREACH(ifp, &ifnet, if_list) {
1.34      thorpej  1670:                bcopy(ifp->if_xname, ifr.ifr_name, IFNAMSIZ);
1.98      matt     1671:                if ((ifa = TAILQ_FIRST(&ifp->if_addrlist)) == 0) {
1.127     christos 1672:                        memset(&ifr.ifr_addr, 0, sizeof(ifr.ifr_addr));
                   1673:                        if (ifrp != NULL && space >= sz) {
                   1674:                                error = copyout(&ifr, ifrp, sz);
1.70      mellon   1675:                                if (error)
                   1676:                                        break;
1.127     christos 1677:                                ifrp++;
1.70      mellon   1678:                        }
1.127     christos 1679:                        space -= sizeof(ifr) * sign;
                   1680:                        continue;
                   1681:                }
                   1682:
                   1683:                for (; ifa != 0; ifa = TAILQ_NEXT(ifa, ifa_list)) {
1.61      augustss 1684:                        struct sockaddr *sa = ifa->ifa_addr;
1.119     christos 1685: #if defined(COMPAT_43) || defined(COMPAT_LINUX) || defined(COMPAT_SVR4) || defined(COMPAT_ULTRIX)
1.1       cgd      1686:                        if (cmd == OSIOCGIFCONF) {
                   1687:                                struct osockaddr *osa =
                   1688:                                         (struct osockaddr *)&ifr.ifr_addr;
1.127     christos 1689:                                /*
                   1690:                                 * If it does not fit, we don't bother with it
                   1691:                                 */
                   1692:                                if (sa->sa_len > sizeof(*osa))
                   1693:                                        continue;
1.1       cgd      1694:                                ifr.ifr_addr = *sa;
                   1695:                                osa->sa_family = sa->sa_family;
1.127     christos 1696:                                if (ifrp != NULL && space >= sz) {
                   1697:                                        error = copyout(&ifr, ifrp, sz);
1.70      mellon   1698:                                        ifrp++;
                   1699:                                }
1.1       cgd      1700:                        } else
                   1701: #endif
                   1702:                        if (sa->sa_len <= sizeof(*sa)) {
                   1703:                                ifr.ifr_addr = *sa;
1.127     christos 1704:                                if (ifrp != NULL && space >= sz) {
                   1705:                                        error = copyout(&ifr, ifrp, sz);
1.70      mellon   1706:                                        ifrp++;
                   1707:                                }
1.1       cgd      1708:                        } else {
1.127     christos 1709:                                space -= (sa->sa_len - sizeof(*sa)) * sign;
                   1710:                                if (ifrp != NULL && space >= sz) {
                   1711:                                        error = copyout(&ifr, ifrp,
                   1712:                                            sizeof(ifr.ifr_name));
1.70      mellon   1713:                                        if (error == 0) {
1.127     christos 1714:                                                error = copyout(sa,
                   1715:                                                    &ifrp->ifr_addr,
                   1716:                                                    sa->sa_len);
1.70      mellon   1717:                                        }
                   1718:                                        ifrp = (struct ifreq *)
                   1719:                                                (sa->sa_len +
                   1720:                                                 (caddr_t)&ifrp->ifr_addr);
                   1721:                                }
1.1       cgd      1722:                        }
                   1723:                        if (error)
                   1724:                                break;
1.127     christos 1725:                        space -= sz * sign;
1.1       cgd      1726:                }
                   1727:        }
1.127     christos 1728:        if (ifrp != NULL)
                   1729:                ifc->ifc_len -= space;
                   1730:        else
                   1731:                ifc->ifc_len = space;
1.1       cgd      1732:        return (error);
                   1733: }
1.133     jonathan 1734:
1.138     drochner 1735: #if defined(INET) || defined(INET6)
1.136     atatat   1736: static void
1.139     atatat   1737: sysctl_net_ifq_setup(struct sysctllog **clog,
                   1738:                     int pf, const char *pfname,
1.136     atatat   1739:                     int ipn, const char *ipname,
                   1740:                     int qid, struct ifqueue *ifq)
                   1741: {
                   1742:
1.139     atatat   1743:        sysctl_createv(clog, 0, NULL, NULL,
                   1744:                       CTLFLAG_PERMANENT,
1.136     atatat   1745:                       CTLTYPE_NODE, "net", NULL,
                   1746:                       NULL, 0, NULL, 0,
                   1747:                       CTL_NET, CTL_EOL);
1.139     atatat   1748:        sysctl_createv(clog, 0, NULL, NULL,
                   1749:                       CTLFLAG_PERMANENT,
1.136     atatat   1750:                       CTLTYPE_NODE, pfname, NULL,
                   1751:                       NULL, 0, NULL, 0,
                   1752:                       CTL_NET, pf, CTL_EOL);
1.139     atatat   1753:        sysctl_createv(clog, 0, NULL, NULL,
                   1754:                       CTLFLAG_PERMANENT,
1.136     atatat   1755:                       CTLTYPE_NODE, ipname, NULL,
                   1756:                       NULL, 0, NULL, 0,
                   1757:                       CTL_NET, pf, ipn, CTL_EOL);
1.139     atatat   1758:        sysctl_createv(clog, 0, NULL, NULL,
                   1759:                       CTLFLAG_PERMANENT,
1.142     atatat   1760:                       CTLTYPE_NODE, "ifq",
                   1761:                       SYSCTL_DESCR("Protocol input queue controls"),
1.139     atatat   1762:                       NULL, 0, NULL, 0,
                   1763:                       CTL_NET, pf, ipn, qid, CTL_EOL);
1.136     atatat   1764:
1.139     atatat   1765:        sysctl_createv(clog, 0, NULL, NULL,
                   1766:                       CTLFLAG_PERMANENT,
1.142     atatat   1767:                       CTLTYPE_INT, "len",
                   1768:                       SYSCTL_DESCR("Current input queue length"),
1.136     atatat   1769:                       NULL, 0, &ifq->ifq_len, 0,
                   1770:                       CTL_NET, pf, ipn, qid, IFQCTL_LEN, CTL_EOL);
1.139     atatat   1771:        sysctl_createv(clog, 0, NULL, NULL,
                   1772:                       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
1.142     atatat   1773:                       CTLTYPE_INT, "maxlen",
                   1774:                       SYSCTL_DESCR("Maximum allowed input queue length"),
1.136     atatat   1775:                       NULL, 0, &ifq->ifq_maxlen, 0,
                   1776:                       CTL_NET, pf, ipn, qid, IFQCTL_MAXLEN, CTL_EOL);
1.133     jonathan 1777: #ifdef notyet
1.139     atatat   1778:        sysctl_createv(clog, 0, NULL, NULL,
                   1779:                       CTLFLAG_PERMANENT,
1.142     atatat   1780:                       CTLTYPE_INT, "peak",
                   1781:                       SYSCTL_DESCR("Highest input queue length"),
1.136     atatat   1782:                       NULL, 0, &ifq->ifq_peak, 0,
                   1783:                       CTL_NET, pf, ipn, qid, IFQCTL_PEAK, CTL_EOL);
                   1784: #endif
1.139     atatat   1785:        sysctl_createv(clog, 0, NULL, NULL,
                   1786:                       CTLFLAG_PERMANENT,
1.142     atatat   1787:                       CTLTYPE_INT, "drops",
                   1788:                       SYSCTL_DESCR("Packets dropped due to full input queue"),
1.136     atatat   1789:                       NULL, 0, &ifq->ifq_drops, 0,
                   1790:                       CTL_NET, pf, ipn, qid, IFQCTL_DROPS, CTL_EOL);
                   1791: }
                   1792:
                   1793: #ifdef INET
                   1794: SYSCTL_SETUP(sysctl_net_inet_ip_ifq_setup,
                   1795:             "sysctl net.inet.ip.ifq subtree setup")
                   1796: {
                   1797:        extern struct ifqueue ipintrq;
                   1798:
1.139     atatat   1799:        sysctl_net_ifq_setup(clog, PF_INET, "inet", IPPROTO_IP, "ip",
1.136     atatat   1800:                             IPCTL_IFQ, &ipintrq);
1.133     jonathan 1801: }
1.136     atatat   1802: #endif /* INET */
1.133     jonathan 1803:
1.136     atatat   1804: #ifdef INET6
                   1805: SYSCTL_SETUP(sysctl_net_inet6_ip6_ifq_setup,
                   1806:             "sysctl net.inet6.ip6.ifq subtree setup")
                   1807: {
                   1808:        extern struct ifqueue ip6intrq;
                   1809:
1.139     atatat   1810:        sysctl_net_ifq_setup(clog, PF_INET6, "inet6", IPPROTO_IPV6, "ip6",
1.136     atatat   1811:                             IPV6CTL_IFQ, &ip6intrq);
                   1812: }
                   1813: #endif /* INET6 */
1.138     drochner 1814: #endif /* INET || INET6 */

CVSweb <webmaster@jp.NetBSD.org>