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

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

CVSweb <webmaster@jp.NetBSD.org>