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

Annotation of src/sys/net/if_ethersubr.c, Revision 1.224

1.224   ! knakahar    1: /*     $NetBSD: if_ethersubr.c,v 1.223 2016/06/16 03:03:33 ozaki-r Exp $       */
1.44      itojun      2:
                      3: /*
                      4:  * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
                      5:  * All rights reserved.
1.120     perry       6:  *
1.44      itojun      7:  * Redistribution and use in source and binary forms, with or without
                      8:  * modification, are permitted provided that the following conditions
                      9:  * are met:
                     10:  * 1. Redistributions of source code must retain the above copyright
                     11:  *    notice, this list of conditions and the following disclaimer.
                     12:  * 2. Redistributions in binary form must reproduce the above copyright
                     13:  *    notice, this list of conditions and the following disclaimer in the
                     14:  *    documentation and/or other materials provided with the distribution.
                     15:  * 3. Neither the name of the project nor the names of its contributors
                     16:  *    may be used to endorse or promote products derived from this software
                     17:  *    without specific prior written permission.
1.120     perry      18:  *
1.44      itojun     19:  * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
                     20:  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
                     21:  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
                     22:  * ARE DISCLAIMED.  IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
                     23:  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
                     24:  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
                     25:  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
                     26:  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
                     27:  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
                     28:  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
                     29:  * SUCH DAMAGE.
                     30:  */
1.9       cgd        31:
1.1       cgd        32: /*
1.8       mycroft    33:  * Copyright (c) 1982, 1989, 1993
                     34:  *     The Regents of the University of California.  All rights reserved.
1.1       cgd        35:  *
                     36:  * Redistribution and use in source and binary forms, with or without
                     37:  * modification, are permitted provided that the following conditions
                     38:  * are met:
                     39:  * 1. Redistributions of source code must retain the above copyright
                     40:  *    notice, this list of conditions and the following disclaimer.
                     41:  * 2. Redistributions in binary form must reproduce the above copyright
                     42:  *    notice, this list of conditions and the following disclaimer in the
                     43:  *    documentation and/or other materials provided with the distribution.
1.113     agc        44:  * 3. Neither the name of the University nor the names of its contributors
1.1       cgd        45:  *    may be used to endorse or promote products derived from this software
                     46:  *    without specific prior written permission.
                     47:  *
                     48:  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
                     49:  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
                     50:  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
                     51:  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
                     52:  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
                     53:  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
                     54:  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
                     55:  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
                     56:  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
                     57:  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
                     58:  * SUCH DAMAGE.
                     59:  *
1.27      fvdl       60:  *     @(#)if_ethersubr.c      8.2 (Berkeley) 4/4/96
1.1       cgd        61:  */
1.90      lukem      62:
                     63: #include <sys/cdefs.h>
1.224   ! knakahar   64: __KERNEL_RCSID(0, "$NetBSD: if_ethersubr.c,v 1.223 2016/06/16 03:03:33 ozaki-r Exp $");
1.1       cgd        65:
1.212     pooka      66: #ifdef _KERNEL_OPT
1.33      jonathan   67: #include "opt_inet.h"
                     68: #include "opt_atalk.h"
1.112     martin     69: #include "opt_mbuftrace.h"
1.182     kefren     70: #include "opt_mpls.h"
1.30      matt       71: #include "opt_gateway.h"
1.135     pavel      72: #include "opt_pppoe.h"
1.206     ozaki-r    73: #include "opt_net_mpsafe.h"
1.212     pooka      74: #endif
                     75:
1.59      thorpej    76: #include "vlan.h"
1.81      martin     77: #include "pppoe.h"
1.78      thorpej    78: #include "bridge.h"
1.93      martin     79: #include "arp.h"
1.121     yamt       80: #include "agr.h"
1.30      matt       81:
1.200     joerg      82: #include <sys/sysctl.h>
1.4       mycroft    83: #include <sys/malloc.h>
                     84: #include <sys/mbuf.h>
1.211     ozaki-r    85: #include <sys/mutex.h>
1.4       mycroft    86: #include <sys/ioctl.h>
                     87: #include <sys/errno.h>
1.158     dyoung     88: #include <sys/device.h>
1.204     tls        89: #include <sys/rnd.h>
1.207     riastrad   90: #include <sys/rndsource.h>
1.216     ozaki-r    91: #include <sys/cpu.h>
1.8       mycroft    92:
1.4       mycroft    93: #include <net/if.h>
                     94: #include <net/netisr.h>
                     95: #include <net/route.h>
                     96: #include <net/if_llc.h>
                     97: #include <net/if_dl.h>
1.8       mycroft    98: #include <net/if_types.h>
1.211     ozaki-r    99: #include <net/pktqueue.h>
1.93      martin    100:
1.158     dyoung    101: #include <net/if_media.h>
                    102: #include <dev/mii/mii.h>
                    103: #include <dev/mii/miivar.h>
                    104:
1.93      martin    105: #if NARP == 0
                    106: /*
1.102     jmmv      107:  * XXX there should really be a way to issue this warning from within config(8)
1.93      martin    108:  */
1.111     martin    109: #error You have included NETATALK or a pseudo-device in your configuration that depends on the presence of ethernet interfaces, but have no such interfaces configured. Check if you really need pseudo-device bridge, pppoe, vlan or options NETATALK.
1.93      martin    110: #endif
1.1       cgd       111:
1.69      thorpej   112: #include <net/bpf.h>
                    113:
1.22      is        114: #include <net/if_ether.h>
1.59      thorpej   115: #include <net/if_vlanvar.h>
1.22      is        116:
1.81      martin    117: #if NPPPOE > 0
                    118: #include <net/if_pppoe.h>
                    119: #endif
                    120:
1.121     yamt      121: #if NAGR > 0
                    122: #include <net/agr/ieee8023_slowprotocols.h>    /* XXX */
                    123: #include <net/agr/ieee8023ad.h>
                    124: #include <net/agr/if_agrvar.h>
                    125: #endif
                    126:
1.78      thorpej   127: #if NBRIDGE > 0
                    128: #include <net/if_bridgevar.h>
                    129: #endif
                    130:
1.15      phil      131: #include <netinet/in.h>
1.1       cgd       132: #ifdef INET
1.4       mycroft   133: #include <netinet/in_var.h>
1.1       cgd       134: #endif
1.22      is        135: #include <netinet/if_inarp.h>
1.1       cgd       136:
1.44      itojun    137: #ifdef INET6
                    138: #ifndef INET
                    139: #include <netinet/in.h>
                    140: #endif
                    141: #include <netinet6/in6_var.h>
                    142: #include <netinet6/nd6.h>
                    143: #endif
                    144:
1.1       cgd       145:
1.133     liamjfoy  146: #include "carp.h"
                    147: #if NCARP > 0
                    148: #include <netinet/ip_carp.h>
                    149: #endif
                    150:
1.23      christos  151: #ifdef NETATALK
                    152: #include <netatalk/at.h>
                    153: #include <netatalk/at_var.h>
                    154: #include <netatalk/at_extern.h>
                    155:
                    156: #define llc_snap_org_code llc_un.type_snap.org_code
                    157: #define llc_snap_ether_type llc_un.type_snap.ether_type
                    158:
                    159: extern u_char  at_org_code[3];
                    160: extern u_char  aarp_org_code[3];
                    161: #endif /* NETATALK */
                    162:
1.182     kefren    163: #ifdef MPLS
                    164: #include <netmpls/mpls.h>
                    165: #include <netmpls/mpls_var.h>
                    166: #endif
                    167:
1.123     matt      168: static struct timeval bigpktppslim_last;
                    169: static int bigpktppslim = 2;   /* XXX */
                    170: static int bigpktpps_count;
1.203     ozaki-r   171: static kmutex_t bigpktpps_lock __cacheline_aligned;
1.123     matt      172:
                    173:
1.118     yamt      174: const uint8_t etherbroadcastaddr[ETHER_ADDR_LEN] =
                    175:     { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
1.121     yamt      176: const uint8_t ethermulticastaddr_slowprotocols[ETHER_ADDR_LEN] =
                    177:     { 0x01, 0x80, 0xc2, 0x00, 0x00, 0x02 };
1.8       mycroft   178: #define senderr(e) { error = (e); goto bad;}
1.1       cgd       179:
1.128     thorpej   180: static int ether_output(struct ifnet *, struct mbuf *,
1.222     ozaki-r   181:            const struct sockaddr *, const struct rtentry *);
1.42      thorpej   182:
1.1       cgd       183: /*
                    184:  * Ethernet output routine.
                    185:  * Encapsulate a packet of type family for the local net.
1.22      is        186:  * Assumes that ifp is actually pointer to ethercom structure.
1.1       cgd       187:  */
1.42      thorpej   188: static int
1.178     dyoung    189: ether_output(struct ifnet * const ifp0, struct mbuf * const m0,
                    190:        const struct sockaddr * const dst,
1.222     ozaki-r   191:        const struct rtentry *rt)
1.1       cgd       192: {
1.162     matt      193:        uint16_t etype = 0;
1.122     christos  194:        int error = 0, hdrcmplt = 0;
1.145     dyoung    195:        uint8_t esrc[6], edst[6];
1.29      mrg       196:        struct mbuf *m = m0;
1.151     dyoung    197:        struct mbuf *mcopy = NULL;
1.29      mrg       198:        struct ether_header *eh;
1.133     liamjfoy  199:        struct ifnet *ifp = ifp0;
1.24      christos  200: #ifdef INET
1.22      is        201:        struct arphdr *ah;
1.24      christos  202: #endif /* INET */
1.23      christos  203: #ifdef NETATALK
                    204:        struct at_ifaddr *aa;
                    205: #endif /* NETATALK */
1.1       cgd       206:
1.224   ! knakahar  207:        KASSERT(ifp->if_extflags & IFEF_OUTPUT_MPSAFE);
1.197     bouyer    208:
1.104     matt      209: #ifdef MBUFTRACE
1.116     jonathan  210:        m_claimm(m, ifp->if_mowner);
1.104     matt      211: #endif
1.133     liamjfoy  212:
                    213: #if NCARP > 0
                    214:        if (ifp->if_type == IFT_CARP) {
                    215:                struct ifaddr *ifa;
                    216:
                    217:                /* loop back if this is going to the carp interface */
                    218:                if (dst != NULL && ifp0->if_link_state == LINK_STATE_UP &&
                    219:                    (ifa = ifa_ifwithaddr(dst)) != NULL &&
                    220:                    ifa->ifa_ifp == ifp0)
1.210     ozaki-r   221:                        return looutput(ifp0, m, dst, rt);
1.133     liamjfoy  222:
                    223:                ifp = ifp->if_carpdev;
                    224:                /* ac = (struct arpcom *)ifp; */
                    225:
                    226:                if ((ifp0->if_flags & (IFF_UP|IFF_RUNNING)) !=
                    227:                    (IFF_UP|IFF_RUNNING))
                    228:                        senderr(ENETDOWN);
                    229:        }
                    230: #endif /* NCARP > 0 */
                    231:
1.8       mycroft   232:        if ((ifp->if_flags & (IFF_UP|IFF_RUNNING)) != (IFF_UP|IFF_RUNNING))
                    233:                senderr(ENETDOWN);
1.72      thorpej   234:
1.1       cgd       235:        switch (dst->sa_family) {
                    236:
                    237: #ifdef INET
                    238:        case AF_INET:
1.224   ! knakahar  239:                KERNEL_LOCK(1, NULL);
1.22      is        240:                if (m->m_flags & M_BCAST)
1.174     isaki     241:                        (void)memcpy(edst, etherbroadcastaddr, sizeof(edst));
1.145     dyoung    242:                else if (m->m_flags & M_MCAST)
                    243:                        ETHER_MAP_IP_MULTICAST(&satocsin(dst)->sin_addr, edst);
1.217     christos  244:                else if ((error = arpresolve(ifp, rt, m, dst, edst,
1.224   ! knakahar  245:                    sizeof(edst))) != 0) {
        !           246:                        KERNEL_UNLOCK_ONE(NULL);
1.214     roy       247:                        return error == EWOULDBLOCK ? 0 : error;
1.224   ! knakahar  248:                }
        !           249:                KERNEL_UNLOCK_ONE(NULL);
1.3       hpeyerl   250:                /* If broadcasting on a simplex interface, loopback a copy */
                    251:                if ((m->m_flags & M_BCAST) && (ifp->if_flags & IFF_SIMPLEX))
1.1       cgd       252:                        mcopy = m_copy(m, 0, (int)M_COPYALL);
1.17      mycroft   253:                etype = htons(ETHERTYPE_IP);
1.8       mycroft   254:                break;
1.22      is        255:
                    256:        case AF_ARP:
                    257:                ah = mtod(m, struct arphdr *);
                    258:                if (m->m_flags & M_BCAST)
1.174     isaki     259:                        (void)memcpy(edst, etherbroadcastaddr, sizeof(edst));
1.131     mrg       260:                else {
1.147     christos  261:                        void *tha = ar_tha(ah);
1.131     mrg       262:
1.173     christos  263:                        if (tha == NULL) {
                    264:                                /* fake with ARPHDR_IEEE1394 */
                    265:                                return 0;
                    266:                        }
1.146     dyoung    267:                        memcpy(edst, tha, sizeof(edst));
1.131     mrg       268:                }
1.120     perry     269:
1.22      is        270:                ah->ar_hrd = htons(ARPHRD_ETHER);
                    271:
1.107     itojun    272:                switch (ntohs(ah->ar_op)) {
1.22      is        273:                case ARPOP_REVREQUEST:
                    274:                case ARPOP_REVREPLY:
                    275:                        etype = htons(ETHERTYPE_REVARP);
                    276:                        break;
                    277:
                    278:                case ARPOP_REQUEST:
                    279:                case ARPOP_REPLY:
                    280:                default:
                    281:                        etype = htons(ETHERTYPE_ARP);
                    282:                }
                    283:
                    284:                break;
1.1       cgd       285: #endif
1.44      itojun    286: #ifdef INET6
                    287:        case AF_INET6:
1.166     dyoung    288:                if (!nd6_storelladdr(ifp, rt, m, dst, edst, sizeof(edst))){
1.67      itojun    289:                        /* something bad happened */
1.99      itojun    290:                        return (0);
1.51      itojun    291:                }
1.44      itojun    292:                etype = htons(ETHERTYPE_IPV6);
                    293:                break;
                    294: #endif
1.23      christos  295: #ifdef NETATALK
                    296:     case AF_APPLETALK:
1.224   ! knakahar  297:                KERNEL_LOCK(1, NULL);
1.214     roy       298:                if (aarpresolve(ifp, m, (const struct sockaddr_at *)dst, edst)) {
1.23      christos  299: #ifdef NETATALKDEBUG
                    300:                        printf("aarpresolv failed\n");
                    301: #endif /* NETATALKDEBUG */
1.224   ! knakahar  302:                        KERNEL_UNLOCK_ONE(NULL);
1.23      christos  303:                        return (0);
                    304:                }
                    305:                /*
                    306:                 * ifaddr is the first thing in at_ifaddr
                    307:                 */
                    308:                aa = (struct at_ifaddr *) at_ifawithnet(
1.145     dyoung    309:                    (const struct sockaddr_at *)dst, ifp);
1.224   ! knakahar  310:                if (aa == NULL) {
        !           311:                    KERNEL_UNLOCK_ONE(NULL);
1.23      christos  312:                    goto bad;
1.224   ! knakahar  313:                }
1.120     perry     314:
1.23      christos  315:                /*
                    316:                 * In the phase 2 case, we need to prepend an mbuf for the
                    317:                 * llc header.  Since we must preserve the value of m,
                    318:                 * which is passed to us by value, we m_copy() the first
                    319:                 * mbuf, and use it for our llc header.
                    320:                 */
                    321:                if (aa->aa_flags & AFA_PHASE2) {
                    322:                        struct llc llc;
                    323:
1.43      bouyer    324:                        M_PREPEND(m, sizeof(struct llc), M_DONTWAIT);
1.23      christos  325:                        llc.llc_dsap = llc.llc_ssap = LLC_SNAP_LSAP;
                    326:                        llc.llc_control = LLC_UI;
1.146     dyoung    327:                        memcpy(llc.llc_snap_org_code, at_org_code,
1.23      christos  328:                            sizeof(llc.llc_snap_org_code));
1.38      kim       329:                        llc.llc_snap_ether_type = htons(ETHERTYPE_ATALK);
1.147     christos  330:                        memcpy(mtod(m, void *), &llc, sizeof(struct llc));
1.23      christos  331:                } else {
1.38      kim       332:                        etype = htons(ETHERTYPE_ATALK);
1.23      christos  333:                }
1.224   ! knakahar  334:                KERNEL_UNLOCK_ONE(NULL);
1.23      christos  335:                break;
                    336: #endif /* NETATALK */
1.31      thorpej   337:        case pseudo_AF_HDRCMPLT:
                    338:                hdrcmplt = 1;
1.146     dyoung    339:                memcpy(esrc,
                    340:                    ((const struct ether_header *)dst->sa_data)->ether_shost,
                    341:                    sizeof(esrc));
1.31      thorpej   342:                /* FALLTHROUGH */
                    343:
1.1       cgd       344:        case AF_UNSPEC:
1.146     dyoung    345:                memcpy(edst,
                    346:                    ((const struct ether_header *)dst->sa_data)->ether_dhost,
                    347:                    sizeof(edst));
1.8       mycroft   348:                /* AF_UNSPEC doesn't swap the byte order of the ether_type. */
1.145     dyoung    349:                etype = ((const struct ether_header *)dst->sa_data)->ether_type;
1.8       mycroft   350:                break;
1.1       cgd       351:
                    352:        default:
1.21      christos  353:                printf("%s: can't handle af%d\n", ifp->if_xname,
1.1       cgd       354:                        dst->sa_family);
1.8       mycroft   355:                senderr(EAFNOSUPPORT);
1.1       cgd       356:        }
                    357:
1.182     kefren    358: #ifdef MPLS
1.224   ! knakahar  359:        KERNEL_LOCK(1, NULL);
1.210     ozaki-r   360:        {
                    361:                struct m_tag *mtag;
                    362:                mtag = m_tag_find(m, PACKET_TAG_MPLS, NULL);
                    363:                if (mtag != NULL) {
                    364:                        /* Having the tag itself indicates it's MPLS */
1.184     dyoung    365:                        etype = htons(ETHERTYPE_MPLS);
1.210     ozaki-r   366:                        m_tag_delete(m, mtag);
                    367:                }
1.184     dyoung    368:        }
1.224   ! knakahar  369:        KERNEL_UNLOCK_ONE(NULL);
1.182     kefren    370: #endif
                    371:
1.1       cgd       372:        if (mcopy)
1.145     dyoung    373:                (void)looutput(ifp, mcopy, dst, rt);
1.16      mycroft   374:
1.50      matt      375:        /* If no ether type is set, this must be a 802.2 formatted packet.
                    376:         */
                    377:        if (etype == 0)
                    378:                etype = htons(m->m_pkthdr.len);
1.1       cgd       379:        /*
                    380:         * Add local net header.  If no space in first mbuf,
                    381:         * allocate another.
                    382:         */
                    383:        M_PREPEND(m, sizeof (struct ether_header), M_DONTWAIT);
1.8       mycroft   384:        if (m == 0)
                    385:                senderr(ENOBUFS);
1.1       cgd       386:        eh = mtod(m, struct ether_header *);
1.96      thorpej   387:        /* Note: etype is already in network byte order. */
1.143     cbiere    388:        (void)memcpy(&eh->ether_type, &etype, sizeof(eh->ether_type));
1.146     dyoung    389:        memcpy(eh->ether_dhost, edst, sizeof(edst));
1.31      thorpej   390:        if (hdrcmplt)
1.146     dyoung    391:                memcpy(eh->ether_shost, esrc, sizeof(eh->ether_shost));
1.31      thorpej   392:        else
1.153     dyoung    393:                memcpy(eh->ether_shost, CLLADDR(ifp->if_sadl),
1.31      thorpej   394:                    sizeof(eh->ether_shost));
1.77      thorpej   395:
1.133     liamjfoy  396: #if NCARP > 0
                    397:        if (ifp0 != ifp && ifp0->if_type == IFT_CARP) {
1.153     dyoung    398:                memcpy(eh->ether_shost, CLLADDR(ifp0->if_sadl),
1.133     liamjfoy  399:                    sizeof(eh->ether_shost));
                    400:        }
                    401: #endif /* NCARP > 0 */
                    402:
1.195     rmind     403:        if ((error = pfil_run_hooks(ifp->if_pfil, &m, ifp, PFIL_OUT)) != 0)
1.77      thorpej   404:                return (error);
                    405:        if (m == NULL)
                    406:                return (0);
                    407:
1.78      thorpej   408: #if NBRIDGE > 0
                    409:        /*
                    410:         * Bridges require special output handling.
                    411:         */
                    412:        if (ifp->if_bridge)
                    413:                return (bridge_output(ifp, m, NULL, NULL));
                    414: #endif
                    415:
1.133     liamjfoy  416: #if NCARP > 0
                    417:        if (ifp != ifp0)
                    418:                ifp0->if_obytes += m->m_pkthdr.len + ETHER_HDR_LEN;
                    419: #endif /* NCARP > 0 */
                    420:
1.77      thorpej   421: #ifdef ALTQ
1.224   ! knakahar  422:        KERNEL_LOCK(1, NULL);
1.77      thorpej   423:        /*
                    424:         * If ALTQ is enabled on the parent interface, do
                    425:         * classification; the queueing discipline might not
                    426:         * require classification, but might require the
                    427:         * address family/header pointer in the pktattr.
                    428:         */
                    429:        if (ALTQ_IS_ENABLED(&ifp->if_snd))
1.220     knakahar  430:                altq_etherclassify(&ifp->if_snd, m);
1.224   ! knakahar  431:        KERNEL_UNLOCK_ONE(NULL);
1.77      thorpej   432: #endif
1.221     knakahar  433:        return ifq_enqueue(ifp, m);
1.1       cgd       434:
                    435: bad:
                    436:        if (m)
                    437:                m_freem(m);
                    438:        return (error);
                    439: }
1.76      thorpej   440:
                    441: #ifdef ALTQ
                    442: /*
                    443:  * This routine is a slight hack to allow a packet to be classified
                    444:  * if the Ethernet headers are present.  It will go away when ALTQ's
                    445:  * classification engine understands link headers.
                    446:  */
                    447: void
1.220     knakahar  448: altq_etherclassify(struct ifaltq *ifq, struct mbuf *m)
1.76      thorpej   449: {
                    450:        struct ether_header *eh;
1.162     matt      451:        uint16_t ether_type;
1.76      thorpej   452:        int hlen, af, hdrsize;
1.147     christos  453:        void *hdr;
1.76      thorpej   454:
                    455:        hlen = ETHER_HDR_LEN;
                    456:        eh = mtod(m, struct ether_header *);
                    457:
                    458:        ether_type = htons(eh->ether_type);
                    459:
                    460:        if (ether_type < ETHERMTU) {
                    461:                /* LLC/SNAP */
                    462:                struct llc *llc = (struct llc *)(eh + 1);
                    463:                hlen += 8;
                    464:
                    465:                if (m->m_len < hlen ||
                    466:                    llc->llc_dsap != LLC_SNAP_LSAP ||
                    467:                    llc->llc_ssap != LLC_SNAP_LSAP ||
                    468:                    llc->llc_control != LLC_UI) {
                    469:                        /* Not SNAP. */
                    470:                        goto bad;
                    471:                }
                    472:
                    473:                ether_type = htons(llc->llc_un.type_snap.ether_type);
                    474:        }
                    475:
                    476:        switch (ether_type) {
                    477:        case ETHERTYPE_IP:
                    478:                af = AF_INET;
                    479:                hdrsize = 20;           /* sizeof(struct ip) */
                    480:                break;
                    481:
                    482:        case ETHERTYPE_IPV6:
                    483:                af = AF_INET6;
                    484:                hdrsize = 40;           /* sizeof(struct ip6_hdr) */
                    485:                break;
                    486:
                    487:        default:
                    488:                af = AF_UNSPEC;
                    489:                hdrsize = 0;
                    490:                break;
                    491:        }
                    492:
1.95      itojun    493:        while (m->m_len <= hlen) {
                    494:                hlen -= m->m_len;
                    495:                m = m->m_next;
                    496:        }
1.76      thorpej   497:        if (m->m_len < (hlen + hdrsize)) {
                    498:                /*
1.95      itojun    499:                 * protocol header not in a single mbuf.
                    500:                 * We can't cope with this situation right
1.76      thorpej   501:                 * now (but it shouldn't ever happen, really, anyhow).
                    502:                 */
1.92      itojun    503: #ifdef DEBUG
1.78      thorpej   504:                printf("altq_etherclassify: headers span multiple mbufs: "
                    505:                    "%d < %d\n", m->m_len, (hlen + hdrsize));
1.92      itojun    506: #endif
1.76      thorpej   507:                goto bad;
                    508:        }
                    509:
                    510:        m->m_data += hlen;
                    511:        m->m_len -= hlen;
                    512:
1.147     christos  513:        hdr = mtod(m, void *);
1.76      thorpej   514:
                    515:        if (ALTQ_NEEDS_CLASSIFY(ifq))
1.219     knakahar  516:                m->m_pkthdr.pattr_class =
1.76      thorpej   517:                    (*ifq->altq_classify)(ifq->altq_clfier, m, af);
1.219     knakahar  518:        m->m_pkthdr.pattr_af = af;
                    519:        m->m_pkthdr.pattr_hdr = hdr;
1.76      thorpej   520:
                    521:        m->m_data -= hlen;
                    522:        m->m_len += hlen;
                    523:
                    524:        return;
                    525:
                    526:  bad:
1.219     knakahar  527:        m->m_pkthdr.pattr_class = NULL;
                    528:        m->m_pkthdr.pattr_hdr = NULL;
                    529:        m->m_pkthdr.pattr_af = AF_UNSPEC;
1.76      thorpej   530: }
                    531: #endif /* ALTQ */
1.1       cgd       532:
                    533: /*
                    534:  * Process a received Ethernet packet;
1.42      thorpej   535:  * the packet is in the mbuf chain m with
                    536:  * the ether header.
1.1       cgd       537:  */
1.133     liamjfoy  538: void
1.58      matt      539: ether_input(struct ifnet *ifp, struct mbuf *m)
1.1       cgd       540: {
1.91      thorpej   541:        struct ethercom *ec = (struct ethercom *) ifp;
1.199     rmind     542:        pktqueue_t *pktq = NULL;
                    543:        struct ifqueue *inq = NULL;
1.162     matt      544:        uint16_t etype;
1.42      thorpej   545:        struct ether_header *eh;
1.187     matt      546:        size_t ehlen;
1.204     tls       547:        static int earlypkts;
1.198     msaitoh   548:        int isr = 0;
1.194     joerg     549: #if defined (LLC) || defined(NETATALK)
1.29      mrg       550:        struct llc *l;
1.18      christos  551: #endif
1.1       cgd       552:
1.216     ozaki-r   553:        KASSERT(!cpu_intr_p());
                    554:
1.8       mycroft   555:        if ((ifp->if_flags & IFF_UP) == 0) {
                    556:                m_freem(m);
                    557:                return;
                    558:        }
1.42      thorpej   559:
1.104     matt      560: #ifdef MBUFTRACE
1.116     jonathan  561:        m_claimm(m, &ec->ec_rx_mowner);
1.104     matt      562: #endif
1.42      thorpej   563:        eh = mtod(m, struct ether_header *);
1.63      thorpej   564:        etype = ntohs(eh->ether_type);
1.187     matt      565:        ehlen = sizeof(*eh);
1.63      thorpej   566:
1.204     tls       567:        if(__predict_false(earlypkts < 100 || !rnd_initial_entropy)) {
                    568:                rnd_add_data(NULL, eh, ehlen, 0);
                    569:                earlypkts++;
                    570:        }
                    571:
1.63      thorpej   572:        /*
                    573:         * Determine if the packet is within its size limits.
                    574:         */
1.182     kefren    575:        if (etype != ETHERTYPE_MPLS && m->m_pkthdr.len >
1.83      thorpej   576:            ETHER_MAX_FRAME(ifp, etype, m->m_flags & M_HASFCS)) {
1.203     ozaki-r   577:                mutex_enter(&bigpktpps_lock);
1.123     matt      578:                if (ppsratecheck(&bigpktppslim_last, &bigpktpps_count,
                    579:                            bigpktppslim)) {
                    580:                        printf("%s: discarding oversize frame (len=%d)\n",
                    581:                            ifp->if_xname, m->m_pkthdr.len);
                    582:                }
1.203     ozaki-r   583:                mutex_exit(&bigpktpps_lock);
1.63      thorpej   584:                m_freem(m);
                    585:                return;
                    586:        }
1.77      thorpej   587:
1.84      thorpej   588:        if (ETHER_IS_MULTICAST(eh->ether_dhost)) {
                    589:                /*
                    590:                 * If this is not a simplex interface, drop the packet
                    591:                 * if it came from us.
                    592:                 */
                    593:                if ((ifp->if_flags & IFF_SIMPLEX) == 0 &&
1.153     dyoung    594:                    memcmp(CLLADDR(ifp->if_sadl), eh->ether_shost,
1.84      thorpej   595:                    ETHER_ADDR_LEN) == 0) {
                    596:                        m_freem(m);
                    597:                        return;
                    598:                }
                    599:
                    600:                if (memcmp(etherbroadcastaddr,
                    601:                    eh->ether_dhost, ETHER_ADDR_LEN) == 0)
                    602:                        m->m_flags |= M_BCAST;
                    603:                else
                    604:                        m->m_flags |= M_MCAST;
                    605:                ifp->if_imcasts++;
                    606:        }
                    607:
1.79      thorpej   608:        /* If the CRC is still on the packet, trim it off. */
                    609:        if (m->m_flags & M_HASFCS) {
                    610:                m_adj(m, -ETHER_CRC_LEN);
                    611:                m->m_flags &= ~M_HASFCS;
                    612:        }
                    613:
1.42      thorpej   614:        ifp->if_ibytes += m->m_pkthdr.len;
1.78      thorpej   615:
1.201     ozaki-r   616: #if NCARP > 0
                    617:        if (__predict_false(ifp->if_carp && ifp->if_type != IFT_CARP)) {
                    618:                /*
                    619:                 * clear M_PROMISC, in case the packets comes from a
                    620:                 * vlan
                    621:                 */
1.137     rpaulo    622:                m->m_flags &= ~M_PROMISC;
1.201     ozaki-r   623:                if (carp_input(m, (uint8_t *)&eh->ether_shost,
                    624:                    (uint8_t *)&eh->ether_dhost, eh->ether_type) == 0)
1.137     rpaulo    625:                        return;
1.201     ozaki-r   626:        }
1.133     liamjfoy  627: #endif /* NCARP > 0 */
1.201     ozaki-r   628:        if ((m->m_flags & (M_BCAST|M_MCAST|M_PROMISC)) == 0 &&
                    629:            (ifp->if_flags & IFF_PROMISC) != 0 &&
                    630:            memcmp(CLLADDR(ifp->if_sadl), eh->ether_dhost,
                    631:                   ETHER_ADDR_LEN) != 0) {
                    632:                m->m_flags |= M_PROMISC;
1.70      bouyer    633:        }
1.78      thorpej   634:
1.106     bouyer    635:        if ((m->m_flags & M_PROMISC) == 0) {
1.195     rmind     636:                if (pfil_run_hooks(ifp->if_pfil, &m, ifp, PFIL_IN) != 0)
1.106     bouyer    637:                        return;
                    638:                if (m == NULL)
                    639:                        return;
1.78      thorpej   640:
1.106     bouyer    641:                eh = mtod(m, struct ether_header *);
                    642:                etype = ntohs(eh->ether_type);
1.187     matt      643:                ehlen = sizeof(*eh);
1.106     bouyer    644:        }
1.70      bouyer    645:
1.172     darran    646: #if NAGR > 0
                    647:        if (ifp->if_agrprivate &&
                    648:            __predict_true(etype != ETHERTYPE_SLOWPROTOCOLS)) {
                    649:                m->m_flags &= ~M_PROMISC;
                    650:                agr_input(ifp, m);
                    651:                return;
                    652:        }
                    653: #endif /* NAGR > 0 */
                    654:
1.91      thorpej   655:        /*
                    656:         * If VLANs are configured on the interface, check to
                    657:         * see if the device performed the decapsulation and
                    658:         * provided us with the tag.
                    659:         */
1.114     simonb    660:        if (ec->ec_nvlans && m_tag_find(m, PACKET_TAG_VLAN, NULL) != NULL) {
1.70      bouyer    661: #if NVLAN > 0
                    662:                /*
                    663:                 * vlan_input() will either recursively call ether_input()
                    664:                 * or drop the packet.
                    665:                 */
1.91      thorpej   666:                vlan_input(ifp, m);
                    667: #else
                    668:                m_freem(m);
1.70      bouyer    669: #endif
1.61      thorpej   670:                return;
                    671:        }
1.1       cgd       672:
1.59      thorpej   673:        /*
                    674:         * Handle protocols that expect to have the Ethernet header
                    675:         * (and possibly FCS) intact.
                    676:         */
                    677:        switch (etype) {
1.187     matt      678:        case ETHERTYPE_VLAN: {
                    679:                struct ether_vlan_header *evl = (void *)eh;
                    680:                /*
                    681:                 * If there is a tag of 0, then the VLAN header was probably
                    682:                 * just being used to store the priority.  Extract the ether
                    683:                 * type, and if IP or IPV6, let them deal with it.
                    684:                 */
                    685:                if (m->m_len <= sizeof(*evl)
                    686:                    && EVL_VLANOFTAG(evl->evl_tag) == 0) {
                    687:                        etype = ntohs(evl->evl_proto);
                    688:                        ehlen = sizeof(*evl);
                    689:                        if ((m->m_flags & M_PROMISC) == 0
                    690:                            && (etype == ETHERTYPE_IP
                    691:                                || etype == ETHERTYPE_IPV6))
                    692:                                break;
                    693:                }
1.59      thorpej   694: #if NVLAN > 0
                    695:                /*
                    696:                 * vlan_input() will either recursively call ether_input()
                    697:                 * or drop the packet.
                    698:                 */
1.63      thorpej   699:                if (((struct ethercom *)ifp)->ec_nvlans != 0)
                    700:                        vlan_input(ifp, m);
1.65      enami     701:                else
1.187     matt      702: #endif /* NVLAN > 0 */
1.65      enami     703:                        m_freem(m);
1.59      thorpej   704:                return;
1.187     matt      705:        }
1.81      martin    706: #if NPPPOE > 0
                    707:        case ETHERTYPE_PPPOEDISC:
1.218     ozaki-r   708:                pppoedisc_input(ifp, m);
                    709:                return;
1.81      martin    710:        case ETHERTYPE_PPPOE:
1.218     ozaki-r   711:                pppoe_input(ifp, m);
1.81      martin    712:                return;
                    713: #endif /* NPPPOE > 0 */
1.121     yamt      714:        case ETHERTYPE_SLOWPROTOCOLS: {
                    715:                uint8_t subtype;
                    716:
                    717: #if defined(DIAGNOSTIC)
                    718:                if (m->m_pkthdr.len < sizeof(*eh) + sizeof(subtype)) {
                    719:                        panic("ether_input: too short slow protocol packet");
                    720:                }
                    721: #endif
                    722:                m_copydata(m, sizeof(*eh), sizeof(subtype), &subtype);
                    723:                switch (subtype) {
                    724: #if NAGR > 0
                    725:                case SLOWPROTOCOLS_SUBTYPE_LACP:
                    726:                        if (ifp->if_agrprivate) {
                    727:                                ieee8023ad_lacp_input(ifp, m);
                    728:                                return;
                    729:                        }
                    730:                        break;
                    731:
                    732:                case SLOWPROTOCOLS_SUBTYPE_MARKER:
                    733:                        if (ifp->if_agrprivate) {
                    734:                                ieee8023ad_marker_input(ifp, m);
                    735:                                return;
                    736:                        }
                    737:                        break;
                    738: #endif /* NAGR > 0 */
                    739:                default:
                    740:                        if (subtype == 0 || subtype > 10) {
                    741:                                /* illegal value */
                    742:                                m_freem(m);
                    743:                                return;
                    744:                        }
                    745:                        /* unknown subtype */
                    746:                        break;
                    747:                }
                    748:                /* FALLTHROUGH */
                    749:        }
1.59      thorpej   750:        default:
1.106     bouyer    751:                if (m->m_flags & M_PROMISC) {
                    752:                        m_freem(m);
                    753:                        return;
                    754:                }
1.59      thorpej   755:        }
1.42      thorpej   756:
1.45      thorpej   757:        /* If the CRC is still on the packet, trim it off. */
1.79      thorpej   758:        if (m->m_flags & M_HASFCS) {
1.45      thorpej   759:                m_adj(m, -ETHER_CRC_LEN);
1.79      thorpej   760:                m->m_flags &= ~M_HASFCS;
                    761:        }
1.42      thorpej   762:
1.139     is        763:        if (etype > ETHERMTU + sizeof (struct ether_header)) {
                    764:                /* Strip off the Ethernet header. */
1.187     matt      765:                m_adj(m, ehlen);
1.139     is        766:
                    767:                switch (etype) {
1.1       cgd       768: #ifdef INET
1.139     is        769:                case ETHERTYPE_IP:
1.30      matt      770: #ifdef GATEWAY
1.139     is        771:                        if (ipflow_fastforward(m))
                    772:                                return;
1.30      matt      773: #endif
1.199     rmind     774:                        pktq = ip_pktq;
1.139     is        775:                        break;
1.1       cgd       776:
1.139     is        777:                case ETHERTYPE_ARP:
1.198     msaitoh   778:                        isr = NETISR_ARP;
1.139     is        779:                        inq = &arpintrq;
                    780:                        break;
1.7       glass     781:
1.139     is        782:                case ETHERTYPE_REVARP:
                    783:                        revarpinput(m); /* XXX queue? */
                    784:                        return;
1.1       cgd       785: #endif
1.44      itojun    786: #ifdef INET6
1.139     is        787:                case ETHERTYPE_IPV6:
1.196     pooka     788:                        if (__predict_false(!in6_present)) {
                    789:                                m_freem(m);
                    790:                                return;
                    791:                        }
1.148     liamjfoy  792: #ifdef GATEWAY
1.192     christos  793:                        if (ip6flow_fastforward(&m))
1.148     liamjfoy  794:                                return;
                    795: #endif
1.199     rmind     796:                        pktq = ip6_pktq;
1.139     is        797:                        break;
1.44      itojun    798: #endif
1.23      christos  799: #ifdef NETATALK
1.174     isaki     800:                case ETHERTYPE_ATALK:
1.198     msaitoh   801:                        isr = NETISR_ATALK;
1.174     isaki     802:                        inq = &atintrq1;
                    803:                        break;
                    804:                case ETHERTYPE_AARP:
1.139     is        805:                        /* probably this should be done with a NETISR as well */
1.175     mbalmer   806:                        aarpinput(ifp, m); /* XXX */
1.174     isaki     807:                        return;
1.23      christos  808: #endif /* NETATALK */
1.182     kefren    809: #ifdef MPLS
                    810:                case ETHERTYPE_MPLS:
1.198     msaitoh   811:                        isr = NETISR_MPLS;
1.182     kefren    812:                        inq = &mplsintrq;
                    813:                        break;
                    814: #endif
1.139     is        815:                default:
                    816:                        m_freem(m);
                    817:                        return;
                    818:                }
                    819:        } else {
1.194     joerg     820: #if defined (LLC) || defined (NETATALK)
1.139     is        821:                l = (struct llc *)(eh+1);
1.8       mycroft   822:                switch (l->llc_dsap) {
1.23      christos  823: #ifdef NETATALK
                    824:                case LLC_SNAP_LSAP:
                    825:                        switch (l->llc_control) {
                    826:                        case LLC_UI:
                    827:                                if (l->llc_ssap != LLC_SNAP_LSAP) {
                    828:                                        goto dropanyway;
                    829:                                }
1.120     perry     830:
1.166     dyoung    831:                                if (memcmp(&(l->llc_snap_org_code)[0],
1.23      christos  832:                                    at_org_code, sizeof(at_org_code)) == 0 &&
                    833:                                    ntohs(l->llc_snap_ether_type) ==
1.38      kim       834:                                    ETHERTYPE_ATALK) {
1.23      christos  835:                                        inq = &atintrq2;
1.139     is        836:                                        m_adj(m, sizeof(struct ether_header)
                    837:                                            + sizeof(struct llc));
1.198     msaitoh   838:                                        isr = NETISR_ATALK;
1.23      christos  839:                                        break;
                    840:                                }
                    841:
1.166     dyoung    842:                                if (memcmp(&(l->llc_snap_org_code)[0],
1.23      christos  843:                                    aarp_org_code,
                    844:                                    sizeof(aarp_org_code)) == 0 &&
                    845:                                    ntohs(l->llc_snap_ether_type) ==
                    846:                                    ETHERTYPE_AARP) {
1.139     is        847:                                        m_adj( m, sizeof(struct ether_header)
                    848:                                            + sizeof(struct llc));
1.23      christos  849:                                        aarpinput(ifp, m); /* XXX */
                    850:                                    return;
                    851:                                }
1.120     perry     852:
1.23      christos  853:                        default:
                    854:                                goto dropanyway;
                    855:                        }
                    856:                        break;
1.1       cgd       857:                dropanyway:
1.142     bouyer    858: #endif
1.1       cgd       859:                default:
1.8       mycroft   860:                        m_freem(m);
                    861:                        return;
                    862:                }
1.139     is        863: #else /* ISO || LLC || NETATALK*/
                    864:                m_freem(m);
                    865:                return;
1.23      christos  866: #endif /* ISO || LLC || NETATALK*/
1.1       cgd       867:        }
                    868:
1.199     rmind     869:        if (__predict_true(pktq)) {
                    870:                const uint32_t h = pktq_rps_hash(m);
                    871:                if (__predict_false(!pktq_enqueue(pktq, m, h))) {
                    872:                        m_freem(m);
                    873:                }
                    874:                return;
                    875:        }
                    876:
                    877:        if (__predict_false(!inq)) {
                    878:                /* Should not happen. */
                    879:                m_freem(m);
                    880:                return;
                    881:        }
1.1       cgd       882:        if (IF_QFULL(inq)) {
                    883:                IF_DROP(inq);
                    884:                m_freem(m);
1.198     msaitoh   885:        } else {
1.1       cgd       886:                IF_ENQUEUE(inq, m);
1.198     msaitoh   887:                schednetisr(isr);
                    888:        }
1.1       cgd       889: }
                    890:
                    891: /*
                    892:  * Convert Ethernet address to printable (loggable) representation.
                    893:  */
                    894: char *
1.58      matt      895: ether_sprintf(const u_char *ap)
1.1       cgd       896: {
1.129     christos  897:        static char etherbuf[3 * ETHER_ADDR_LEN];
                    898:        return ether_snprintf(etherbuf, sizeof(etherbuf), ap);
                    899: }
                    900:
                    901: char *
                    902: ether_snprintf(char *buf, size_t len, const u_char *ap)
                    903: {
                    904:        char *cp = buf;
                    905:        size_t i;
1.1       cgd       906:
1.129     christos  907:        for (i = 0; i < len / 3; i++) {
1.124     christos  908:                *cp++ = hexdigits[*ap >> 4];
                    909:                *cp++ = hexdigits[*ap++ & 0xf];
1.1       cgd       910:                *cp++ = ':';
                    911:        }
1.129     christos  912:        *--cp = '\0';
                    913:        return buf;
1.1       cgd       914: }
1.8       mycroft   915:
                    916: /*
                    917:  * Perform common duties while attaching to interface list
                    918:  */
                    919: void
1.162     matt      920: ether_ifattach(struct ifnet *ifp, const uint8_t *lla)
1.8       mycroft   921: {
1.104     matt      922:        struct ethercom *ec = (struct ethercom *)ifp;
1.8       mycroft   923:
1.224   ! knakahar  924:        ifp->if_extflags |= IFEF_OUTPUT_MPSAFE;
1.8       mycroft   925:        ifp->if_type = IFT_ETHER;
1.94      enami     926:        ifp->if_hdrlen = ETHER_HDR_LEN;
1.73      thorpej   927:        ifp->if_dlt = DLT_EN10MB;
1.8       mycroft   928:        ifp->if_mtu = ETHERMTU;
1.12      mycroft   929:        ifp->if_output = ether_output;
1.216     ozaki-r   930:        ifp->_if_input = ether_input;
1.54      thorpej   931:        if (ifp->if_baudrate == 0)
                    932:                ifp->if_baudrate = IF_Mbps(10);         /* just a default */
1.75      thorpej   933:
1.170     dyoung    934:        if_set_sadl(ifp, lla, ETHER_ADDR_LEN, !ETHER_IS_LOCAL(lla));
1.75      thorpej   935:
1.104     matt      936:        LIST_INIT(&ec->ec_multiaddrs);
1.26      is        937:        ifp->if_broadcastaddr = etherbroadcastaddr;
1.177     joerg     938:        bpf_attach(ifp, DLT_EN10MB, sizeof(struct ether_header));
1.104     matt      939: #ifdef MBUFTRACE
1.108     itojun    940:        strlcpy(ec->ec_tx_mowner.mo_name, ifp->if_xname,
                    941:            sizeof(ec->ec_tx_mowner.mo_name));
                    942:        strlcpy(ec->ec_tx_mowner.mo_descr, "tx",
                    943:            sizeof(ec->ec_tx_mowner.mo_descr));
                    944:        strlcpy(ec->ec_rx_mowner.mo_name, ifp->if_xname,
                    945:            sizeof(ec->ec_rx_mowner.mo_name));
                    946:        strlcpy(ec->ec_rx_mowner.mo_descr, "rx",
                    947:            sizeof(ec->ec_rx_mowner.mo_descr));
1.104     matt      948:        MOWNER_ATTACH(&ec->ec_tx_mowner);
                    949:        MOWNER_ATTACH(&ec->ec_rx_mowner);
                    950:        ifp->if_mowner = &ec->ec_tx_mowner;
                    951: #endif
1.52      thorpej   952: }
                    953:
                    954: void
1.58      matt      955: ether_ifdetach(struct ifnet *ifp)
1.52      thorpej   956: {
1.63      thorpej   957:        struct ethercom *ec = (void *) ifp;
                    958:        struct ether_multi *enm;
                    959:        int s;
1.69      thorpej   960:
1.190     christos  961:        /*
                    962:         * Prevent further calls to ioctl (for example turning off
                    963:         * promiscuous mode from the bridge code), which eventually can
                    964:         * call if_init() which can cause panics because the interface
                    965:         * is in the process of being detached. Return device not configured
                    966:         * instead.
                    967:         */
                    968:        ifp->if_ioctl = (int (*)(struct ifnet *, u_long, void *))enxio;
                    969:
1.115     dyoung    970: #if NBRIDGE > 0
                    971:        if (ifp->if_bridge)
                    972:                bridge_ifdetach(ifp);
                    973: #endif
                    974:
1.177     joerg     975:        bpf_detach(ifp);
1.64      thorpej   976:
                    977: #if NVLAN > 0
                    978:        if (ec->ec_nvlans)
                    979:                vlan_ifdetach(ifp);
                    980: #endif
1.63      thorpej   981:
1.79      thorpej   982:        s = splnet();
1.63      thorpej   983:        while ((enm = LIST_FIRST(&ec->ec_multiaddrs)) != NULL) {
                    984:                LIST_REMOVE(enm, enm_list);
1.100     jdolecek  985:                free(enm, M_IFMADDR);
1.63      thorpej   986:                ec->ec_multicnt--;
                    987:        }
                    988:        splx(s);
1.52      thorpej   989:
1.189     chs       990:        ifp->if_mowner = NULL;
1.104     matt      991:        MOWNER_DETACH(&ec->ec_rx_mowner);
                    992:        MOWNER_DETACH(&ec->ec_tx_mowner);
1.53      thorpej   993: }
                    994:
1.56      thorpej   995: #if 0
                    996: /*
                    997:  * This is for reference.  We have a table-driven version
                    998:  * of the little-endian crc32 generator, which is faster
                    999:  * than the double-loop.
                   1000:  */
1.162     matt     1001: uint32_t
                   1002: ether_crc32_le(const uint8_t *buf, size_t len)
1.53      thorpej  1003: {
1.162     matt     1004:        uint32_t c, crc, carry;
1.53      thorpej  1005:        size_t i, j;
                   1006:
                   1007:        crc = 0xffffffffU;      /* initial value */
                   1008:
                   1009:        for (i = 0; i < len; i++) {
                   1010:                c = buf[i];
                   1011:                for (j = 0; j < 8; j++) {
                   1012:                        carry = ((crc & 0x01) ? 1 : 0) ^ (c & 0x01);
                   1013:                        crc >>= 1;
                   1014:                        c >>= 1;
                   1015:                        if (carry)
1.56      thorpej  1016:                                crc = (crc ^ ETHER_CRC_POLY_LE);
1.53      thorpej  1017:                }
                   1018:        }
                   1019:
                   1020:        return (crc);
                   1021: }
1.56      thorpej  1022: #else
1.162     matt     1023: uint32_t
                   1024: ether_crc32_le(const uint8_t *buf, size_t len)
1.56      thorpej  1025: {
1.162     matt     1026:        static const uint32_t crctab[] = {
1.56      thorpej  1027:                0x00000000, 0x1db71064, 0x3b6e20c8, 0x26d930ac,
                   1028:                0x76dc4190, 0x6b6b51f4, 0x4db26158, 0x5005713c,
                   1029:                0xedb88320, 0xf00f9344, 0xd6d6a3e8, 0xcb61b38c,
                   1030:                0x9b64c2b0, 0x86d3d2d4, 0xa00ae278, 0xbdbdf21c
                   1031:        };
1.162     matt     1032:        uint32_t crc;
1.98      thorpej  1033:        size_t i;
1.56      thorpej  1034:
                   1035:        crc = 0xffffffffU;      /* initial value */
                   1036:
                   1037:        for (i = 0; i < len; i++) {
                   1038:                crc ^= buf[i];
                   1039:                crc = (crc >> 4) ^ crctab[crc & 0xf];
                   1040:                crc = (crc >> 4) ^ crctab[crc & 0xf];
                   1041:        }
                   1042:
                   1043:        return (crc);
                   1044: }
                   1045: #endif
1.53      thorpej  1046:
1.162     matt     1047: uint32_t
                   1048: ether_crc32_be(const uint8_t *buf, size_t len)
1.53      thorpej  1049: {
1.162     matt     1050:        uint32_t c, crc, carry;
1.53      thorpej  1051:        size_t i, j;
                   1052:
                   1053:        crc = 0xffffffffU;      /* initial value */
                   1054:
                   1055:        for (i = 0; i < len; i++) {
                   1056:                c = buf[i];
                   1057:                for (j = 0; j < 8; j++) {
                   1058:                        carry = ((crc & 0x80000000U) ? 1 : 0) ^ (c & 0x01);
                   1059:                        crc <<= 1;
                   1060:                        c >>= 1;
                   1061:                        if (carry)
                   1062:                                crc = (crc ^ ETHER_CRC_POLY_BE) | carry;
                   1063:                }
                   1064:        }
                   1065:
                   1066:        return (crc);
1.8       mycroft  1067: }
                   1068:
1.48      is       1069: #ifdef INET
1.118     yamt     1070: const uint8_t ether_ipmulticast_min[ETHER_ADDR_LEN] =
                   1071:     { 0x01, 0x00, 0x5e, 0x00, 0x00, 0x00 };
                   1072: const uint8_t ether_ipmulticast_max[ETHER_ADDR_LEN] =
                   1073:     { 0x01, 0x00, 0x5e, 0x7f, 0xff, 0xff };
1.48      is       1074: #endif
1.44      itojun   1075: #ifdef INET6
1.118     yamt     1076: const uint8_t ether_ip6multicast_min[ETHER_ADDR_LEN] =
                   1077:     { 0x33, 0x33, 0x00, 0x00, 0x00, 0x00 };
                   1078: const uint8_t ether_ip6multicast_max[ETHER_ADDR_LEN] =
                   1079:     { 0x33, 0x33, 0xff, 0xff, 0xff, 0xff };
1.44      itojun   1080: #endif
1.60      enami    1081:
1.3       hpeyerl  1082: /*
1.138     rpaulo   1083:  * ether_aton implementation, not using a static buffer.
                   1084:  */
                   1085: int
1.180     christos 1086: ether_aton_r(u_char *dest, size_t len, const char *str)
1.138     rpaulo   1087: {
1.180     christos 1088:         const u_char *cp = (const void *)str;
                   1089:        u_char *ep;
                   1090:
1.185     tsutsui  1091: #define atox(c)        (((c) <= '9') ? ((c) - '0') : ((toupper(c) - 'A') + 10))
1.180     christos 1092:
                   1093:        if (len < ETHER_ADDR_LEN)
                   1094:                return ENOSPC;
                   1095:
                   1096:        ep = dest + ETHER_ADDR_LEN;
                   1097:
                   1098:        while (*cp) {
                   1099:                 if (!isxdigit(*cp))
                   1100:                         return EINVAL;
                   1101:                *dest = atox(*cp);
                   1102:                cp++;
                   1103:                 if (isxdigit(*cp)) {
                   1104:                         *dest = (*dest << 4) | atox(*cp);
                   1105:                        dest++;
                   1106:                        cp++;
                   1107:                 } else
1.181     christos 1108:                        dest++;
1.180     christos 1109:                if (dest == ep)
                   1110:                        return *cp == '\0' ? 0 : ENAMETOOLONG;
                   1111:                switch (*cp) {
                   1112:                case ':':
                   1113:                case '-':
                   1114:                case '.':
1.179     jakllsch 1115:                        cp++;
1.180     christos 1116:                        break;
1.179     jakllsch 1117:                }
1.180     christos 1118:         }
                   1119:        return ENOBUFS;
1.138     rpaulo   1120: }
                   1121:
                   1122: /*
1.60      enami    1123:  * Convert a sockaddr into an Ethernet address or range of Ethernet
                   1124:  * addresses.
1.3       hpeyerl  1125:  */
                   1126: int
1.162     matt     1127: ether_multiaddr(const struct sockaddr *sa, uint8_t addrlo[ETHER_ADDR_LEN],
                   1128:     uint8_t addrhi[ETHER_ADDR_LEN])
1.3       hpeyerl  1129: {
1.24      christos 1130: #ifdef INET
1.155     dyoung   1131:        const struct sockaddr_in *sin;
1.24      christos 1132: #endif /* INET */
1.44      itojun   1133: #ifdef INET6
1.155     dyoung   1134:        const struct sockaddr_in6 *sin6;
1.44      itojun   1135: #endif /* INET6 */
1.3       hpeyerl  1136:
1.60      enami    1137:        switch (sa->sa_family) {
1.3       hpeyerl  1138:
                   1139:        case AF_UNSPEC:
1.146     dyoung   1140:                memcpy(addrlo, sa->sa_data, ETHER_ADDR_LEN);
                   1141:                memcpy(addrhi, addrlo, ETHER_ADDR_LEN);
1.3       hpeyerl  1142:                break;
                   1143:
                   1144: #ifdef INET
                   1145:        case AF_INET:
1.155     dyoung   1146:                sin = satocsin(sa);
1.3       hpeyerl  1147:                if (sin->sin_addr.s_addr == INADDR_ANY) {
                   1148:                        /*
1.60      enami    1149:                         * An IP address of INADDR_ANY means listen to
                   1150:                         * or stop listening to all of the Ethernet
                   1151:                         * multicast addresses used for IP.
1.3       hpeyerl  1152:                         * (This is for the sake of IP multicast routers.)
                   1153:                         */
1.146     dyoung   1154:                        memcpy(addrlo, ether_ipmulticast_min, ETHER_ADDR_LEN);
                   1155:                        memcpy(addrhi, ether_ipmulticast_max, ETHER_ADDR_LEN);
1.3       hpeyerl  1156:                }
                   1157:                else {
                   1158:                        ETHER_MAP_IP_MULTICAST(&sin->sin_addr, addrlo);
1.146     dyoung   1159:                        memcpy(addrhi, addrlo, ETHER_ADDR_LEN);
1.3       hpeyerl  1160:                }
                   1161:                break;
                   1162: #endif
1.44      itojun   1163: #ifdef INET6
                   1164:        case AF_INET6:
1.155     dyoung   1165:                sin6 = satocsin6(sa);
1.47      itojun   1166:                if (IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) {
1.44      itojun   1167:                        /*
1.60      enami    1168:                         * An IP6 address of 0 means listen to or stop
                   1169:                         * listening to all of the Ethernet multicast
                   1170:                         * address used for IP6.
1.44      itojun   1171:                         * (This is used for multicast routers.)
                   1172:                         */
1.146     dyoung   1173:                        memcpy(addrlo, ether_ip6multicast_min, ETHER_ADDR_LEN);
                   1174:                        memcpy(addrhi, ether_ip6multicast_max, ETHER_ADDR_LEN);
1.44      itojun   1175:                } else {
                   1176:                        ETHER_MAP_IPV6_MULTICAST(&sin6->sin6_addr, addrlo);
1.146     dyoung   1177:                        memcpy(addrhi, addrlo, ETHER_ADDR_LEN);
1.44      itojun   1178:                }
                   1179:                break;
                   1180: #endif
1.3       hpeyerl  1181:
                   1182:        default:
1.146     dyoung   1183:                return EAFNOSUPPORT;
1.60      enami    1184:        }
1.146     dyoung   1185:        return 0;
1.60      enami    1186: }
                   1187:
                   1188: /*
                   1189:  * Add an Ethernet multicast address or range of addresses to the list for a
                   1190:  * given interface.
                   1191:  */
                   1192: int
1.155     dyoung   1193: ether_addmulti(const struct sockaddr *sa, struct ethercom *ec)
1.60      enami    1194: {
                   1195:        struct ether_multi *enm;
                   1196:        u_char addrlo[ETHER_ADDR_LEN];
                   1197:        u_char addrhi[ETHER_ADDR_LEN];
1.79      thorpej  1198:        int s = splnet(), error;
1.60      enami    1199:
1.155     dyoung   1200:        error = ether_multiaddr(sa, addrlo, addrhi);
1.60      enami    1201:        if (error != 0) {
1.3       hpeyerl  1202:                splx(s);
1.146     dyoung   1203:                return error;
1.3       hpeyerl  1204:        }
                   1205:
                   1206:        /*
                   1207:         * Verify that we have valid Ethernet multicast addresses.
                   1208:         */
1.186     yamt     1209:        if (!ETHER_IS_MULTICAST(addrlo) || !ETHER_IS_MULTICAST(addrhi)) {
1.3       hpeyerl  1210:                splx(s);
1.146     dyoung   1211:                return EINVAL;
1.3       hpeyerl  1212:        }
                   1213:        /*
                   1214:         * See if the address range is already in the list.
                   1215:         */
1.22      is       1216:        ETHER_LOOKUP_MULTI(addrlo, addrhi, ec, enm);
1.3       hpeyerl  1217:        if (enm != NULL) {
                   1218:                /*
                   1219:                 * Found it; just increment the reference count.
                   1220:                 */
                   1221:                ++enm->enm_refcount;
                   1222:                splx(s);
1.146     dyoung   1223:                return 0;
1.3       hpeyerl  1224:        }
                   1225:        /*
                   1226:         * New address or range; malloc a new multicast record
                   1227:         * and link it into the interface's multicast list.
                   1228:         */
                   1229:        enm = (struct ether_multi *)malloc(sizeof(*enm), M_IFMADDR, M_NOWAIT);
                   1230:        if (enm == NULL) {
                   1231:                splx(s);
1.146     dyoung   1232:                return ENOBUFS;
1.3       hpeyerl  1233:        }
1.146     dyoung   1234:        memcpy(enm->enm_addrlo, addrlo, 6);
                   1235:        memcpy(enm->enm_addrhi, addrhi, 6);
1.3       hpeyerl  1236:        enm->enm_refcount = 1;
1.22      is       1237:        LIST_INSERT_HEAD(&ec->ec_multiaddrs, enm, enm_list);
                   1238:        ec->ec_multicnt++;
1.3       hpeyerl  1239:        splx(s);
                   1240:        /*
                   1241:         * Return ENETRESET to inform the driver that the list has changed
                   1242:         * and its reception filter should be adjusted accordingly.
                   1243:         */
1.146     dyoung   1244:        return ENETRESET;
1.3       hpeyerl  1245: }
                   1246:
                   1247: /*
                   1248:  * Delete a multicast address record.
                   1249:  */
                   1250: int
1.155     dyoung   1251: ether_delmulti(const struct sockaddr *sa, struct ethercom *ec)
1.3       hpeyerl  1252: {
1.29      mrg      1253:        struct ether_multi *enm;
1.60      enami    1254:        u_char addrlo[ETHER_ADDR_LEN];
                   1255:        u_char addrhi[ETHER_ADDR_LEN];
1.79      thorpej  1256:        int s = splnet(), error;
1.3       hpeyerl  1257:
1.155     dyoung   1258:        error = ether_multiaddr(sa, addrlo, addrhi);
1.60      enami    1259:        if (error != 0) {
1.3       hpeyerl  1260:                splx(s);
1.60      enami    1261:                return (error);
1.3       hpeyerl  1262:        }
                   1263:
                   1264:        /*
1.66      thorpej  1265:         * Look ur the address in our list.
1.3       hpeyerl  1266:         */
1.22      is       1267:        ETHER_LOOKUP_MULTI(addrlo, addrhi, ec, enm);
1.3       hpeyerl  1268:        if (enm == NULL) {
                   1269:                splx(s);
                   1270:                return (ENXIO);
                   1271:        }
                   1272:        if (--enm->enm_refcount != 0) {
                   1273:                /*
                   1274:                 * Still some claims to this record.
                   1275:                 */
                   1276:                splx(s);
                   1277:                return (0);
                   1278:        }
                   1279:        /*
                   1280:         * No remaining claims to this record; unlink and free it.
                   1281:         */
1.13      mycroft  1282:        LIST_REMOVE(enm, enm_list);
1.3       hpeyerl  1283:        free(enm, M_IFMADDR);
1.22      is       1284:        ec->ec_multicnt--;
1.3       hpeyerl  1285:        splx(s);
                   1286:        /*
                   1287:         * Return ENETRESET to inform the driver that the list has changed
                   1288:         * and its reception filter should be adjusted accordingly.
                   1289:         */
                   1290:        return (ENETRESET);
1.66      thorpej  1291: }
                   1292:
1.170     dyoung   1293: void
                   1294: ether_set_ifflags_cb(struct ethercom *ec, ether_cb_t cb)
                   1295: {
                   1296:        ec->ec_ifflags_cb = cb;
                   1297: }
                   1298:
1.66      thorpej  1299: /*
                   1300:  * Common ioctls for Ethernet interfaces.  Note, we must be
                   1301:  * called at splnet().
                   1302:  */
                   1303: int
1.147     christos 1304: ether_ioctl(struct ifnet *ifp, u_long cmd, void *data)
1.66      thorpej  1305: {
                   1306:        struct ethercom *ec = (void *) ifp;
1.193     msaitoh  1307:        struct eccapreq *eccr;
1.66      thorpej  1308:        struct ifreq *ifr = (struct ifreq *)data;
1.170     dyoung   1309:        struct if_laddrreq *iflr = data;
                   1310:        const struct sockaddr_dl *sdl;
                   1311:        static const uint8_t zero[ETHER_ADDR_LEN];
1.169     dyoung   1312:        int error;
1.66      thorpej  1313:
                   1314:        switch (cmd) {
1.170     dyoung   1315:        case SIOCINITIFADDR:
1.191     matt     1316:            {
                   1317:                struct ifaddr *ifa = (struct ifaddr *)data;
                   1318:                if (ifa->ifa_addr->sa_family != AF_LINK
                   1319:                    && (ifp->if_flags & (IFF_UP|IFF_RUNNING)) !=
                   1320:                       (IFF_UP|IFF_RUNNING)) {
1.170     dyoung   1321:                        ifp->if_flags |= IFF_UP;
                   1322:                        if ((error = (*ifp->if_init)(ifp)) != 0)
                   1323:                                return error;
                   1324:                }
1.66      thorpej  1325: #ifdef INET
1.191     matt     1326:                if (ifa->ifa_addr->sa_family == AF_INET)
                   1327:                        arp_ifinit(ifp, ifa);
1.66      thorpej  1328: #endif /* INET */
1.169     dyoung   1329:                return 0;
1.191     matt     1330:            }
1.66      thorpej  1331:
                   1332:        case SIOCSIFMTU:
1.82      thorpej  1333:            {
                   1334:                int maxmtu;
                   1335:
                   1336:                if (ec->ec_capabilities & ETHERCAP_JUMBO_MTU)
                   1337:                        maxmtu = ETHERMTU_JUMBO;
                   1338:                else
                   1339:                        maxmtu = ETHERMTU;
                   1340:
                   1341:                if (ifr->ifr_mtu < ETHERMIN || ifr->ifr_mtu > maxmtu)
1.169     dyoung   1342:                        return EINVAL;
                   1343:                else if ((error = ifioctl_common(ifp, cmd, data)) != ENETRESET)
                   1344:                        return error;
                   1345:                else if (ifp->if_flags & IFF_UP) {
1.88      thorpej  1346:                        /* Make sure the device notices the MTU change. */
1.169     dyoung   1347:                        return (*ifp->if_init)(ifp);
                   1348:                } else
                   1349:                        return 0;
1.82      thorpej  1350:            }
1.66      thorpej  1351:
                   1352:        case SIOCSIFFLAGS:
1.170     dyoung   1353:                if ((error = ifioctl_common(ifp, cmd, data)) != 0)
                   1354:                        return error;
1.169     dyoung   1355:                switch (ifp->if_flags & (IFF_UP|IFF_RUNNING)) {
                   1356:                case IFF_RUNNING:
1.66      thorpej  1357:                        /*
                   1358:                         * If interface is marked down and it is running,
                   1359:                         * then stop and disable it.
                   1360:                         */
                   1361:                        (*ifp->if_stop)(ifp, 1);
1.169     dyoung   1362:                        break;
                   1363:                case IFF_UP:
1.66      thorpej  1364:                        /*
                   1365:                         * If interface is marked up and it is stopped, then
                   1366:                         * start it.
                   1367:                         */
1.169     dyoung   1368:                        return (*ifp->if_init)(ifp);
                   1369:                case IFF_UP|IFF_RUNNING:
1.170     dyoung   1370:                        error = 0;
                   1371:                        if (ec->ec_ifflags_cb == NULL ||
                   1372:                            (error = (*ec->ec_ifflags_cb)(ec)) == ENETRESET) {
                   1373:                                /*
                   1374:                                 * Reset the interface to pick up
                   1375:                                 * changes in any other flags that
                   1376:                                 * affect the hardware state.
                   1377:                                 */
                   1378:                                return (*ifp->if_init)(ifp);
                   1379:                        } else
                   1380:                                return error;
1.169     dyoung   1381:                case 0:
                   1382:                        break;
1.66      thorpej  1383:                }
1.169     dyoung   1384:                return 0;
1.193     msaitoh  1385:        case SIOCGETHERCAP:
                   1386:                eccr = (struct eccapreq *)data;
                   1387:                eccr->eccr_capabilities = ec->ec_capabilities;
                   1388:                eccr->eccr_capenable = ec->ec_capenable;
                   1389:                return 0;
1.66      thorpej  1390:        case SIOCADDMULTI:
1.169     dyoung   1391:                return ether_addmulti(ifreq_getaddr(cmd, ifr), ec);
1.66      thorpej  1392:        case SIOCDELMULTI:
1.169     dyoung   1393:                return ether_delmulti(ifreq_getaddr(cmd, ifr), ec);
1.160     dyoung   1394:        case SIOCSIFMEDIA:
                   1395:        case SIOCGIFMEDIA:
                   1396:                if (ec->ec_mii == NULL)
1.169     dyoung   1397:                        return ENOTTY;
                   1398:                return ifmedia_ioctl(ifp, ifr, &ec->ec_mii->mii_media, cmd);
1.170     dyoung   1399:        case SIOCALIFADDR:
                   1400:                sdl = satocsdl(sstocsa(&iflr->addr));
                   1401:                if (sdl->sdl_family != AF_LINK)
                   1402:                        ;
                   1403:                else if (ETHER_IS_MULTICAST(CLLADDR(sdl)))
                   1404:                        return EINVAL;
                   1405:                else if (memcmp(zero, CLLADDR(sdl), sizeof(zero)) == 0)
                   1406:                        return EINVAL;
                   1407:                /*FALLTHROUGH*/
                   1408:        default:
1.161     dyoung   1409:                return ifioctl_common(ifp, cmd, data);
1.66      thorpej  1410:        }
1.169     dyoung   1411:        return 0;
1.3       hpeyerl  1412: }
1.200     joerg    1413:
1.215     christos 1414: /*
                   1415:  * Enable/disable passing VLAN packets if the parent interface supports it.
                   1416:  * Return:
                   1417:  *      0: Ok
                   1418:  *     -1: Parent interface does not support vlans
                   1419:  *     >0: Error
                   1420:  */
                   1421: int
                   1422: ether_enable_vlan_mtu(struct ifnet *ifp)
                   1423: {
                   1424:        int error;
                   1425:        struct ethercom *ec = (void *)ifp;
                   1426:
                   1427:        /* Already have VLAN's do nothing. */
                   1428:        if (ec->ec_nvlans != 0)
                   1429:                return 0;
                   1430:
                   1431:        /* Parent does not support VLAN's */
                   1432:        if ((ec->ec_capabilities & ETHERCAP_VLAN_MTU) == 0)
                   1433:                return -1;
                   1434:
                   1435:        /*
                   1436:         * Parent supports the VLAN_MTU capability,
                   1437:         * i.e. can Tx/Rx larger than ETHER_MAX_LEN frames;
                   1438:         * enable it.
                   1439:         */
                   1440:        ec->ec_capenable |= ETHERCAP_VLAN_MTU;
                   1441:
                   1442:        /* Interface is down, defer for later */
                   1443:        if ((ifp->if_flags & IFF_UP) == 0)
                   1444:                return 0;
                   1445:
                   1446:        if ((error = if_flags_set(ifp, ifp->if_flags)) == 0)
                   1447:                return 0;
                   1448:
                   1449:        ec->ec_capenable &= ~ETHERCAP_VLAN_MTU;
                   1450:        return error;
                   1451: }
                   1452:
                   1453: int
                   1454: ether_disable_vlan_mtu(struct ifnet *ifp)
                   1455: {
                   1456:        int error;
                   1457:        struct ethercom *ec = (void *)ifp;
                   1458:
                   1459:        /* We still have VLAN's, defer for later */
                   1460:        if (ec->ec_nvlans != 0)
                   1461:                return 0;
                   1462:
                   1463:        /* Parent does not support VLAB's, nothing to do. */
                   1464:        if ((ec->ec_capenable & ETHERCAP_VLAN_MTU) == 0)
                   1465:                return -1;
                   1466:
                   1467:        /*
                   1468:         * Disable Tx/Rx of VLAN-sized frames.
                   1469:         */
                   1470:        ec->ec_capenable &= ~ETHERCAP_VLAN_MTU;
                   1471:
                   1472:        /* Interface is down, defer for later */
                   1473:        if ((ifp->if_flags & IFF_UP) == 0)
                   1474:                return 0;
                   1475:
                   1476:        if ((error = if_flags_set(ifp, ifp->if_flags)) == 0)
                   1477:                return 0;
                   1478:
                   1479:        ec->ec_capenable |= ETHERCAP_VLAN_MTU;
                   1480:        return error;
                   1481: }
                   1482:
1.200     joerg    1483: static int
                   1484: ether_multicast_sysctl(SYSCTLFN_ARGS)
                   1485: {
                   1486:        struct ether_multi *enm;
                   1487:        struct ether_multi_sysctl addr;
                   1488:        struct ifnet *ifp;
                   1489:        struct ethercom *ec;
1.223     ozaki-r  1490:        int error = 0;
1.200     joerg    1491:        size_t written;
1.223     ozaki-r  1492:        struct psref psref;
                   1493:        int bound;
1.200     joerg    1494:
                   1495:        if (namelen != 1)
                   1496:                return EINVAL;
                   1497:
1.223     ozaki-r  1498:        bound = curlwp_bind();
                   1499:        ifp = if_get_byindex(name[0], &psref);
                   1500:        if (ifp == NULL) {
                   1501:                error = ENODEV;
                   1502:                goto out;
                   1503:        }
1.200     joerg    1504:        if (ifp->if_type != IFT_ETHER) {
1.223     ozaki-r  1505:                if_put(ifp, &psref);
1.200     joerg    1506:                *oldlenp = 0;
1.223     ozaki-r  1507:                goto out;
1.200     joerg    1508:        }
                   1509:        ec = (struct ethercom *)ifp;
                   1510:
                   1511:        if (oldp == NULL) {
1.223     ozaki-r  1512:                if_put(ifp, &psref);
1.200     joerg    1513:                *oldlenp = ec->ec_multicnt * sizeof(addr);
1.223     ozaki-r  1514:                goto out;
1.200     joerg    1515:        }
                   1516:
                   1517:        memset(&addr, 0, sizeof(addr));
                   1518:        error = 0;
                   1519:        written = 0;
                   1520:
                   1521:        LIST_FOREACH(enm, &ec->ec_multiaddrs, enm_list) {
                   1522:                if (written + sizeof(addr) > *oldlenp)
                   1523:                        break;
                   1524:                addr.enm_refcount = enm->enm_refcount;
                   1525:                memcpy(addr.enm_addrlo, enm->enm_addrlo, ETHER_ADDR_LEN);
                   1526:                memcpy(addr.enm_addrhi, enm->enm_addrhi, ETHER_ADDR_LEN);
                   1527:                error = sysctl_copyout(l, &addr, oldp, sizeof(addr));
                   1528:                if (error)
                   1529:                        break;
                   1530:                written += sizeof(addr);
                   1531:                oldp = (char *)oldp + sizeof(addr);
                   1532:        }
1.223     ozaki-r  1533:        if_put(ifp, &psref);
1.200     joerg    1534:
                   1535:        *oldlenp = written;
1.223     ozaki-r  1536: out:
                   1537:        curlwp_bindx(bound);
1.200     joerg    1538:        return error;
                   1539: }
                   1540:
                   1541: SYSCTL_SETUP(sysctl_net_ether_setup, "sysctl net.ether subtree setup")
                   1542: {
                   1543:        const struct sysctlnode *rnode = NULL;
                   1544:
                   1545:        sysctl_createv(clog, 0, NULL, &rnode,
                   1546:                       CTLFLAG_PERMANENT,
                   1547:                       CTLTYPE_NODE, "ether",
                   1548:                       SYSCTL_DESCR("Ethernet-specific information"),
                   1549:                       NULL, 0, NULL, 0,
                   1550:                       CTL_NET, CTL_CREATE, CTL_EOL);
                   1551:
                   1552:        sysctl_createv(clog, 0, &rnode, NULL,
                   1553:                       CTLFLAG_PERMANENT,
                   1554:                       CTLTYPE_NODE, "multicast",
                   1555:                       SYSCTL_DESCR("multicast addresses"),
                   1556:                       ether_multicast_sysctl, 0, NULL, 0,
                   1557:                       CTL_CREATE, CTL_EOL);
                   1558: }
1.203     ozaki-r  1559:
                   1560: void
                   1561: etherinit(void)
                   1562: {
                   1563:        mutex_init(&bigpktpps_lock, MUTEX_DEFAULT, IPL_NET);
                   1564: }

CVSweb <webmaster@jp.NetBSD.org>