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

1.252   ! maxv        1: /*     $NetBSD: if_ethersubr.c,v 1.251 2018/01/15 07:59:48 maxv 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.252   ! maxv       64: __KERNEL_RCSID(0, "$NetBSD: if_ethersubr.c,v 1.251 2018/01/15 07:59:48 maxv 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/mbuf.h>
1.211     ozaki-r    84: #include <sys/mutex.h>
1.4       mycroft    85: #include <sys/ioctl.h>
                     86: #include <sys/errno.h>
1.158     dyoung     87: #include <sys/device.h>
1.204     tls        88: #include <sys/rnd.h>
1.207     riastrad   89: #include <sys/rndsource.h>
1.216     ozaki-r    90: #include <sys/cpu.h>
1.231     ozaki-r    91: #include <sys/kmem.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.250     maxv      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.104     matt      207: #ifdef MBUFTRACE
1.116     jonathan  208:        m_claimm(m, ifp->if_mowner);
1.104     matt      209: #endif
1.133     liamjfoy  210:
                    211: #if NCARP > 0
                    212:        if (ifp->if_type == IFT_CARP) {
                    213:                struct ifaddr *ifa;
1.227     ozaki-r   214:                int s = pserialize_read_enter();
1.133     liamjfoy  215:
                    216:                /* loop back if this is going to the carp interface */
                    217:                if (dst != NULL && ifp0->if_link_state == LINK_STATE_UP &&
1.227     ozaki-r   218:                    (ifa = ifa_ifwithaddr(dst)) != NULL) {
                    219:                        if (ifa->ifa_ifp == ifp0) {
                    220:                                pserialize_read_exit(s);
                    221:                                return looutput(ifp0, m, dst, rt);
                    222:                        }
                    223:                }
                    224:                pserialize_read_exit(s);
1.133     liamjfoy  225:
                    226:                ifp = ifp->if_carpdev;
                    227:                /* ac = (struct arpcom *)ifp; */
                    228:
1.247     msaitoh   229:                if ((ifp0->if_flags & (IFF_UP | IFF_RUNNING)) !=
                    230:                    (IFF_UP | IFF_RUNNING))
1.133     liamjfoy  231:                        senderr(ENETDOWN);
                    232:        }
                    233: #endif /* NCARP > 0 */
                    234:
1.247     msaitoh   235:        if ((ifp->if_flags & (IFF_UP | IFF_RUNNING)) != (IFF_UP | IFF_RUNNING))
1.8       mycroft   236:                senderr(ENETDOWN);
1.72      thorpej   237:
1.1       cgd       238:        switch (dst->sa_family) {
                    239:
                    240: #ifdef INET
                    241:        case AF_INET:
1.22      is        242:                if (m->m_flags & M_BCAST)
1.174     isaki     243:                        (void)memcpy(edst, etherbroadcastaddr, sizeof(edst));
1.145     dyoung    244:                else if (m->m_flags & M_MCAST)
                    245:                        ETHER_MAP_IP_MULTICAST(&satocsin(dst)->sin_addr, edst);
1.217     christos  246:                else if ((error = arpresolve(ifp, rt, m, dst, edst,
1.224     knakahar  247:                    sizeof(edst))) != 0) {
1.214     roy       248:                        return error == EWOULDBLOCK ? 0 : error;
1.224     knakahar  249:                }
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 */
1.236     maxv      265:                                m_freem(m);
1.173     christos  266:                                return 0;
                    267:                        }
1.146     dyoung    268:                        memcpy(edst, tha, sizeof(edst));
1.131     mrg       269:                }
1.120     perry     270:
1.22      is        271:                ah->ar_hrd = htons(ARPHRD_ETHER);
                    272:
1.107     itojun    273:                switch (ntohs(ah->ar_op)) {
1.22      is        274:                case ARPOP_REVREQUEST:
                    275:                case ARPOP_REVREPLY:
                    276:                        etype = htons(ETHERTYPE_REVARP);
                    277:                        break;
                    278:
                    279:                case ARPOP_REQUEST:
                    280:                case ARPOP_REPLY:
                    281:                default:
                    282:                        etype = htons(ETHERTYPE_ARP);
                    283:                }
                    284:
                    285:                break;
1.1       cgd       286: #endif
1.44      itojun    287: #ifdef INET6
                    288:        case AF_INET6:
1.238     ozaki-r   289:                if (m->m_flags & M_BCAST)
                    290:                        (void)memcpy(edst, etherbroadcastaddr, sizeof(edst));
                    291:                else if (m->m_flags & M_MCAST) {
                    292:                        ETHER_MAP_IPV6_MULTICAST(&satocsin6(dst)->sin6_addr,
                    293:                            edst);
                    294:                } else {
                    295:                        error = nd6_resolve(ifp, rt, m, dst, edst,
                    296:                            sizeof(edst));
                    297:                        if (error != 0)
                    298:                                return error == EWOULDBLOCK ? 0 : error;
1.51      itojun    299:                }
1.44      itojun    300:                etype = htons(ETHERTYPE_IPV6);
                    301:                break;
                    302: #endif
1.23      christos  303: #ifdef NETATALK
1.250     maxv      304:        case AF_APPLETALK: {
1.227     ozaki-r   305:                struct ifaddr *ifa;
                    306:                int s;
                    307:
1.224     knakahar  308:                KERNEL_LOCK(1, NULL);
1.226     rjs       309:                if (!aarpresolve(ifp, m, (const struct sockaddr_at *)dst, edst)) {
1.23      christos  310: #ifdef NETATALKDEBUG
                    311:                        printf("aarpresolv failed\n");
                    312: #endif /* NETATALKDEBUG */
1.224     knakahar  313:                        KERNEL_UNLOCK_ONE(NULL);
1.23      christos  314:                        return (0);
                    315:                }
                    316:                /*
                    317:                 * ifaddr is the first thing in at_ifaddr
                    318:                 */
1.227     ozaki-r   319:                s = pserialize_read_enter();
                    320:                ifa = at_ifawithnet((const struct sockaddr_at *)dst, ifp);
                    321:                if (ifa == NULL) {
                    322:                        pserialize_read_exit(s);
                    323:                        KERNEL_UNLOCK_ONE(NULL);
                    324:                        goto bad;
1.224     knakahar  325:                }
1.227     ozaki-r   326:                aa = (struct at_ifaddr *)ifa;
1.120     perry     327:
1.23      christos  328:                /*
                    329:                 * In the phase 2 case, we need to prepend an mbuf for the
                    330:                 * llc header.  Since we must preserve the value of m,
                    331:                 * which is passed to us by value, we m_copy() the first
                    332:                 * mbuf, and use it for our llc header.
                    333:                 */
                    334:                if (aa->aa_flags & AFA_PHASE2) {
                    335:                        struct llc llc;
                    336:
1.43      bouyer    337:                        M_PREPEND(m, sizeof(struct llc), M_DONTWAIT);
1.23      christos  338:                        llc.llc_dsap = llc.llc_ssap = LLC_SNAP_LSAP;
                    339:                        llc.llc_control = LLC_UI;
1.146     dyoung    340:                        memcpy(llc.llc_snap_org_code, at_org_code,
1.23      christos  341:                            sizeof(llc.llc_snap_org_code));
1.38      kim       342:                        llc.llc_snap_ether_type = htons(ETHERTYPE_ATALK);
1.147     christos  343:                        memcpy(mtod(m, void *), &llc, sizeof(struct llc));
1.23      christos  344:                } else {
1.38      kim       345:                        etype = htons(ETHERTYPE_ATALK);
1.23      christos  346:                }
1.227     ozaki-r   347:                pserialize_read_exit(s);
1.224     knakahar  348:                KERNEL_UNLOCK_ONE(NULL);
1.23      christos  349:                break;
1.250     maxv      350:        }
1.23      christos  351: #endif /* NETATALK */
1.31      thorpej   352:        case pseudo_AF_HDRCMPLT:
                    353:                hdrcmplt = 1;
1.146     dyoung    354:                memcpy(esrc,
                    355:                    ((const struct ether_header *)dst->sa_data)->ether_shost,
                    356:                    sizeof(esrc));
1.31      thorpej   357:                /* FALLTHROUGH */
                    358:
1.1       cgd       359:        case AF_UNSPEC:
1.250     maxv      360:                memcpy(edst,
1.146     dyoung    361:                    ((const struct ether_header *)dst->sa_data)->ether_dhost,
                    362:                    sizeof(edst));
1.8       mycroft   363:                /* AF_UNSPEC doesn't swap the byte order of the ether_type. */
1.145     dyoung    364:                etype = ((const struct ether_header *)dst->sa_data)->ether_type;
1.8       mycroft   365:                break;
1.1       cgd       366:
                    367:        default:
1.21      christos  368:                printf("%s: can't handle af%d\n", ifp->if_xname,
1.1       cgd       369:                        dst->sa_family);
1.8       mycroft   370:                senderr(EAFNOSUPPORT);
1.1       cgd       371:        }
                    372:
1.182     kefren    373: #ifdef MPLS
1.224     knakahar  374:        KERNEL_LOCK(1, NULL);
1.210     ozaki-r   375:        {
                    376:                struct m_tag *mtag;
                    377:                mtag = m_tag_find(m, PACKET_TAG_MPLS, NULL);
                    378:                if (mtag != NULL) {
                    379:                        /* Having the tag itself indicates it's MPLS */
1.184     dyoung    380:                        etype = htons(ETHERTYPE_MPLS);
1.210     ozaki-r   381:                        m_tag_delete(m, mtag);
                    382:                }
1.184     dyoung    383:        }
1.224     knakahar  384:        KERNEL_UNLOCK_ONE(NULL);
1.182     kefren    385: #endif
                    386:
1.1       cgd       387:        if (mcopy)
1.145     dyoung    388:                (void)looutput(ifp, mcopy, dst, rt);
1.16      mycroft   389:
1.50      matt      390:        /* If no ether type is set, this must be a 802.2 formatted packet.
                    391:         */
                    392:        if (etype == 0)
                    393:                etype = htons(m->m_pkthdr.len);
1.1       cgd       394:        /*
                    395:         * Add local net header.  If no space in first mbuf,
                    396:         * allocate another.
                    397:         */
                    398:        M_PREPEND(m, sizeof (struct ether_header), M_DONTWAIT);
1.8       mycroft   399:        if (m == 0)
                    400:                senderr(ENOBUFS);
1.1       cgd       401:        eh = mtod(m, struct ether_header *);
1.96      thorpej   402:        /* Note: etype is already in network byte order. */
1.143     cbiere    403:        (void)memcpy(&eh->ether_type, &etype, sizeof(eh->ether_type));
1.250     maxv      404:        memcpy(eh->ether_dhost, edst, sizeof(edst));
1.31      thorpej   405:        if (hdrcmplt)
1.146     dyoung    406:                memcpy(eh->ether_shost, esrc, sizeof(eh->ether_shost));
1.31      thorpej   407:        else
1.153     dyoung    408:                memcpy(eh->ether_shost, CLLADDR(ifp->if_sadl),
1.31      thorpej   409:                    sizeof(eh->ether_shost));
1.77      thorpej   410:
1.133     liamjfoy  411: #if NCARP > 0
                    412:        if (ifp0 != ifp && ifp0->if_type == IFT_CARP) {
1.153     dyoung    413:                memcpy(eh->ether_shost, CLLADDR(ifp0->if_sadl),
1.133     liamjfoy  414:                    sizeof(eh->ether_shost));
                    415:        }
                    416: #endif /* NCARP > 0 */
                    417:
1.195     rmind     418:        if ((error = pfil_run_hooks(ifp->if_pfil, &m, ifp, PFIL_OUT)) != 0)
1.77      thorpej   419:                return (error);
                    420:        if (m == NULL)
                    421:                return (0);
                    422:
1.78      thorpej   423: #if NBRIDGE > 0
                    424:        /*
                    425:         * Bridges require special output handling.
                    426:         */
                    427:        if (ifp->if_bridge)
                    428:                return (bridge_output(ifp, m, NULL, NULL));
                    429: #endif
                    430:
1.133     liamjfoy  431: #if NCARP > 0
                    432:        if (ifp != ifp0)
                    433:                ifp0->if_obytes += m->m_pkthdr.len + ETHER_HDR_LEN;
                    434: #endif /* NCARP > 0 */
                    435:
1.77      thorpej   436: #ifdef ALTQ
1.224     knakahar  437:        KERNEL_LOCK(1, NULL);
1.77      thorpej   438:        /*
                    439:         * If ALTQ is enabled on the parent interface, do
                    440:         * classification; the queueing discipline might not
                    441:         * require classification, but might require the
                    442:         * address family/header pointer in the pktattr.
                    443:         */
                    444:        if (ALTQ_IS_ENABLED(&ifp->if_snd))
1.220     knakahar  445:                altq_etherclassify(&ifp->if_snd, m);
1.224     knakahar  446:        KERNEL_UNLOCK_ONE(NULL);
1.77      thorpej   447: #endif
1.221     knakahar  448:        return ifq_enqueue(ifp, m);
1.1       cgd       449:
                    450: bad:
                    451:        if (m)
                    452:                m_freem(m);
                    453:        return (error);
                    454: }
1.76      thorpej   455:
                    456: #ifdef ALTQ
                    457: /*
                    458:  * This routine is a slight hack to allow a packet to be classified
                    459:  * if the Ethernet headers are present.  It will go away when ALTQ's
                    460:  * classification engine understands link headers.
                    461:  */
                    462: void
1.220     knakahar  463: altq_etherclassify(struct ifaltq *ifq, struct mbuf *m)
1.76      thorpej   464: {
                    465:        struct ether_header *eh;
1.162     matt      466:        uint16_t ether_type;
1.76      thorpej   467:        int hlen, af, hdrsize;
1.147     christos  468:        void *hdr;
1.76      thorpej   469:
                    470:        hlen = ETHER_HDR_LEN;
                    471:        eh = mtod(m, struct ether_header *);
                    472:
                    473:        ether_type = htons(eh->ether_type);
                    474:
                    475:        if (ether_type < ETHERMTU) {
                    476:                /* LLC/SNAP */
                    477:                struct llc *llc = (struct llc *)(eh + 1);
                    478:                hlen += 8;
                    479:
                    480:                if (m->m_len < hlen ||
                    481:                    llc->llc_dsap != LLC_SNAP_LSAP ||
                    482:                    llc->llc_ssap != LLC_SNAP_LSAP ||
                    483:                    llc->llc_control != LLC_UI) {
                    484:                        /* Not SNAP. */
                    485:                        goto bad;
                    486:                }
                    487:
                    488:                ether_type = htons(llc->llc_un.type_snap.ether_type);
                    489:        }
                    490:
                    491:        switch (ether_type) {
                    492:        case ETHERTYPE_IP:
                    493:                af = AF_INET;
                    494:                hdrsize = 20;           /* sizeof(struct ip) */
                    495:                break;
                    496:
                    497:        case ETHERTYPE_IPV6:
                    498:                af = AF_INET6;
                    499:                hdrsize = 40;           /* sizeof(struct ip6_hdr) */
                    500:                break;
                    501:
                    502:        default:
                    503:                af = AF_UNSPEC;
                    504:                hdrsize = 0;
                    505:                break;
                    506:        }
                    507:
1.95      itojun    508:        while (m->m_len <= hlen) {
                    509:                hlen -= m->m_len;
                    510:                m = m->m_next;
                    511:        }
1.76      thorpej   512:        if (m->m_len < (hlen + hdrsize)) {
                    513:                /*
1.95      itojun    514:                 * protocol header not in a single mbuf.
                    515:                 * We can't cope with this situation right
1.76      thorpej   516:                 * now (but it shouldn't ever happen, really, anyhow).
                    517:                 */
1.92      itojun    518: #ifdef DEBUG
1.78      thorpej   519:                printf("altq_etherclassify: headers span multiple mbufs: "
                    520:                    "%d < %d\n", m->m_len, (hlen + hdrsize));
1.92      itojun    521: #endif
1.76      thorpej   522:                goto bad;
                    523:        }
                    524:
                    525:        m->m_data += hlen;
                    526:        m->m_len -= hlen;
                    527:
1.147     christos  528:        hdr = mtod(m, void *);
1.76      thorpej   529:
                    530:        if (ALTQ_NEEDS_CLASSIFY(ifq))
1.219     knakahar  531:                m->m_pkthdr.pattr_class =
1.76      thorpej   532:                    (*ifq->altq_classify)(ifq->altq_clfier, m, af);
1.219     knakahar  533:        m->m_pkthdr.pattr_af = af;
                    534:        m->m_pkthdr.pattr_hdr = hdr;
1.76      thorpej   535:
                    536:        m->m_data -= hlen;
                    537:        m->m_len += hlen;
                    538:
                    539:        return;
                    540:
1.250     maxv      541: bad:
1.219     knakahar  542:        m->m_pkthdr.pattr_class = NULL;
                    543:        m->m_pkthdr.pattr_hdr = NULL;
                    544:        m->m_pkthdr.pattr_af = AF_UNSPEC;
1.76      thorpej   545: }
                    546: #endif /* ALTQ */
1.1       cgd       547:
                    548: /*
                    549:  * Process a received Ethernet packet;
1.42      thorpej   550:  * the packet is in the mbuf chain m with
                    551:  * the ether header.
1.1       cgd       552:  */
1.133     liamjfoy  553: void
1.58      matt      554: ether_input(struct ifnet *ifp, struct mbuf *m)
1.1       cgd       555: {
1.91      thorpej   556:        struct ethercom *ec = (struct ethercom *) ifp;
1.199     rmind     557:        pktqueue_t *pktq = NULL;
                    558:        struct ifqueue *inq = NULL;
1.162     matt      559:        uint16_t etype;
1.42      thorpej   560:        struct ether_header *eh;
1.187     matt      561:        size_t ehlen;
1.204     tls       562:        static int earlypkts;
1.198     msaitoh   563:        int isr = 0;
1.194     joerg     564: #if defined (LLC) || defined(NETATALK)
1.29      mrg       565:        struct llc *l;
1.18      christos  566: #endif
1.1       cgd       567:
1.216     ozaki-r   568:        KASSERT(!cpu_intr_p());
                    569:
1.8       mycroft   570:        if ((ifp->if_flags & IFF_UP) == 0) {
                    571:                m_freem(m);
                    572:                return;
                    573:        }
1.42      thorpej   574:
1.104     matt      575: #ifdef MBUFTRACE
1.116     jonathan  576:        m_claimm(m, &ec->ec_rx_mowner);
1.104     matt      577: #endif
1.42      thorpej   578:        eh = mtod(m, struct ether_header *);
1.63      thorpej   579:        etype = ntohs(eh->ether_type);
1.187     matt      580:        ehlen = sizeof(*eh);
1.63      thorpej   581:
1.250     maxv      582:        if (__predict_false(earlypkts < 100 || !rnd_initial_entropy)) {
1.204     tls       583:                rnd_add_data(NULL, eh, ehlen, 0);
                    584:                earlypkts++;
                    585:        }
                    586:
1.63      thorpej   587:        /*
                    588:         * Determine if the packet is within its size limits.
                    589:         */
1.182     kefren    590:        if (etype != ETHERTYPE_MPLS && m->m_pkthdr.len >
1.83      thorpej   591:            ETHER_MAX_FRAME(ifp, etype, m->m_flags & M_HASFCS)) {
1.203     ozaki-r   592:                mutex_enter(&bigpktpps_lock);
1.123     matt      593:                if (ppsratecheck(&bigpktppslim_last, &bigpktpps_count,
                    594:                            bigpktppslim)) {
                    595:                        printf("%s: discarding oversize frame (len=%d)\n",
                    596:                            ifp->if_xname, m->m_pkthdr.len);
                    597:                }
1.203     ozaki-r   598:                mutex_exit(&bigpktpps_lock);
1.63      thorpej   599:                m_freem(m);
                    600:                return;
                    601:        }
1.77      thorpej   602:
1.84      thorpej   603:        if (ETHER_IS_MULTICAST(eh->ether_dhost)) {
                    604:                /*
                    605:                 * If this is not a simplex interface, drop the packet
                    606:                 * if it came from us.
                    607:                 */
                    608:                if ((ifp->if_flags & IFF_SIMPLEX) == 0 &&
1.153     dyoung    609:                    memcmp(CLLADDR(ifp->if_sadl), eh->ether_shost,
1.84      thorpej   610:                    ETHER_ADDR_LEN) == 0) {
                    611:                        m_freem(m);
                    612:                        return;
                    613:                }
                    614:
                    615:                if (memcmp(etherbroadcastaddr,
                    616:                    eh->ether_dhost, ETHER_ADDR_LEN) == 0)
                    617:                        m->m_flags |= M_BCAST;
                    618:                else
                    619:                        m->m_flags |= M_MCAST;
                    620:                ifp->if_imcasts++;
                    621:        }
                    622:
1.79      thorpej   623:        /* If the CRC is still on the packet, trim it off. */
                    624:        if (m->m_flags & M_HASFCS) {
                    625:                m_adj(m, -ETHER_CRC_LEN);
                    626:                m->m_flags &= ~M_HASFCS;
                    627:        }
                    628:
1.42      thorpej   629:        ifp->if_ibytes += m->m_pkthdr.len;
1.78      thorpej   630:
1.201     ozaki-r   631: #if NCARP > 0
                    632:        if (__predict_false(ifp->if_carp && ifp->if_type != IFT_CARP)) {
                    633:                /*
                    634:                 * clear M_PROMISC, in case the packets comes from a
                    635:                 * vlan
                    636:                 */
1.137     rpaulo    637:                m->m_flags &= ~M_PROMISC;
1.201     ozaki-r   638:                if (carp_input(m, (uint8_t *)&eh->ether_shost,
                    639:                    (uint8_t *)&eh->ether_dhost, eh->ether_type) == 0)
1.137     rpaulo    640:                        return;
1.201     ozaki-r   641:        }
1.133     liamjfoy  642: #endif /* NCARP > 0 */
1.250     maxv      643:
1.247     msaitoh   644:        if ((m->m_flags & (M_BCAST | M_MCAST | M_PROMISC)) == 0 &&
1.201     ozaki-r   645:            (ifp->if_flags & IFF_PROMISC) != 0 &&
                    646:            memcmp(CLLADDR(ifp->if_sadl), eh->ether_dhost,
                    647:                   ETHER_ADDR_LEN) != 0) {
                    648:                m->m_flags |= M_PROMISC;
1.70      bouyer    649:        }
1.78      thorpej   650:
1.106     bouyer    651:        if ((m->m_flags & M_PROMISC) == 0) {
1.195     rmind     652:                if (pfil_run_hooks(ifp->if_pfil, &m, ifp, PFIL_IN) != 0)
1.106     bouyer    653:                        return;
                    654:                if (m == NULL)
                    655:                        return;
1.78      thorpej   656:
1.106     bouyer    657:                eh = mtod(m, struct ether_header *);
                    658:                etype = ntohs(eh->ether_type);
1.187     matt      659:                ehlen = sizeof(*eh);
1.106     bouyer    660:        }
1.70      bouyer    661:
1.172     darran    662: #if NAGR > 0
                    663:        if (ifp->if_agrprivate &&
                    664:            __predict_true(etype != ETHERTYPE_SLOWPROTOCOLS)) {
                    665:                m->m_flags &= ~M_PROMISC;
                    666:                agr_input(ifp, m);
                    667:                return;
                    668:        }
                    669: #endif /* NAGR > 0 */
                    670:
1.91      thorpej   671:        /*
                    672:         * If VLANs are configured on the interface, check to
                    673:         * see if the device performed the decapsulation and
                    674:         * provided us with the tag.
                    675:         */
1.244     knakahar  676:        if (ec->ec_nvlans && vlan_has_tag(m)) {
1.70      bouyer    677: #if NVLAN > 0
                    678:                /*
                    679:                 * vlan_input() will either recursively call ether_input()
                    680:                 * or drop the packet.
                    681:                 */
1.91      thorpej   682:                vlan_input(ifp, m);
                    683: #else
                    684:                m_freem(m);
1.70      bouyer    685: #endif
1.61      thorpej   686:                return;
                    687:        }
1.1       cgd       688:
1.59      thorpej   689:        /*
                    690:         * Handle protocols that expect to have the Ethernet header
                    691:         * (and possibly FCS) intact.
                    692:         */
                    693:        switch (etype) {
1.187     matt      694:        case ETHERTYPE_VLAN: {
                    695:                struct ether_vlan_header *evl = (void *)eh;
                    696:                /*
                    697:                 * If there is a tag of 0, then the VLAN header was probably
                    698:                 * just being used to store the priority.  Extract the ether
1.250     maxv      699:                 * type, and if IP or IPV6, let them deal with it.
1.187     matt      700:                 */
                    701:                if (m->m_len <= sizeof(*evl)
                    702:                    && EVL_VLANOFTAG(evl->evl_tag) == 0) {
                    703:                        etype = ntohs(evl->evl_proto);
                    704:                        ehlen = sizeof(*evl);
                    705:                        if ((m->m_flags & M_PROMISC) == 0
                    706:                            && (etype == ETHERTYPE_IP
                    707:                                || etype == ETHERTYPE_IPV6))
                    708:                                break;
                    709:                }
1.59      thorpej   710: #if NVLAN > 0
                    711:                /*
                    712:                 * vlan_input() will either recursively call ether_input()
                    713:                 * or drop the packet.
                    714:                 */
1.63      thorpej   715:                if (((struct ethercom *)ifp)->ec_nvlans != 0)
                    716:                        vlan_input(ifp, m);
1.65      enami     717:                else
1.187     matt      718: #endif /* NVLAN > 0 */
1.65      enami     719:                        m_freem(m);
1.59      thorpej   720:                return;
1.187     matt      721:        }
1.81      martin    722: #if NPPPOE > 0
                    723:        case ETHERTYPE_PPPOEDISC:
1.218     ozaki-r   724:                pppoedisc_input(ifp, m);
                    725:                return;
1.81      martin    726:        case ETHERTYPE_PPPOE:
1.218     ozaki-r   727:                pppoe_input(ifp, m);
1.81      martin    728:                return;
                    729: #endif /* NPPPOE > 0 */
1.121     yamt      730:        case ETHERTYPE_SLOWPROTOCOLS: {
                    731:                uint8_t subtype;
                    732:
                    733: #if defined(DIAGNOSTIC)
                    734:                if (m->m_pkthdr.len < sizeof(*eh) + sizeof(subtype)) {
                    735:                        panic("ether_input: too short slow protocol packet");
                    736:                }
                    737: #endif
                    738:                m_copydata(m, sizeof(*eh), sizeof(subtype), &subtype);
                    739:                switch (subtype) {
                    740: #if NAGR > 0
                    741:                case SLOWPROTOCOLS_SUBTYPE_LACP:
                    742:                        if (ifp->if_agrprivate) {
                    743:                                ieee8023ad_lacp_input(ifp, m);
                    744:                                return;
                    745:                        }
                    746:                        break;
                    747:
                    748:                case SLOWPROTOCOLS_SUBTYPE_MARKER:
                    749:                        if (ifp->if_agrprivate) {
                    750:                                ieee8023ad_marker_input(ifp, m);
                    751:                                return;
                    752:                        }
                    753:                        break;
                    754: #endif /* NAGR > 0 */
                    755:                default:
                    756:                        if (subtype == 0 || subtype > 10) {
                    757:                                /* illegal value */
                    758:                                m_freem(m);
                    759:                                return;
                    760:                        }
                    761:                        /* unknown subtype */
                    762:                        break;
                    763:                }
                    764:                /* FALLTHROUGH */
                    765:        }
1.59      thorpej   766:        default:
1.106     bouyer    767:                if (m->m_flags & M_PROMISC) {
                    768:                        m_freem(m);
                    769:                        return;
                    770:                }
1.59      thorpej   771:        }
1.42      thorpej   772:
1.45      thorpej   773:        /* If the CRC is still on the packet, trim it off. */
1.79      thorpej   774:        if (m->m_flags & M_HASFCS) {
1.45      thorpej   775:                m_adj(m, -ETHER_CRC_LEN);
1.79      thorpej   776:                m->m_flags &= ~M_HASFCS;
                    777:        }
1.42      thorpej   778:
1.139     is        779:        if (etype > ETHERMTU + sizeof (struct ether_header)) {
                    780:                /* Strip off the Ethernet header. */
1.187     matt      781:                m_adj(m, ehlen);
1.139     is        782:
                    783:                switch (etype) {
1.1       cgd       784: #ifdef INET
1.139     is        785:                case ETHERTYPE_IP:
1.30      matt      786: #ifdef GATEWAY
1.139     is        787:                        if (ipflow_fastforward(m))
                    788:                                return;
1.30      matt      789: #endif
1.199     rmind     790:                        pktq = ip_pktq;
1.139     is        791:                        break;
1.1       cgd       792:
1.139     is        793:                case ETHERTYPE_ARP:
1.198     msaitoh   794:                        isr = NETISR_ARP;
1.139     is        795:                        inq = &arpintrq;
                    796:                        break;
1.7       glass     797:
1.139     is        798:                case ETHERTYPE_REVARP:
                    799:                        revarpinput(m); /* XXX queue? */
                    800:                        return;
1.1       cgd       801: #endif
1.44      itojun    802: #ifdef INET6
1.139     is        803:                case ETHERTYPE_IPV6:
1.196     pooka     804:                        if (__predict_false(!in6_present)) {
                    805:                                m_freem(m);
                    806:                                return;
                    807:                        }
1.250     maxv      808: #ifdef GATEWAY
1.192     christos  809:                        if (ip6flow_fastforward(&m))
1.148     liamjfoy  810:                                return;
                    811: #endif
1.199     rmind     812:                        pktq = ip6_pktq;
1.139     is        813:                        break;
1.44      itojun    814: #endif
1.23      christos  815: #ifdef NETATALK
1.174     isaki     816:                case ETHERTYPE_ATALK:
1.198     msaitoh   817:                        isr = NETISR_ATALK;
1.174     isaki     818:                        inq = &atintrq1;
                    819:                        break;
                    820:                case ETHERTYPE_AARP:
1.139     is        821:                        /* probably this should be done with a NETISR as well */
1.175     mbalmer   822:                        aarpinput(ifp, m); /* XXX */
1.174     isaki     823:                        return;
1.23      christos  824: #endif /* NETATALK */
1.182     kefren    825: #ifdef MPLS
                    826:                case ETHERTYPE_MPLS:
1.198     msaitoh   827:                        isr = NETISR_MPLS;
1.182     kefren    828:                        inq = &mplsintrq;
                    829:                        break;
                    830: #endif
1.139     is        831:                default:
                    832:                        m_freem(m);
                    833:                        return;
                    834:                }
                    835:        } else {
1.194     joerg     836: #if defined (LLC) || defined (NETATALK)
1.249     maxv      837:                if (m->m_len < ehlen + sizeof(struct llc)) {
                    838:                        goto dropanyway;
                    839:                }
1.139     is        840:                l = (struct llc *)(eh+1);
1.8       mycroft   841:                switch (l->llc_dsap) {
1.23      christos  842: #ifdef NETATALK
                    843:                case LLC_SNAP_LSAP:
                    844:                        switch (l->llc_control) {
                    845:                        case LLC_UI:
                    846:                                if (l->llc_ssap != LLC_SNAP_LSAP) {
                    847:                                        goto dropanyway;
                    848:                                }
1.120     perry     849:
1.166     dyoung    850:                                if (memcmp(&(l->llc_snap_org_code)[0],
1.23      christos  851:                                    at_org_code, sizeof(at_org_code)) == 0 &&
                    852:                                    ntohs(l->llc_snap_ether_type) ==
1.38      kim       853:                                    ETHERTYPE_ATALK) {
1.23      christos  854:                                        inq = &atintrq2;
1.139     is        855:                                        m_adj(m, sizeof(struct ether_header)
                    856:                                            + sizeof(struct llc));
1.198     msaitoh   857:                                        isr = NETISR_ATALK;
1.23      christos  858:                                        break;
                    859:                                }
                    860:
1.166     dyoung    861:                                if (memcmp(&(l->llc_snap_org_code)[0],
1.23      christos  862:                                    aarp_org_code,
                    863:                                    sizeof(aarp_org_code)) == 0 &&
                    864:                                    ntohs(l->llc_snap_ether_type) ==
                    865:                                    ETHERTYPE_AARP) {
1.139     is        866:                                        m_adj( m, sizeof(struct ether_header)
                    867:                                            + sizeof(struct llc));
1.23      christos  868:                                        aarpinput(ifp, m); /* XXX */
                    869:                                    return;
                    870:                                }
1.120     perry     871:
1.23      christos  872:                        default:
                    873:                                goto dropanyway;
                    874:                        }
                    875:                        break;
1.249     maxv      876: #endif
1.1       cgd       877:                dropanyway:
                    878:                default:
1.8       mycroft   879:                        m_freem(m);
                    880:                        return;
                    881:                }
1.250     maxv      882: #else /* LLC || NETATALK */
1.139     is        883:                m_freem(m);
                    884:                return;
1.250     maxv      885: #endif /* LLC || NETATALK */
1.1       cgd       886:        }
                    887:
1.199     rmind     888:        if (__predict_true(pktq)) {
1.229     ozaki-r   889: #ifdef NET_MPSAFE
                    890:                const u_int h = curcpu()->ci_index;
                    891: #else
1.199     rmind     892:                const uint32_t h = pktq_rps_hash(m);
1.229     ozaki-r   893: #endif
1.199     rmind     894:                if (__predict_false(!pktq_enqueue(pktq, m, h))) {
                    895:                        m_freem(m);
                    896:                }
                    897:                return;
                    898:        }
                    899:
                    900:        if (__predict_false(!inq)) {
                    901:                /* Should not happen. */
                    902:                m_freem(m);
                    903:                return;
                    904:        }
1.228     ozaki-r   905:
                    906:        IFQ_LOCK(inq);
1.1       cgd       907:        if (IF_QFULL(inq)) {
                    908:                IF_DROP(inq);
1.228     ozaki-r   909:                IFQ_UNLOCK(inq);
1.1       cgd       910:                m_freem(m);
1.198     msaitoh   911:        } else {
1.1       cgd       912:                IF_ENQUEUE(inq, m);
1.228     ozaki-r   913:                IFQ_UNLOCK(inq);
1.198     msaitoh   914:                schednetisr(isr);
                    915:        }
1.1       cgd       916: }
                    917:
                    918: /*
                    919:  * Convert Ethernet address to printable (loggable) representation.
                    920:  */
                    921: char *
1.58      matt      922: ether_sprintf(const u_char *ap)
1.1       cgd       923: {
1.129     christos  924:        static char etherbuf[3 * ETHER_ADDR_LEN];
                    925:        return ether_snprintf(etherbuf, sizeof(etherbuf), ap);
                    926: }
                    927:
                    928: char *
                    929: ether_snprintf(char *buf, size_t len, const u_char *ap)
                    930: {
                    931:        char *cp = buf;
                    932:        size_t i;
1.1       cgd       933:
1.129     christos  934:        for (i = 0; i < len / 3; i++) {
1.124     christos  935:                *cp++ = hexdigits[*ap >> 4];
                    936:                *cp++ = hexdigits[*ap++ & 0xf];
1.1       cgd       937:                *cp++ = ':';
                    938:        }
1.129     christos  939:        *--cp = '\0';
                    940:        return buf;
1.1       cgd       941: }
1.8       mycroft   942:
                    943: /*
                    944:  * Perform common duties while attaching to interface list
                    945:  */
                    946: void
1.162     matt      947: ether_ifattach(struct ifnet *ifp, const uint8_t *lla)
1.8       mycroft   948: {
1.104     matt      949:        struct ethercom *ec = (struct ethercom *)ifp;
1.8       mycroft   950:
                    951:        ifp->if_type = IFT_ETHER;
1.94      enami     952:        ifp->if_hdrlen = ETHER_HDR_LEN;
1.73      thorpej   953:        ifp->if_dlt = DLT_EN10MB;
1.8       mycroft   954:        ifp->if_mtu = ETHERMTU;
1.12      mycroft   955:        ifp->if_output = ether_output;
1.216     ozaki-r   956:        ifp->_if_input = ether_input;
1.54      thorpej   957:        if (ifp->if_baudrate == 0)
                    958:                ifp->if_baudrate = IF_Mbps(10);         /* just a default */
1.75      thorpej   959:
1.230     ozaki-r   960:        if (lla != NULL)
                    961:                if_set_sadl(ifp, lla, ETHER_ADDR_LEN, !ETHER_IS_LOCAL(lla));
1.75      thorpej   962:
1.104     matt      963:        LIST_INIT(&ec->ec_multiaddrs);
1.233     ozaki-r   964:        ec->ec_lock = mutex_obj_alloc(MUTEX_DEFAULT, IPL_NET);
1.26      is        965:        ifp->if_broadcastaddr = etherbroadcastaddr;
1.177     joerg     966:        bpf_attach(ifp, DLT_EN10MB, sizeof(struct ether_header));
1.104     matt      967: #ifdef MBUFTRACE
1.108     itojun    968:        strlcpy(ec->ec_tx_mowner.mo_name, ifp->if_xname,
                    969:            sizeof(ec->ec_tx_mowner.mo_name));
                    970:        strlcpy(ec->ec_tx_mowner.mo_descr, "tx",
                    971:            sizeof(ec->ec_tx_mowner.mo_descr));
                    972:        strlcpy(ec->ec_rx_mowner.mo_name, ifp->if_xname,
                    973:            sizeof(ec->ec_rx_mowner.mo_name));
                    974:        strlcpy(ec->ec_rx_mowner.mo_descr, "rx",
                    975:            sizeof(ec->ec_rx_mowner.mo_descr));
1.104     matt      976:        MOWNER_ATTACH(&ec->ec_tx_mowner);
                    977:        MOWNER_ATTACH(&ec->ec_rx_mowner);
                    978:        ifp->if_mowner = &ec->ec_tx_mowner;
                    979: #endif
1.52      thorpej   980: }
                    981:
                    982: void
1.58      matt      983: ether_ifdetach(struct ifnet *ifp)
1.52      thorpej   984: {
1.63      thorpej   985:        struct ethercom *ec = (void *) ifp;
                    986:        struct ether_multi *enm;
1.69      thorpej   987:
1.190     christos  988:        /*
                    989:         * Prevent further calls to ioctl (for example turning off
                    990:         * promiscuous mode from the bridge code), which eventually can
                    991:         * call if_init() which can cause panics because the interface
                    992:         * is in the process of being detached. Return device not configured
                    993:         * instead.
                    994:         */
                    995:        ifp->if_ioctl = (int (*)(struct ifnet *, u_long, void *))enxio;
                    996:
1.115     dyoung    997: #if NBRIDGE > 0
                    998:        if (ifp->if_bridge)
                    999:                bridge_ifdetach(ifp);
                   1000: #endif
1.177     joerg    1001:        bpf_detach(ifp);
1.64      thorpej  1002: #if NVLAN > 0
                   1003:        if (ec->ec_nvlans)
                   1004:                vlan_ifdetach(ifp);
                   1005: #endif
1.63      thorpej  1006:
1.245     msaitoh  1007:        ETHER_LOCK(ec);
1.63      thorpej  1008:        while ((enm = LIST_FIRST(&ec->ec_multiaddrs)) != NULL) {
                   1009:                LIST_REMOVE(enm, enm_list);
1.248     ozaki-r  1010:                kmem_free(enm, sizeof(*enm));
1.63      thorpej  1011:                ec->ec_multicnt--;
                   1012:        }
1.245     msaitoh  1013:        ETHER_UNLOCK(ec);
1.52      thorpej  1014:
1.252   ! maxv     1015:        mutex_obj_free(ec->ec_lock);
1.231     ozaki-r  1016:
1.189     chs      1017:        ifp->if_mowner = NULL;
1.104     matt     1018:        MOWNER_DETACH(&ec->ec_rx_mowner);
                   1019:        MOWNER_DETACH(&ec->ec_tx_mowner);
1.53      thorpej  1020: }
                   1021:
1.56      thorpej  1022: #if 0
                   1023: /*
                   1024:  * This is for reference.  We have a table-driven version
                   1025:  * of the little-endian crc32 generator, which is faster
                   1026:  * than the double-loop.
                   1027:  */
1.162     matt     1028: uint32_t
                   1029: ether_crc32_le(const uint8_t *buf, size_t len)
1.53      thorpej  1030: {
1.162     matt     1031:        uint32_t c, crc, carry;
1.53      thorpej  1032:        size_t i, j;
                   1033:
                   1034:        crc = 0xffffffffU;      /* initial value */
                   1035:
                   1036:        for (i = 0; i < len; i++) {
                   1037:                c = buf[i];
                   1038:                for (j = 0; j < 8; j++) {
                   1039:                        carry = ((crc & 0x01) ? 1 : 0) ^ (c & 0x01);
                   1040:                        crc >>= 1;
                   1041:                        c >>= 1;
                   1042:                        if (carry)
1.56      thorpej  1043:                                crc = (crc ^ ETHER_CRC_POLY_LE);
1.53      thorpej  1044:                }
                   1045:        }
                   1046:
                   1047:        return (crc);
                   1048: }
1.56      thorpej  1049: #else
1.162     matt     1050: uint32_t
                   1051: ether_crc32_le(const uint8_t *buf, size_t len)
1.56      thorpej  1052: {
1.162     matt     1053:        static const uint32_t crctab[] = {
1.56      thorpej  1054:                0x00000000, 0x1db71064, 0x3b6e20c8, 0x26d930ac,
                   1055:                0x76dc4190, 0x6b6b51f4, 0x4db26158, 0x5005713c,
                   1056:                0xedb88320, 0xf00f9344, 0xd6d6a3e8, 0xcb61b38c,
                   1057:                0x9b64c2b0, 0x86d3d2d4, 0xa00ae278, 0xbdbdf21c
                   1058:        };
1.162     matt     1059:        uint32_t crc;
1.98      thorpej  1060:        size_t i;
1.56      thorpej  1061:
                   1062:        crc = 0xffffffffU;      /* initial value */
                   1063:
                   1064:        for (i = 0; i < len; i++) {
                   1065:                crc ^= buf[i];
                   1066:                crc = (crc >> 4) ^ crctab[crc & 0xf];
                   1067:                crc = (crc >> 4) ^ crctab[crc & 0xf];
                   1068:        }
                   1069:
                   1070:        return (crc);
                   1071: }
                   1072: #endif
1.53      thorpej  1073:
1.162     matt     1074: uint32_t
                   1075: ether_crc32_be(const uint8_t *buf, size_t len)
1.53      thorpej  1076: {
1.162     matt     1077:        uint32_t c, crc, carry;
1.53      thorpej  1078:        size_t i, j;
                   1079:
                   1080:        crc = 0xffffffffU;      /* initial value */
                   1081:
                   1082:        for (i = 0; i < len; i++) {
                   1083:                c = buf[i];
                   1084:                for (j = 0; j < 8; j++) {
                   1085:                        carry = ((crc & 0x80000000U) ? 1 : 0) ^ (c & 0x01);
                   1086:                        crc <<= 1;
                   1087:                        c >>= 1;
                   1088:                        if (carry)
                   1089:                                crc = (crc ^ ETHER_CRC_POLY_BE) | carry;
                   1090:                }
                   1091:        }
                   1092:
                   1093:        return (crc);
1.8       mycroft  1094: }
                   1095:
1.48      is       1096: #ifdef INET
1.118     yamt     1097: const uint8_t ether_ipmulticast_min[ETHER_ADDR_LEN] =
                   1098:     { 0x01, 0x00, 0x5e, 0x00, 0x00, 0x00 };
                   1099: const uint8_t ether_ipmulticast_max[ETHER_ADDR_LEN] =
                   1100:     { 0x01, 0x00, 0x5e, 0x7f, 0xff, 0xff };
1.48      is       1101: #endif
1.44      itojun   1102: #ifdef INET6
1.118     yamt     1103: const uint8_t ether_ip6multicast_min[ETHER_ADDR_LEN] =
                   1104:     { 0x33, 0x33, 0x00, 0x00, 0x00, 0x00 };
                   1105: const uint8_t ether_ip6multicast_max[ETHER_ADDR_LEN] =
                   1106:     { 0x33, 0x33, 0xff, 0xff, 0xff, 0xff };
1.44      itojun   1107: #endif
1.60      enami    1108:
1.3       hpeyerl  1109: /*
1.138     rpaulo   1110:  * ether_aton implementation, not using a static buffer.
                   1111:  */
                   1112: int
1.180     christos 1113: ether_aton_r(u_char *dest, size_t len, const char *str)
1.138     rpaulo   1114: {
1.250     maxv     1115:        const u_char *cp = (const void *)str;
1.180     christos 1116:        u_char *ep;
                   1117:
1.185     tsutsui  1118: #define atox(c)        (((c) <= '9') ? ((c) - '0') : ((toupper(c) - 'A') + 10))
1.180     christos 1119:
                   1120:        if (len < ETHER_ADDR_LEN)
                   1121:                return ENOSPC;
                   1122:
                   1123:        ep = dest + ETHER_ADDR_LEN;
1.250     maxv     1124:
1.180     christos 1125:        while (*cp) {
1.250     maxv     1126:                if (!isxdigit(*cp))
                   1127:                        return EINVAL;
1.252   ! maxv     1128:
1.180     christos 1129:                *dest = atox(*cp);
                   1130:                cp++;
1.250     maxv     1131:                if (isxdigit(*cp)) {
                   1132:                        *dest = (*dest << 4) | atox(*cp);
1.180     christos 1133:                        cp++;
1.250     maxv     1134:                }
1.252   ! maxv     1135:                dest++;
        !          1136:
1.180     christos 1137:                if (dest == ep)
1.252   ! maxv     1138:                        return (*cp == '\0') ? 0 : ENAMETOOLONG;
        !          1139:
1.180     christos 1140:                switch (*cp) {
                   1141:                case ':':
                   1142:                case '-':
                   1143:                case '.':
1.179     jakllsch 1144:                        cp++;
1.180     christos 1145:                        break;
1.179     jakllsch 1146:                }
1.250     maxv     1147:        }
1.180     christos 1148:        return ENOBUFS;
1.138     rpaulo   1149: }
                   1150:
                   1151: /*
1.60      enami    1152:  * Convert a sockaddr into an Ethernet address or range of Ethernet
                   1153:  * addresses.
1.3       hpeyerl  1154:  */
                   1155: int
1.162     matt     1156: ether_multiaddr(const struct sockaddr *sa, uint8_t addrlo[ETHER_ADDR_LEN],
                   1157:     uint8_t addrhi[ETHER_ADDR_LEN])
1.3       hpeyerl  1158: {
1.24      christos 1159: #ifdef INET
1.155     dyoung   1160:        const struct sockaddr_in *sin;
1.24      christos 1161: #endif /* INET */
1.44      itojun   1162: #ifdef INET6
1.155     dyoung   1163:        const struct sockaddr_in6 *sin6;
1.44      itojun   1164: #endif /* INET6 */
1.3       hpeyerl  1165:
1.60      enami    1166:        switch (sa->sa_family) {
1.3       hpeyerl  1167:
                   1168:        case AF_UNSPEC:
1.146     dyoung   1169:                memcpy(addrlo, sa->sa_data, ETHER_ADDR_LEN);
                   1170:                memcpy(addrhi, addrlo, ETHER_ADDR_LEN);
1.3       hpeyerl  1171:                break;
                   1172:
                   1173: #ifdef INET
                   1174:        case AF_INET:
1.155     dyoung   1175:                sin = satocsin(sa);
1.3       hpeyerl  1176:                if (sin->sin_addr.s_addr == INADDR_ANY) {
                   1177:                        /*
1.60      enami    1178:                         * An IP address of INADDR_ANY means listen to
                   1179:                         * or stop listening to all of the Ethernet
                   1180:                         * multicast addresses used for IP.
1.3       hpeyerl  1181:                         * (This is for the sake of IP multicast routers.)
                   1182:                         */
1.146     dyoung   1183:                        memcpy(addrlo, ether_ipmulticast_min, ETHER_ADDR_LEN);
                   1184:                        memcpy(addrhi, ether_ipmulticast_max, ETHER_ADDR_LEN);
1.252   ! maxv     1185:                } else {
1.3       hpeyerl  1186:                        ETHER_MAP_IP_MULTICAST(&sin->sin_addr, addrlo);
1.146     dyoung   1187:                        memcpy(addrhi, addrlo, ETHER_ADDR_LEN);
1.3       hpeyerl  1188:                }
                   1189:                break;
                   1190: #endif
1.44      itojun   1191: #ifdef INET6
                   1192:        case AF_INET6:
1.155     dyoung   1193:                sin6 = satocsin6(sa);
1.47      itojun   1194:                if (IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) {
1.44      itojun   1195:                        /*
1.60      enami    1196:                         * An IP6 address of 0 means listen to or stop
                   1197:                         * listening to all of the Ethernet multicast
                   1198:                         * address used for IP6.
1.44      itojun   1199:                         * (This is used for multicast routers.)
                   1200:                         */
1.146     dyoung   1201:                        memcpy(addrlo, ether_ip6multicast_min, ETHER_ADDR_LEN);
                   1202:                        memcpy(addrhi, ether_ip6multicast_max, ETHER_ADDR_LEN);
1.44      itojun   1203:                } else {
                   1204:                        ETHER_MAP_IPV6_MULTICAST(&sin6->sin6_addr, addrlo);
1.146     dyoung   1205:                        memcpy(addrhi, addrlo, ETHER_ADDR_LEN);
1.44      itojun   1206:                }
                   1207:                break;
                   1208: #endif
1.3       hpeyerl  1209:
                   1210:        default:
1.146     dyoung   1211:                return EAFNOSUPPORT;
1.60      enami    1212:        }
1.146     dyoung   1213:        return 0;
1.60      enami    1214: }
                   1215:
                   1216: /*
                   1217:  * Add an Ethernet multicast address or range of addresses to the list for a
                   1218:  * given interface.
                   1219:  */
                   1220: int
1.155     dyoung   1221: ether_addmulti(const struct sockaddr *sa, struct ethercom *ec)
1.60      enami    1222: {
1.231     ozaki-r  1223:        struct ether_multi *enm, *_enm;
1.60      enami    1224:        u_char addrlo[ETHER_ADDR_LEN];
                   1225:        u_char addrhi[ETHER_ADDR_LEN];
1.237     skrll    1226:        int error = 0;
1.231     ozaki-r  1227:
                   1228:        /* Allocate out of lock */
1.248     ozaki-r  1229:        enm = kmem_alloc(sizeof(*enm), KM_SLEEP);
1.60      enami    1230:
1.245     msaitoh  1231:        ETHER_LOCK(ec);
1.155     dyoung   1232:        error = ether_multiaddr(sa, addrlo, addrhi);
1.231     ozaki-r  1233:        if (error != 0)
                   1234:                goto out;
1.3       hpeyerl  1235:
                   1236:        /*
                   1237:         * Verify that we have valid Ethernet multicast addresses.
                   1238:         */
1.186     yamt     1239:        if (!ETHER_IS_MULTICAST(addrlo) || !ETHER_IS_MULTICAST(addrhi)) {
1.231     ozaki-r  1240:                error = EINVAL;
                   1241:                goto out;
1.3       hpeyerl  1242:        }
1.252   ! maxv     1243:
1.3       hpeyerl  1244:        /*
                   1245:         * See if the address range is already in the list.
                   1246:         */
1.231     ozaki-r  1247:        ETHER_LOOKUP_MULTI(addrlo, addrhi, ec, _enm);
                   1248:        if (_enm != NULL) {
1.3       hpeyerl  1249:                /*
                   1250:                 * Found it; just increment the reference count.
                   1251:                 */
1.231     ozaki-r  1252:                ++_enm->enm_refcount;
                   1253:                error = 0;
                   1254:                goto out;
1.3       hpeyerl  1255:        }
1.252   ! maxv     1256:
1.3       hpeyerl  1257:        /*
1.239     ozaki-r  1258:         * Link a new multicast record into the interface's multicast list.
1.3       hpeyerl  1259:         */
1.252   ! maxv     1260:        memcpy(enm->enm_addrlo, addrlo, ETHER_ADDR_LEN);
        !          1261:        memcpy(enm->enm_addrhi, addrhi, ETHER_ADDR_LEN);
1.3       hpeyerl  1262:        enm->enm_refcount = 1;
1.22      is       1263:        LIST_INSERT_HEAD(&ec->ec_multiaddrs, enm, enm_list);
                   1264:        ec->ec_multicnt++;
1.252   ! maxv     1265:
1.3       hpeyerl  1266:        /*
                   1267:         * Return ENETRESET to inform the driver that the list has changed
                   1268:         * and its reception filter should be adjusted accordingly.
                   1269:         */
1.231     ozaki-r  1270:        error = ENETRESET;
                   1271:        enm = NULL;
1.252   ! maxv     1272:
1.231     ozaki-r  1273: out:
1.245     msaitoh  1274:        ETHER_UNLOCK(ec);
1.231     ozaki-r  1275:        if (enm != NULL)
1.248     ozaki-r  1276:                kmem_free(enm, sizeof(*enm));
1.231     ozaki-r  1277:        return error;
1.3       hpeyerl  1278: }
                   1279:
                   1280: /*
                   1281:  * Delete a multicast address record.
                   1282:  */
                   1283: int
1.155     dyoung   1284: ether_delmulti(const struct sockaddr *sa, struct ethercom *ec)
1.3       hpeyerl  1285: {
1.29      mrg      1286:        struct ether_multi *enm;
1.60      enami    1287:        u_char addrlo[ETHER_ADDR_LEN];
                   1288:        u_char addrhi[ETHER_ADDR_LEN];
1.237     skrll    1289:        int error;
1.3       hpeyerl  1290:
1.245     msaitoh  1291:        ETHER_LOCK(ec);
1.155     dyoung   1292:        error = ether_multiaddr(sa, addrlo, addrhi);
1.231     ozaki-r  1293:        if (error != 0)
                   1294:                goto error;
1.3       hpeyerl  1295:
                   1296:        /*
1.252   ! maxv     1297:         * Look up the address in our list.
1.3       hpeyerl  1298:         */
1.22      is       1299:        ETHER_LOOKUP_MULTI(addrlo, addrhi, ec, enm);
1.3       hpeyerl  1300:        if (enm == NULL) {
1.231     ozaki-r  1301:                error = ENXIO;
                   1302:                goto error;
1.3       hpeyerl  1303:        }
                   1304:        if (--enm->enm_refcount != 0) {
                   1305:                /*
                   1306:                 * Still some claims to this record.
                   1307:                 */
1.231     ozaki-r  1308:                error = 0;
                   1309:                goto error;
1.3       hpeyerl  1310:        }
1.252   ! maxv     1311:
1.3       hpeyerl  1312:        /*
                   1313:         * No remaining claims to this record; unlink and free it.
                   1314:         */
1.13      mycroft  1315:        LIST_REMOVE(enm, enm_list);
1.22      is       1316:        ec->ec_multicnt--;
1.245     msaitoh  1317:        ETHER_UNLOCK(ec);
1.252   ! maxv     1318:        kmem_free(enm, sizeof(*enm));
1.231     ozaki-r  1319:
1.3       hpeyerl  1320:        /*
                   1321:         * Return ENETRESET to inform the driver that the list has changed
                   1322:         * and its reception filter should be adjusted accordingly.
                   1323:         */
1.231     ozaki-r  1324:        return ENETRESET;
1.252   ! maxv     1325:
1.231     ozaki-r  1326: error:
1.245     msaitoh  1327:        ETHER_UNLOCK(ec);
1.231     ozaki-r  1328:        return error;
1.66      thorpej  1329: }
                   1330:
1.170     dyoung   1331: void
                   1332: ether_set_ifflags_cb(struct ethercom *ec, ether_cb_t cb)
                   1333: {
                   1334:        ec->ec_ifflags_cb = cb;
                   1335: }
                   1336:
1.66      thorpej  1337: /*
                   1338:  * Common ioctls for Ethernet interfaces.  Note, we must be
                   1339:  * called at splnet().
                   1340:  */
                   1341: int
1.147     christos 1342: ether_ioctl(struct ifnet *ifp, u_long cmd, void *data)
1.66      thorpej  1343: {
                   1344:        struct ethercom *ec = (void *) ifp;
1.193     msaitoh  1345:        struct eccapreq *eccr;
1.66      thorpej  1346:        struct ifreq *ifr = (struct ifreq *)data;
1.170     dyoung   1347:        struct if_laddrreq *iflr = data;
                   1348:        const struct sockaddr_dl *sdl;
                   1349:        static const uint8_t zero[ETHER_ADDR_LEN];
1.169     dyoung   1350:        int error;
1.66      thorpej  1351:
                   1352:        switch (cmd) {
1.170     dyoung   1353:        case SIOCINITIFADDR:
1.191     matt     1354:            {
                   1355:                struct ifaddr *ifa = (struct ifaddr *)data;
                   1356:                if (ifa->ifa_addr->sa_family != AF_LINK
1.247     msaitoh  1357:                    && (ifp->if_flags & (IFF_UP | IFF_RUNNING)) !=
                   1358:                       (IFF_UP | IFF_RUNNING)) {
1.170     dyoung   1359:                        ifp->if_flags |= IFF_UP;
                   1360:                        if ((error = (*ifp->if_init)(ifp)) != 0)
                   1361:                                return error;
                   1362:                }
1.66      thorpej  1363: #ifdef INET
1.191     matt     1364:                if (ifa->ifa_addr->sa_family == AF_INET)
                   1365:                        arp_ifinit(ifp, ifa);
1.252   ! maxv     1366: #endif
1.169     dyoung   1367:                return 0;
1.191     matt     1368:            }
1.66      thorpej  1369:
                   1370:        case SIOCSIFMTU:
1.82      thorpej  1371:            {
                   1372:                int maxmtu;
                   1373:
                   1374:                if (ec->ec_capabilities & ETHERCAP_JUMBO_MTU)
                   1375:                        maxmtu = ETHERMTU_JUMBO;
                   1376:                else
                   1377:                        maxmtu = ETHERMTU;
                   1378:
                   1379:                if (ifr->ifr_mtu < ETHERMIN || ifr->ifr_mtu > maxmtu)
1.169     dyoung   1380:                        return EINVAL;
                   1381:                else if ((error = ifioctl_common(ifp, cmd, data)) != ENETRESET)
                   1382:                        return error;
                   1383:                else if (ifp->if_flags & IFF_UP) {
1.88      thorpej  1384:                        /* Make sure the device notices the MTU change. */
1.169     dyoung   1385:                        return (*ifp->if_init)(ifp);
                   1386:                } else
                   1387:                        return 0;
1.82      thorpej  1388:            }
1.66      thorpej  1389:
                   1390:        case SIOCSIFFLAGS:
1.170     dyoung   1391:                if ((error = ifioctl_common(ifp, cmd, data)) != 0)
                   1392:                        return error;
1.247     msaitoh  1393:                switch (ifp->if_flags & (IFF_UP | IFF_RUNNING)) {
1.169     dyoung   1394:                case IFF_RUNNING:
1.66      thorpej  1395:                        /*
                   1396:                         * If interface is marked down and it is running,
                   1397:                         * then stop and disable it.
                   1398:                         */
                   1399:                        (*ifp->if_stop)(ifp, 1);
1.169     dyoung   1400:                        break;
                   1401:                case IFF_UP:
1.66      thorpej  1402:                        /*
                   1403:                         * If interface is marked up and it is stopped, then
                   1404:                         * start it.
                   1405:                         */
1.169     dyoung   1406:                        return (*ifp->if_init)(ifp);
1.247     msaitoh  1407:                case IFF_UP | IFF_RUNNING:
1.170     dyoung   1408:                        error = 0;
1.247     msaitoh  1409:                        if (ec->ec_ifflags_cb != NULL) {
                   1410:                                error = (*ec->ec_ifflags_cb)(ec);
                   1411:                                if (error == ENETRESET) {
                   1412:                                        /*
                   1413:                                         * Reset the interface to pick up
                   1414:                                         * changes in any other flags that
                   1415:                                         * affect the hardware state.
                   1416:                                         */
                   1417:                                        return (*ifp->if_init)(ifp);
                   1418:                                }
                   1419:                        } else
                   1420:                                error = (*ifp->if_init)(ifp);
                   1421:                        return error;
1.169     dyoung   1422:                case 0:
                   1423:                        break;
1.66      thorpej  1424:                }
1.169     dyoung   1425:                return 0;
1.193     msaitoh  1426:        case SIOCGETHERCAP:
                   1427:                eccr = (struct eccapreq *)data;
                   1428:                eccr->eccr_capabilities = ec->ec_capabilities;
                   1429:                eccr->eccr_capenable = ec->ec_capenable;
                   1430:                return 0;
1.66      thorpej  1431:        case SIOCADDMULTI:
1.169     dyoung   1432:                return ether_addmulti(ifreq_getaddr(cmd, ifr), ec);
1.66      thorpej  1433:        case SIOCDELMULTI:
1.169     dyoung   1434:                return ether_delmulti(ifreq_getaddr(cmd, ifr), ec);
1.160     dyoung   1435:        case SIOCSIFMEDIA:
                   1436:        case SIOCGIFMEDIA:
                   1437:                if (ec->ec_mii == NULL)
1.169     dyoung   1438:                        return ENOTTY;
                   1439:                return ifmedia_ioctl(ifp, ifr, &ec->ec_mii->mii_media, cmd);
1.170     dyoung   1440:        case SIOCALIFADDR:
                   1441:                sdl = satocsdl(sstocsa(&iflr->addr));
                   1442:                if (sdl->sdl_family != AF_LINK)
                   1443:                        ;
                   1444:                else if (ETHER_IS_MULTICAST(CLLADDR(sdl)))
                   1445:                        return EINVAL;
                   1446:                else if (memcmp(zero, CLLADDR(sdl), sizeof(zero)) == 0)
                   1447:                        return EINVAL;
                   1448:                /*FALLTHROUGH*/
                   1449:        default:
1.161     dyoung   1450:                return ifioctl_common(ifp, cmd, data);
1.66      thorpej  1451:        }
1.169     dyoung   1452:        return 0;
1.3       hpeyerl  1453: }
1.200     joerg    1454:
1.215     christos 1455: /*
                   1456:  * Enable/disable passing VLAN packets if the parent interface supports it.
                   1457:  * Return:
                   1458:  *      0: Ok
                   1459:  *     -1: Parent interface does not support vlans
                   1460:  *     >0: Error
                   1461:  */
                   1462: int
                   1463: ether_enable_vlan_mtu(struct ifnet *ifp)
                   1464: {
                   1465:        int error;
                   1466:        struct ethercom *ec = (void *)ifp;
                   1467:
                   1468:        /* Parent does not support VLAN's */
                   1469:        if ((ec->ec_capabilities & ETHERCAP_VLAN_MTU) == 0)
                   1470:                return -1;
                   1471:
                   1472:        /*
                   1473:         * Parent supports the VLAN_MTU capability,
                   1474:         * i.e. can Tx/Rx larger than ETHER_MAX_LEN frames;
                   1475:         * enable it.
                   1476:         */
                   1477:        ec->ec_capenable |= ETHERCAP_VLAN_MTU;
                   1478:
                   1479:        /* Interface is down, defer for later */
                   1480:        if ((ifp->if_flags & IFF_UP) == 0)
                   1481:                return 0;
                   1482:
                   1483:        if ((error = if_flags_set(ifp, ifp->if_flags)) == 0)
                   1484:                return 0;
                   1485:
                   1486:        ec->ec_capenable &= ~ETHERCAP_VLAN_MTU;
                   1487:        return error;
                   1488: }
                   1489:
                   1490: int
                   1491: ether_disable_vlan_mtu(struct ifnet *ifp)
                   1492: {
                   1493:        int error;
                   1494:        struct ethercom *ec = (void *)ifp;
                   1495:
                   1496:        /* We still have VLAN's, defer for later */
                   1497:        if (ec->ec_nvlans != 0)
                   1498:                return 0;
                   1499:
                   1500:        /* Parent does not support VLAB's, nothing to do. */
                   1501:        if ((ec->ec_capenable & ETHERCAP_VLAN_MTU) == 0)
                   1502:                return -1;
                   1503:
                   1504:        /*
                   1505:         * Disable Tx/Rx of VLAN-sized frames.
                   1506:         */
                   1507:        ec->ec_capenable &= ~ETHERCAP_VLAN_MTU;
1.250     maxv     1508:
1.215     christos 1509:        /* Interface is down, defer for later */
                   1510:        if ((ifp->if_flags & IFF_UP) == 0)
                   1511:                return 0;
                   1512:
                   1513:        if ((error = if_flags_set(ifp, ifp->if_flags)) == 0)
                   1514:                return 0;
                   1515:
                   1516:        ec->ec_capenable |= ETHERCAP_VLAN_MTU;
                   1517:        return error;
                   1518: }
                   1519:
1.200     joerg    1520: static int
                   1521: ether_multicast_sysctl(SYSCTLFN_ARGS)
                   1522: {
                   1523:        struct ether_multi *enm;
                   1524:        struct ifnet *ifp;
                   1525:        struct ethercom *ec;
1.223     ozaki-r  1526:        int error = 0;
1.200     joerg    1527:        size_t written;
1.223     ozaki-r  1528:        struct psref psref;
1.237     skrll    1529:        int bound;
1.233     ozaki-r  1530:        unsigned int multicnt;
                   1531:        struct ether_multi_sysctl *addrs;
                   1532:        int i;
1.200     joerg    1533:
                   1534:        if (namelen != 1)
                   1535:                return EINVAL;
                   1536:
1.223     ozaki-r  1537:        bound = curlwp_bind();
                   1538:        ifp = if_get_byindex(name[0], &psref);
                   1539:        if (ifp == NULL) {
                   1540:                error = ENODEV;
                   1541:                goto out;
                   1542:        }
1.200     joerg    1543:        if (ifp->if_type != IFT_ETHER) {
1.223     ozaki-r  1544:                if_put(ifp, &psref);
1.200     joerg    1545:                *oldlenp = 0;
1.223     ozaki-r  1546:                goto out;
1.200     joerg    1547:        }
                   1548:        ec = (struct ethercom *)ifp;
                   1549:
                   1550:        if (oldp == NULL) {
1.223     ozaki-r  1551:                if_put(ifp, &psref);
1.233     ozaki-r  1552:                *oldlenp = ec->ec_multicnt * sizeof(*addrs);
1.223     ozaki-r  1553:                goto out;
1.200     joerg    1554:        }
                   1555:
1.233     ozaki-r  1556:        /*
                   1557:         * ec->ec_lock is a spin mutex so we cannot call sysctl_copyout, which
1.251     maxv     1558:         * is sleepable, while holding it. Copy data to a local buffer first
                   1559:         * with the lock taken and then call sysctl_copyout without holding it.
1.233     ozaki-r  1560:         */
                   1561: retry:
                   1562:        multicnt = ec->ec_multicnt;
1.251     maxv     1563:
                   1564:        if (multicnt == 0) {
                   1565:                if_put(ifp, &psref);
                   1566:                *oldlenp = 0;
                   1567:                goto out;
                   1568:        }
                   1569:
1.252   ! maxv     1570:        addrs = kmem_zalloc(sizeof(*addrs) * multicnt, KM_SLEEP);
1.200     joerg    1571:
1.245     msaitoh  1572:        ETHER_LOCK(ec);
1.251     maxv     1573:        if (multicnt != ec->ec_multicnt) {
                   1574:                /* The number of multicast addresses has changed */
1.245     msaitoh  1575:                ETHER_UNLOCK(ec);
1.233     ozaki-r  1576:                kmem_free(addrs, sizeof(*addrs) * multicnt);
                   1577:                goto retry;
                   1578:        }
                   1579:
                   1580:        i = 0;
1.200     joerg    1581:        LIST_FOREACH(enm, &ec->ec_multiaddrs, enm_list) {
1.233     ozaki-r  1582:                struct ether_multi_sysctl *addr = &addrs[i];
                   1583:                addr->enm_refcount = enm->enm_refcount;
                   1584:                memcpy(addr->enm_addrlo, enm->enm_addrlo, ETHER_ADDR_LEN);
                   1585:                memcpy(addr->enm_addrhi, enm->enm_addrhi, ETHER_ADDR_LEN);
                   1586:                i++;
                   1587:        }
1.245     msaitoh  1588:        ETHER_UNLOCK(ec);
1.233     ozaki-r  1589:
                   1590:        error = 0;
                   1591:        written = 0;
                   1592:        for (i = 0; i < multicnt; i++) {
                   1593:                struct ether_multi_sysctl *addr = &addrs[i];
                   1594:
                   1595:                if (written + sizeof(*addr) > *oldlenp)
1.200     joerg    1596:                        break;
1.233     ozaki-r  1597:                error = sysctl_copyout(l, addr, oldp, sizeof(*addr));
1.200     joerg    1598:                if (error)
                   1599:                        break;
1.233     ozaki-r  1600:                written += sizeof(*addr);
                   1601:                oldp = (char *)oldp + sizeof(*addr);
1.200     joerg    1602:        }
1.233     ozaki-r  1603:        kmem_free(addrs, sizeof(*addrs) * multicnt);
                   1604:
1.223     ozaki-r  1605:        if_put(ifp, &psref);
1.200     joerg    1606:
                   1607:        *oldlenp = written;
1.223     ozaki-r  1608: out:
                   1609:        curlwp_bindx(bound);
1.200     joerg    1610:        return error;
                   1611: }
                   1612:
1.234     ozaki-r  1613: static void
                   1614: ether_sysctl_setup(struct sysctllog **clog)
1.200     joerg    1615: {
                   1616:        const struct sysctlnode *rnode = NULL;
                   1617:
                   1618:        sysctl_createv(clog, 0, NULL, &rnode,
                   1619:                       CTLFLAG_PERMANENT,
                   1620:                       CTLTYPE_NODE, "ether",
                   1621:                       SYSCTL_DESCR("Ethernet-specific information"),
                   1622:                       NULL, 0, NULL, 0,
                   1623:                       CTL_NET, CTL_CREATE, CTL_EOL);
                   1624:
                   1625:        sysctl_createv(clog, 0, &rnode, NULL,
                   1626:                       CTLFLAG_PERMANENT,
                   1627:                       CTLTYPE_NODE, "multicast",
                   1628:                       SYSCTL_DESCR("multicast addresses"),
                   1629:                       ether_multicast_sysctl, 0, NULL, 0,
                   1630:                       CTL_CREATE, CTL_EOL);
                   1631: }
1.203     ozaki-r  1632:
                   1633: void
                   1634: etherinit(void)
                   1635: {
1.234     ozaki-r  1636:
1.203     ozaki-r  1637:        mutex_init(&bigpktpps_lock, MUTEX_DEFAULT, IPL_NET);
1.234     ozaki-r  1638:        ether_sysctl_setup(NULL);
1.203     ozaki-r  1639: }

CVSweb <webmaster@jp.NetBSD.org>