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

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

CVSweb <webmaster@jp.NetBSD.org>