The Design and Implementation of the FreeBSD Operating System, Second Edition
Now available: The Design and Implementation of the FreeBSD Operating System (Second Edition)


[ source navigation ] [ diff markup ] [ identifier search ] [ freetext search ] [ file search ] [ list types ] [ track identifier ]

FreeBSD/Linux Kernel Cross Reference
sys/net/if_ethersubr.c

Version: -  FREEBSD  -  FREEBSD-13-STABLE  -  FREEBSD-13-0  -  FREEBSD-12-STABLE  -  FREEBSD-12-0  -  FREEBSD-11-STABLE  -  FREEBSD-11-0  -  FREEBSD-10-STABLE  -  FREEBSD-10-0  -  FREEBSD-9-STABLE  -  FREEBSD-9-0  -  FREEBSD-8-STABLE  -  FREEBSD-8-0  -  FREEBSD-7-STABLE  -  FREEBSD-7-0  -  FREEBSD-6-STABLE  -  FREEBSD-6-0  -  FREEBSD-5-STABLE  -  FREEBSD-5-0  -  FREEBSD-4-STABLE  -  FREEBSD-3-STABLE  -  FREEBSD22  -  l41  -  OPENBSD  -  linux-2.6  -  MK84  -  PLAN9  -  xnu-8792 
SearchContext: -  none  -  3  -  10 

    1 /*      $NetBSD: if_ethersubr.c,v 1.323 2022/11/15 10:47:39 roy Exp $   */
    2 
    3 /*
    4  * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
    5  * All rights reserved.
    6  *
    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.
   18  *
   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  */
   31 
   32 /*
   33  * Copyright (c) 1982, 1989, 1993
   34  *      The Regents of the University of California.  All rights reserved.
   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.
   44  * 3. Neither the name of the University nor the names of its contributors
   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  *
   60  *      @(#)if_ethersubr.c      8.2 (Berkeley) 4/4/96
   61  */
   62 
   63 #include <sys/cdefs.h>
   64 __KERNEL_RCSID(0, "$NetBSD: if_ethersubr.c,v 1.323 2022/11/15 10:47:39 roy Exp $");
   65 
   66 #ifdef _KERNEL_OPT
   67 #include "opt_inet.h"
   68 #include "opt_atalk.h"
   69 #include "opt_mbuftrace.h"
   70 #include "opt_mpls.h"
   71 #include "opt_gateway.h"
   72 #include "opt_pppoe.h"
   73 #include "opt_net_mpsafe.h"
   74 #endif
   75 
   76 #include "vlan.h"
   77 #include "pppoe.h"
   78 #include "bridge.h"
   79 #include "arp.h"
   80 #include "agr.h"
   81 
   82 #include <sys/sysctl.h>
   83 #include <sys/mbuf.h>
   84 #include <sys/mutex.h>
   85 #include <sys/ioctl.h>
   86 #include <sys/errno.h>
   87 #include <sys/device.h>
   88 #include <sys/entropy.h>
   89 #include <sys/rndsource.h>
   90 #include <sys/cpu.h>
   91 #include <sys/kmem.h>
   92 #include <sys/hook.h>
   93 
   94 #include <net/if.h>
   95 #include <net/route.h>
   96 #include <net/if_llc.h>
   97 #include <net/if_dl.h>
   98 #include <net/if_types.h>
   99 #include <net/pktqueue.h>
  100 
  101 #include <net/if_media.h>
  102 #include <dev/mii/mii.h>
  103 #include <dev/mii/miivar.h>
  104 
  105 #if NARP == 0
  106 /*
  107  * XXX there should really be a way to issue this warning from within config(8)
  108  */
  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.
  110 #endif
  111 
  112 #include <net/bpf.h>
  113 
  114 #include <net/if_ether.h>
  115 #include <net/if_vlanvar.h>
  116 
  117 #if NPPPOE > 0
  118 #include <net/if_pppoe.h>
  119 #endif
  120 
  121 #if NAGR > 0
  122 #include <net/ether_slowprotocols.h>
  123 #include <net/agr/ieee8023ad.h>
  124 #include <net/agr/if_agrvar.h>
  125 #endif
  126 
  127 #if NBRIDGE > 0
  128 #include <net/if_bridgevar.h>
  129 #endif
  130 
  131 #include <netinet/in.h>
  132 #ifdef INET
  133 #include <netinet/in_var.h>
  134 #endif
  135 #include <netinet/if_inarp.h>
  136 
  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 
  145 #include "carp.h"
  146 #if NCARP > 0
  147 #include <netinet/ip_carp.h>
  148 #endif
  149 
  150 #ifdef NETATALK
  151 #include <netatalk/at.h>
  152 #include <netatalk/at_var.h>
  153 #include <netatalk/at_extern.h>
  154 
  155 #define llc_snap_org_code llc_un.type_snap.org_code
  156 #define llc_snap_ether_type llc_un.type_snap.ether_type
  157 
  158 extern u_char   at_org_code[3];
  159 extern u_char   aarp_org_code[3];
  160 #endif /* NETATALK */
  161 
  162 #ifdef MPLS
  163 #include <netmpls/mpls.h>
  164 #include <netmpls/mpls_var.h>
  165 #endif
  166 
  167 CTASSERT(sizeof(struct ether_addr) == 6);
  168 CTASSERT(sizeof(struct ether_header) == 14);
  169 
  170 #ifdef DIAGNOSTIC
  171 static struct timeval bigpktppslim_last;
  172 static int bigpktppslim = 2;    /* XXX */
  173 static int bigpktpps_count;
  174 static kmutex_t bigpktpps_lock __cacheline_aligned;
  175 #endif
  176 
  177 const uint8_t etherbroadcastaddr[ETHER_ADDR_LEN] =
  178     { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
  179 const uint8_t ethermulticastaddr_slowprotocols[ETHER_ADDR_LEN] =
  180     { 0x01, 0x80, 0xc2, 0x00, 0x00, 0x02 };
  181 #define senderr(e) { error = (e); goto bad;}
  182 
  183 static pktq_rps_hash_func_t ether_pktq_rps_hash_p;
  184 
  185 static int ether_output(struct ifnet *, struct mbuf *,
  186     const struct sockaddr *, const struct rtentry *);
  187 
  188 /*
  189  * Ethernet output routine.
  190  * Encapsulate a packet of type family for the local net.
  191  * Assumes that ifp is actually pointer to ethercom structure.
  192  */
  193 static int
  194 ether_output(struct ifnet * const ifp0, struct mbuf * const m0,
  195     const struct sockaddr * const dst, const struct rtentry *rt)
  196 {
  197         uint8_t esrc[ETHER_ADDR_LEN], edst[ETHER_ADDR_LEN];
  198         uint16_t etype = 0;
  199         int error = 0, hdrcmplt = 0;
  200         struct mbuf *m = m0;
  201         struct mbuf *mcopy = NULL;
  202         struct ether_header *eh;
  203         struct ifnet *ifp = ifp0;
  204 #ifdef INET
  205         struct arphdr *ah;
  206 #endif
  207 #ifdef NETATALK
  208         struct at_ifaddr *aa;
  209 #endif
  210 
  211 #ifdef MBUFTRACE
  212         m_claimm(m, ifp->if_mowner);
  213 #endif
  214 
  215 #if NCARP > 0
  216         if (ifp->if_type == IFT_CARP) {
  217                 struct ifaddr *ifa;
  218                 int s = pserialize_read_enter();
  219 
  220                 /* loop back if this is going to the carp interface */
  221                 if (dst != NULL && ifp0->if_link_state == LINK_STATE_UP &&
  222                     (ifa = ifa_ifwithaddr(dst)) != NULL) {
  223                         if (ifa->ifa_ifp == ifp0) {
  224                                 pserialize_read_exit(s);
  225                                 return looutput(ifp0, m, dst, rt);
  226                         }
  227                 }
  228                 pserialize_read_exit(s);
  229 
  230                 ifp = ifp->if_carpdev;
  231                 /* ac = (struct arpcom *)ifp; */
  232 
  233                 if ((ifp0->if_flags & (IFF_UP | IFF_RUNNING)) !=
  234                     (IFF_UP | IFF_RUNNING))
  235                         senderr(ENETDOWN);
  236         }
  237 #endif
  238 
  239         if ((ifp->if_flags & (IFF_UP | IFF_RUNNING)) != (IFF_UP | IFF_RUNNING))
  240                 senderr(ENETDOWN);
  241 
  242         switch (dst->sa_family) {
  243 
  244 #ifdef INET
  245         case AF_INET:
  246                 if (m->m_flags & M_BCAST) {
  247                         memcpy(edst, etherbroadcastaddr, sizeof(edst));
  248                 } else if (m->m_flags & M_MCAST) {
  249                         ETHER_MAP_IP_MULTICAST(&satocsin(dst)->sin_addr, edst);
  250                 } else {
  251                         error = arpresolve(ifp0, rt, m, dst, edst, sizeof(edst));
  252                         if (error)
  253                                 return (error == EWOULDBLOCK) ? 0 : error;
  254                 }
  255                 /* If broadcasting on a simplex interface, loopback a copy */
  256                 if ((m->m_flags & M_BCAST) && (ifp->if_flags & IFF_SIMPLEX))
  257                         mcopy = m_copypacket(m, M_DONTWAIT);
  258                 etype = htons(ETHERTYPE_IP);
  259                 break;
  260 
  261         case AF_ARP:
  262                 ah = mtod(m, struct arphdr *);
  263                 if (m->m_flags & M_BCAST) {
  264                         memcpy(edst, etherbroadcastaddr, sizeof(edst));
  265                 } else {
  266                         void *tha = ar_tha(ah);
  267 
  268                         if (tha == NULL) {
  269                                 /* fake with ARPHRD_IEEE1394 */
  270                                 m_freem(m);
  271                                 return 0;
  272                         }
  273                         memcpy(edst, tha, sizeof(edst));
  274                 }
  275 
  276                 ah->ar_hrd = htons(ARPHRD_ETHER);
  277 
  278                 switch (ntohs(ah->ar_op)) {
  279                 case ARPOP_REVREQUEST:
  280                 case ARPOP_REVREPLY:
  281                         etype = htons(ETHERTYPE_REVARP);
  282                         break;
  283 
  284                 case ARPOP_REQUEST:
  285                 case ARPOP_REPLY:
  286                 default:
  287                         etype = htons(ETHERTYPE_ARP);
  288                 }
  289                 break;
  290 #endif
  291 
  292 #ifdef INET6
  293         case AF_INET6:
  294                 if (m->m_flags & M_BCAST) {
  295                         memcpy(edst, etherbroadcastaddr, sizeof(edst));
  296                 } else if (m->m_flags & M_MCAST) {
  297                         ETHER_MAP_IPV6_MULTICAST(&satocsin6(dst)->sin6_addr,
  298                             edst);
  299                 } else {
  300                         error = nd6_resolve(ifp0, rt, m, dst, edst,
  301                             sizeof(edst));
  302                         if (error)
  303                                 return (error == EWOULDBLOCK) ? 0 : error;
  304                 }
  305                 etype = htons(ETHERTYPE_IPV6);
  306                 break;
  307 #endif
  308 
  309 #ifdef NETATALK
  310         case AF_APPLETALK: {
  311                 struct ifaddr *ifa;
  312                 int s;
  313 
  314                 KERNEL_LOCK(1, NULL);
  315 
  316                 if (!aarpresolve(ifp, m, (const struct sockaddr_at *)dst, edst)) {
  317                         KERNEL_UNLOCK_ONE(NULL);
  318                         return 0;
  319                 }
  320 
  321                 /*
  322                  * ifaddr is the first thing in at_ifaddr
  323                  */
  324                 s = pserialize_read_enter();
  325                 ifa = at_ifawithnet((const struct sockaddr_at *)dst, ifp);
  326                 if (ifa == NULL) {
  327                         pserialize_read_exit(s);
  328                         KERNEL_UNLOCK_ONE(NULL);
  329                         senderr(EADDRNOTAVAIL);
  330                 }
  331                 aa = (struct at_ifaddr *)ifa;
  332 
  333                 /*
  334                  * In the phase 2 case, we need to prepend an mbuf for the
  335                  * llc header.
  336                  */
  337                 if (aa->aa_flags & AFA_PHASE2) {
  338                         struct llc llc;
  339 
  340                         M_PREPEND(m, sizeof(struct llc), M_DONTWAIT);
  341                         if (m == NULL) {
  342                                 pserialize_read_exit(s);
  343                                 KERNEL_UNLOCK_ONE(NULL);
  344                                 senderr(ENOBUFS);
  345                         }
  346 
  347                         llc.llc_dsap = llc.llc_ssap = LLC_SNAP_LSAP;
  348                         llc.llc_control = LLC_UI;
  349                         memcpy(llc.llc_snap_org_code, at_org_code,
  350                             sizeof(llc.llc_snap_org_code));
  351                         llc.llc_snap_ether_type = htons(ETHERTYPE_ATALK);
  352                         memcpy(mtod(m, void *), &llc, sizeof(struct llc));
  353                 } else {
  354                         etype = htons(ETHERTYPE_ATALK);
  355                 }
  356                 pserialize_read_exit(s);
  357                 KERNEL_UNLOCK_ONE(NULL);
  358                 break;
  359         }
  360 #endif /* NETATALK */
  361 
  362         case pseudo_AF_HDRCMPLT:
  363                 hdrcmplt = 1;
  364                 memcpy(esrc,
  365                     ((const struct ether_header *)dst->sa_data)->ether_shost,
  366                     sizeof(esrc));
  367                 /* FALLTHROUGH */
  368 
  369         case AF_UNSPEC:
  370                 memcpy(edst,
  371                     ((const struct ether_header *)dst->sa_data)->ether_dhost,
  372                     sizeof(edst));
  373                 /* AF_UNSPEC doesn't swap the byte order of the ether_type. */
  374                 etype = ((const struct ether_header *)dst->sa_data)->ether_type;
  375                 break;
  376 
  377         default:
  378                 printf("%s: can't handle af%d\n", ifp->if_xname,
  379                     dst->sa_family);
  380                 senderr(EAFNOSUPPORT);
  381         }
  382 
  383 #ifdef MPLS
  384         {
  385                 struct m_tag *mtag;
  386                 mtag = m_tag_find(m, PACKET_TAG_MPLS);
  387                 if (mtag != NULL) {
  388                         /* Having the tag itself indicates it's MPLS */
  389                         etype = htons(ETHERTYPE_MPLS);
  390                         m_tag_delete(m, mtag);
  391                 }
  392         }
  393 #endif
  394 
  395         if (mcopy)
  396                 (void)looutput(ifp, mcopy, dst, rt);
  397 
  398         KASSERT((m->m_flags & M_PKTHDR) != 0);
  399 
  400         /*
  401          * If no ether type is set, this must be a 802.2 formatted packet.
  402          */
  403         if (etype == 0)
  404                 etype = htons(m->m_pkthdr.len);
  405 
  406         /*
  407          * Add local net header. If no space in first mbuf, allocate another.
  408          */
  409         M_PREPEND(m, sizeof(struct ether_header), M_DONTWAIT);
  410         if (m == NULL)
  411                 senderr(ENOBUFS);
  412 
  413         eh = mtod(m, struct ether_header *);
  414         /* Note: etype is already in network byte order. */
  415         memcpy(&eh->ether_type, &etype, sizeof(eh->ether_type));
  416         memcpy(eh->ether_dhost, edst, sizeof(edst));
  417         if (hdrcmplt) {
  418                 memcpy(eh->ether_shost, esrc, sizeof(eh->ether_shost));
  419         } else {
  420                 memcpy(eh->ether_shost, CLLADDR(ifp->if_sadl),
  421                     sizeof(eh->ether_shost));
  422         }
  423 
  424 #if NCARP > 0
  425         if (ifp0 != ifp && ifp0->if_type == IFT_CARP) {
  426                 memcpy(eh->ether_shost, CLLADDR(ifp0->if_sadl),
  427                     sizeof(eh->ether_shost));
  428         }
  429 #endif
  430 
  431         if ((error = pfil_run_hooks(ifp->if_pfil, &m, ifp, PFIL_OUT)) != 0)
  432                 return error;
  433         if (m == NULL)
  434                 return 0;
  435 
  436 #if NBRIDGE > 0
  437         /*
  438          * Bridges require special output handling.
  439          */
  440         if (ifp->if_bridge)
  441                 return bridge_output(ifp, m, NULL, NULL);
  442 #endif
  443 
  444 #if NCARP > 0
  445         if (ifp != ifp0)
  446                 if_statadd(ifp0, if_obytes, m->m_pkthdr.len + ETHER_HDR_LEN);
  447 #endif
  448 
  449 #ifdef ALTQ
  450         KERNEL_LOCK(1, NULL);
  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))
  458                 altq_etherclassify(&ifp->if_snd, m);
  459         KERNEL_UNLOCK_ONE(NULL);
  460 #endif
  461         return ifq_enqueue(ifp, m);
  462 
  463 bad:
  464         if_statinc(ifp, if_oerrors);
  465         if (m)
  466                 m_freem(m);
  467         return error;
  468 }
  469 
  470 #ifdef ALTQ
  471 /*
  472  * This routine is a slight hack to allow a packet to be classified
  473  * if the Ethernet headers are present.  It will go away when ALTQ's
  474  * classification engine understands link headers.
  475  *
  476  * XXX: We may need to do m_pullups here. First to ensure struct ether_header
  477  * is indeed contiguous, then to read the LLC and so on.
  478  */
  479 void
  480 altq_etherclassify(struct ifaltq *ifq, struct mbuf *m)
  481 {
  482         struct ether_header *eh;
  483         struct mbuf *mtop = m;
  484         uint16_t ether_type;
  485         int hlen, af, hdrsize;
  486         void *hdr;
  487 
  488         KASSERT((mtop->m_flags & M_PKTHDR) != 0);
  489 
  490         hlen = ETHER_HDR_LEN;
  491         eh = mtod(m, struct ether_header *);
  492 
  493         ether_type = htons(eh->ether_type);
  494 
  495         if (ether_type < ETHERMTU) {
  496                 /* LLC/SNAP */
  497                 struct llc *llc = (struct llc *)(eh + 1);
  498                 hlen += 8;
  499 
  500                 if (m->m_len < hlen ||
  501                     llc->llc_dsap != LLC_SNAP_LSAP ||
  502                     llc->llc_ssap != LLC_SNAP_LSAP ||
  503                     llc->llc_control != LLC_UI) {
  504                         /* Not SNAP. */
  505                         goto bad;
  506                 }
  507 
  508                 ether_type = htons(llc->llc_un.type_snap.ether_type);
  509         }
  510 
  511         switch (ether_type) {
  512         case ETHERTYPE_IP:
  513                 af = AF_INET;
  514                 hdrsize = 20;           /* sizeof(struct ip) */
  515                 break;
  516 
  517         case ETHERTYPE_IPV6:
  518                 af = AF_INET6;
  519                 hdrsize = 40;           /* sizeof(struct ip6_hdr) */
  520                 break;
  521 
  522         default:
  523                 af = AF_UNSPEC;
  524                 hdrsize = 0;
  525                 break;
  526         }
  527 
  528         while (m->m_len <= hlen) {
  529                 hlen -= m->m_len;
  530                 m = m->m_next;
  531                 if (m == NULL)
  532                         goto bad;
  533         }
  534 
  535         if (m->m_len < (hlen + hdrsize)) {
  536                 /*
  537                  * protocol header not in a single mbuf.
  538                  * We can't cope with this situation right
  539                  * now (but it shouldn't ever happen, really, anyhow).
  540                  */
  541 #ifdef DEBUG
  542                 printf("altq_etherclassify: headers span multiple mbufs: "
  543                     "%d < %d\n", m->m_len, (hlen + hdrsize));
  544 #endif
  545                 goto bad;
  546         }
  547 
  548         m->m_data += hlen;
  549         m->m_len -= hlen;
  550 
  551         hdr = mtod(m, void *);
  552 
  553         if (ALTQ_NEEDS_CLASSIFY(ifq)) {
  554                 mtop->m_pkthdr.pattr_class =
  555                     (*ifq->altq_classify)(ifq->altq_clfier, m, af);
  556         }
  557         mtop->m_pkthdr.pattr_af = af;
  558         mtop->m_pkthdr.pattr_hdr = hdr;
  559 
  560         m->m_data -= hlen;
  561         m->m_len += hlen;
  562 
  563         return;
  564 
  565 bad:
  566         mtop->m_pkthdr.pattr_class = NULL;
  567         mtop->m_pkthdr.pattr_hdr = NULL;
  568         mtop->m_pkthdr.pattr_af = AF_UNSPEC;
  569 }
  570 #endif /* ALTQ */
  571 
  572 #if defined (LLC) || defined (NETATALK)
  573 static void
  574 ether_input_llc(struct ifnet *ifp, struct mbuf *m, struct ether_header *eh)
  575 {
  576         pktqueue_t *pktq = NULL;
  577         struct llc *l;
  578 
  579         if (m->m_len < sizeof(*eh) + sizeof(struct llc))
  580                 goto error;
  581 
  582         l = (struct llc *)(eh+1);
  583         switch (l->llc_dsap) {
  584 #ifdef NETATALK
  585         case LLC_SNAP_LSAP:
  586                 switch (l->llc_control) {
  587                 case LLC_UI:
  588                         if (l->llc_ssap != LLC_SNAP_LSAP)
  589                                 goto error;
  590 
  591                         if (memcmp(&(l->llc_snap_org_code)[0],
  592                             at_org_code, sizeof(at_org_code)) == 0 &&
  593                             ntohs(l->llc_snap_ether_type) ==
  594                             ETHERTYPE_ATALK) {
  595                                 pktq = at_pktq2;
  596                                 m_adj(m, sizeof(struct ether_header)
  597                                     + sizeof(struct llc));
  598                                 break;
  599                         }
  600 
  601                         if (memcmp(&(l->llc_snap_org_code)[0],
  602                             aarp_org_code,
  603                             sizeof(aarp_org_code)) == 0 &&
  604                             ntohs(l->llc_snap_ether_type) ==
  605                             ETHERTYPE_AARP) {
  606                                 m_adj(m, sizeof(struct ether_header)
  607                                     + sizeof(struct llc));
  608                                 aarpinput(ifp, m); /* XXX queue? */
  609                                 return;
  610                         }
  611 
  612                 default:
  613                         goto error;
  614                 }
  615                 break;
  616 #endif
  617         default:
  618                 goto noproto;
  619         }
  620 
  621         KASSERT(pktq != NULL);
  622         if (__predict_false(!pktq_enqueue(pktq, m, 0))) {
  623                 m_freem(m);
  624         }
  625         return;
  626 
  627 noproto:
  628         m_freem(m);
  629         if_statinc(ifp, if_noproto);
  630         return;
  631 error:
  632         m_freem(m);
  633         if_statinc(ifp, if_ierrors);
  634         return;
  635 }
  636 #endif /* defined (LLC) || defined (NETATALK) */
  637 
  638 /*
  639  * Process a received Ethernet packet;
  640  * the packet is in the mbuf chain m with
  641  * the ether header.
  642  */
  643 void
  644 ether_input(struct ifnet *ifp, struct mbuf *m)
  645 {
  646 #if NVLAN > 0 || defined(MBUFTRACE)
  647         struct ethercom *ec = (struct ethercom *) ifp;
  648 #endif
  649         pktqueue_t *pktq = NULL;
  650         uint16_t etype;
  651         struct ether_header *eh;
  652         size_t ehlen;
  653         static int earlypkts;
  654 
  655         /* No RPS for not-IP. */
  656         pktq_rps_hash_func_t rps_hash = NULL;
  657 
  658         KASSERT(!cpu_intr_p());
  659         KASSERT((m->m_flags & M_PKTHDR) != 0);
  660 
  661         if ((ifp->if_flags & IFF_UP) == 0)
  662                 goto drop;
  663 
  664 #ifdef MBUFTRACE
  665         m_claimm(m, &ec->ec_rx_mowner);
  666 #endif
  667 
  668         if (__predict_false(m->m_len < sizeof(*eh))) {
  669                 if ((m = m_pullup(m, sizeof(*eh))) == NULL) {
  670                         if_statinc(ifp, if_ierrors);
  671                         return;
  672                 }
  673         }
  674 
  675         eh = mtod(m, struct ether_header *);
  676         etype = ntohs(eh->ether_type);
  677         ehlen = sizeof(*eh);
  678 
  679         if (__predict_false(earlypkts < 100 ||
  680                 entropy_epoch() == (unsigned)-1)) {
  681                 rnd_add_data(NULL, eh, ehlen, 0);
  682                 earlypkts++;
  683         }
  684 
  685         /*
  686          * Determine if the packet is within its size limits. For MPLS the
  687          * header length is variable, so we skip the check.
  688          */
  689         if (etype != ETHERTYPE_MPLS && m->m_pkthdr.len >
  690             ETHER_MAX_FRAME(ifp, etype, m->m_flags & M_HASFCS)) {
  691 #ifdef DIAGNOSTIC
  692                 mutex_enter(&bigpktpps_lock);
  693                 if (ppsratecheck(&bigpktppslim_last, &bigpktpps_count,
  694                     bigpktppslim)) {
  695                         printf("%s: discarding oversize frame (len=%d)\n",
  696                             ifp->if_xname, m->m_pkthdr.len);
  697                 }
  698                 mutex_exit(&bigpktpps_lock);
  699 #endif
  700                 goto error;
  701         }
  702 
  703         if (ETHER_IS_MULTICAST(eh->ether_dhost)) {
  704                 /*
  705                  * If this is not a simplex interface, drop the packet
  706                  * if it came from us.
  707                  */
  708                 if ((ifp->if_flags & IFF_SIMPLEX) == 0 &&
  709                     memcmp(CLLADDR(ifp->if_sadl), eh->ether_shost,
  710                     ETHER_ADDR_LEN) == 0) {
  711                         goto drop;
  712                 }
  713 
  714                 if (memcmp(etherbroadcastaddr,
  715                     eh->ether_dhost, ETHER_ADDR_LEN) == 0)
  716                         m->m_flags |= M_BCAST;
  717                 else
  718                         m->m_flags |= M_MCAST;
  719                 if_statinc(ifp, if_imcasts);
  720         }
  721 
  722         /* If the CRC is still on the packet, trim it off. */
  723         if (m->m_flags & M_HASFCS) {
  724                 m_adj(m, -ETHER_CRC_LEN);
  725                 m->m_flags &= ~M_HASFCS;
  726         }
  727 
  728         if_statadd(ifp, if_ibytes, m->m_pkthdr.len);
  729 
  730         if (!vlan_has_tag(m) && etype == ETHERTYPE_VLAN) {
  731                 m = ether_strip_vlantag(m);
  732                 if (m == NULL) {
  733                         if_statinc(ifp, if_ierrors);
  734                         return;
  735                 }
  736 
  737                 eh = mtod(m, struct ether_header *);
  738                 etype = ntohs(eh->ether_type);
  739                 ehlen = sizeof(*eh);
  740         }
  741 
  742         if ((m->m_flags & (M_BCAST | M_MCAST | M_PROMISC)) == 0 &&
  743             (ifp->if_flags & IFF_PROMISC) != 0 &&
  744             memcmp(CLLADDR(ifp->if_sadl), eh->ether_dhost,
  745              ETHER_ADDR_LEN) != 0) {
  746                 m->m_flags |= M_PROMISC;
  747         }
  748 
  749         if ((m->m_flags & M_PROMISC) == 0) {
  750                 if (pfil_run_hooks(ifp->if_pfil, &m, ifp, PFIL_IN) != 0)
  751                         return;
  752                 if (m == NULL)
  753                         return;
  754 
  755                 eh = mtod(m, struct ether_header *);
  756                 etype = ntohs(eh->ether_type);
  757         }
  758 
  759         /*
  760          * Processing a logical interfaces that are able
  761          * to configure vlan(4).
  762         */
  763 #if NAGR > 0
  764         if (ifp->if_lagg != NULL &&
  765             __predict_true(etype != ETHERTYPE_SLOWPROTOCOLS)) {
  766                 m->m_flags &= ~M_PROMISC;
  767                 agr_input(ifp, m);
  768                 return;
  769         }
  770 #endif
  771 
  772         /*
  773          * VLAN processing.
  774          *
  775          * VLAN provides service delimiting so the frames are
  776          * processed before other handlings. If a VLAN interface
  777          * does not exist to take those frames, they're returned
  778          * to ether_input().
  779          */
  780 
  781         if (vlan_has_tag(m)) {
  782                 if (EVL_VLANOFTAG(vlan_get_tag(m)) == 0) {
  783                         if (etype == ETHERTYPE_VLAN ||
  784                              etype == ETHERTYPE_QINQ)
  785                                 goto drop;
  786 
  787                         /* XXX we should actually use the prio value? */
  788                         m->m_flags &= ~M_VLANTAG;
  789                 } else {
  790 #if NVLAN > 0
  791                         if (ec->ec_nvlans > 0) {
  792                                 m = vlan_input(ifp, m);
  793 
  794                                 /* vlan_input() called ether_input() recursively */
  795                                 if (m == NULL)
  796                                         return;
  797                         }
  798 #endif
  799                         /* drop VLAN frames not for this port. */
  800                         goto noproto;
  801                 }
  802         }
  803 
  804 #if NCARP > 0
  805         if (__predict_false(ifp->if_carp && ifp->if_type != IFT_CARP)) {
  806                 /*
  807                  * Clear M_PROMISC, in case the packet comes from a
  808                  * vlan.
  809                  */
  810                 m->m_flags &= ~M_PROMISC;
  811                 if (carp_input(m, (uint8_t *)&eh->ether_shost,
  812                     (uint8_t *)&eh->ether_dhost, eh->ether_type) == 0)
  813                         return;
  814         }
  815 #endif
  816 
  817         /*
  818          * Handle protocols that expect to have the Ethernet header
  819          * (and possibly FCS) intact.
  820          */
  821         switch (etype) {
  822 #if NPPPOE > 0
  823         case ETHERTYPE_PPPOEDISC:
  824                 pppoedisc_input(ifp, m);
  825                 return;
  826 
  827         case ETHERTYPE_PPPOE:
  828                 pppoe_input(ifp, m);
  829                 return;
  830 #endif
  831 
  832         case ETHERTYPE_SLOWPROTOCOLS: {
  833                 uint8_t subtype;
  834 
  835                 if (m->m_pkthdr.len < sizeof(*eh) + sizeof(subtype))
  836                         goto error;
  837 
  838                 m_copydata(m, sizeof(*eh), sizeof(subtype), &subtype);
  839                 switch (subtype) {
  840 #if NAGR > 0
  841                 case SLOWPROTOCOLS_SUBTYPE_LACP:
  842                         if (ifp->if_lagg != NULL) {
  843                                 ieee8023ad_lacp_input(ifp, m);
  844                                 return;
  845                         }
  846                         break;
  847 
  848                 case SLOWPROTOCOLS_SUBTYPE_MARKER:
  849                         if (ifp->if_lagg != NULL) {
  850                                 ieee8023ad_marker_input(ifp, m);
  851                                 return;
  852                         }
  853                         break;
  854 #endif
  855 
  856                 default:
  857                         if (subtype == 0 || subtype > 10) {
  858                                 /* illegal value */
  859                                 goto error;
  860                         }
  861                         /* unknown subtype */
  862                         break;
  863                 }
  864         }
  865         /* FALLTHROUGH */
  866         default:
  867                 if (m->m_flags & M_PROMISC)
  868                         goto drop;
  869         }
  870 
  871         /* If the CRC is still on the packet, trim it off. */
  872         if (m->m_flags & M_HASFCS) {
  873                 m_adj(m, -ETHER_CRC_LEN);
  874                 m->m_flags &= ~M_HASFCS;
  875         }
  876 
  877         /* etype represents the size of the payload in this case */
  878         if (etype <= ETHERMTU + sizeof(struct ether_header)) {
  879                 KASSERT(ehlen == sizeof(*eh));
  880 #if defined (LLC) || defined (NETATALK)
  881                 ether_input_llc(ifp, m, eh);
  882                 return;
  883 #else
  884                 /* ethertype of 0-1500 is regarded as noproto */
  885                 goto noproto;
  886 #endif
  887         }
  888 
  889         /* For ARP packets, store the source address so that
  890          * ARP DAD probes can be validated. */
  891         if (etype == ETHERTYPE_ARP) {
  892                 struct m_tag *mtag;
  893 
  894                 mtag = m_tag_get(PACKET_TAG_ETHERNET_SRC, ETHER_ADDR_LEN,
  895                     M_NOWAIT);
  896                 if (mtag != NULL) {
  897                         memcpy(mtag + 1, &eh->ether_shost, ETHER_ADDR_LEN);
  898                         m_tag_prepend(m, mtag);
  899                 }
  900         }
  901 
  902         /* Strip off the Ethernet header. */
  903         m_adj(m, ehlen);
  904 
  905         switch (etype) {
  906 #ifdef INET
  907         case ETHERTYPE_IP:
  908 #ifdef GATEWAY
  909                 if (ipflow_fastforward(m))
  910                         return;
  911 #endif
  912                 pktq = ip_pktq;
  913                 rps_hash = atomic_load_relaxed(&ether_pktq_rps_hash_p);
  914                 break;
  915 
  916         case ETHERTYPE_ARP:
  917                 pktq = arp_pktq;
  918                 break;
  919 
  920         case ETHERTYPE_REVARP:
  921                 revarpinput(m); /* XXX queue? */
  922                 return;
  923 #endif
  924 
  925 #ifdef INET6
  926         case ETHERTYPE_IPV6:
  927                 if (__predict_false(!in6_present))
  928                         goto noproto;
  929 #ifdef GATEWAY
  930                 if (ip6flow_fastforward(&m))
  931                         return;
  932 #endif
  933                 pktq = ip6_pktq;
  934                 rps_hash = atomic_load_relaxed(&ether_pktq_rps_hash_p);
  935                 break;
  936 #endif
  937 
  938 #ifdef NETATALK
  939         case ETHERTYPE_ATALK:
  940                 pktq = at_pktq1;
  941                 break;
  942 
  943         case ETHERTYPE_AARP:
  944                 aarpinput(ifp, m); /* XXX queue? */
  945                 return;
  946 #endif
  947 
  948 #ifdef MPLS
  949         case ETHERTYPE_MPLS:
  950                 pktq = mpls_pktq;
  951                 break;
  952 #endif
  953 
  954         default:
  955                 goto noproto;
  956         }
  957 
  958         KASSERT(pktq != NULL);
  959         const uint32_t h = rps_hash ? pktq_rps_hash(&rps_hash, m) : 0;
  960         if (__predict_false(!pktq_enqueue(pktq, m, h))) {
  961                 m_freem(m);
  962         }
  963         return;
  964 
  965 drop:
  966         m_freem(m);
  967         if_statinc(ifp, if_iqdrops);
  968         return;
  969 noproto:
  970         m_freem(m);
  971         if_statinc(ifp, if_noproto);
  972         return;
  973 error:
  974         m_freem(m);
  975         if_statinc(ifp, if_ierrors);
  976         return;
  977 }
  978 
  979 static void
  980 ether_bpf_mtap(struct bpf_if *bp, struct mbuf *m, u_int direction)
  981 {
  982         struct ether_vlan_header evl;
  983         struct m_hdr mh, md;
  984 
  985         KASSERT(bp != NULL);
  986 
  987         if (!vlan_has_tag(m)) {
  988                 bpf_mtap3(bp, m, direction);
  989                 return;
  990         }
  991 
  992         memcpy(&evl, mtod(m, char *), ETHER_HDR_LEN);
  993         evl.evl_proto = evl.evl_encap_proto;
  994         evl.evl_encap_proto = htons(ETHERTYPE_VLAN);
  995         evl.evl_tag = htons(vlan_get_tag(m));
  996 
  997         md.mh_flags = 0;
  998         md.mh_data = m->m_data + ETHER_HDR_LEN;
  999         md.mh_len = m->m_len - ETHER_HDR_LEN;
 1000         md.mh_next = m->m_next;
 1001 
 1002         mh.mh_flags = 0;
 1003         mh.mh_data = (char *)&evl;
 1004         mh.mh_len = sizeof(evl);
 1005         mh.mh_next = (struct mbuf *)&md;
 1006 
 1007         bpf_mtap3(bp, (struct mbuf *)&mh, direction);
 1008 }
 1009 
 1010 /*
 1011  * Convert Ethernet address to printable (loggable) representation.
 1012  */
 1013 char *
 1014 ether_sprintf(const u_char *ap)
 1015 {
 1016         static char etherbuf[3 * ETHER_ADDR_LEN];
 1017         return ether_snprintf(etherbuf, sizeof(etherbuf), ap);
 1018 }
 1019 
 1020 char *
 1021 ether_snprintf(char *buf, size_t len, const u_char *ap)
 1022 {
 1023         char *cp = buf;
 1024         size_t i;
 1025 
 1026         for (i = 0; i < len / 3; i++) {
 1027                 *cp++ = hexdigits[*ap >> 4];
 1028                 *cp++ = hexdigits[*ap++ & 0xf];
 1029                 *cp++ = ':';
 1030         }
 1031         *--cp = '\0';
 1032         return buf;
 1033 }
 1034 
 1035 /*
 1036  * Perform common duties while attaching to interface list
 1037  */
 1038 void
 1039 ether_ifattach(struct ifnet *ifp, const uint8_t *lla)
 1040 {
 1041         struct ethercom *ec = (struct ethercom *)ifp;
 1042         char xnamebuf[HOOKNAMSIZ];
 1043 
 1044         ifp->if_type = IFT_ETHER;
 1045         ifp->if_hdrlen = ETHER_HDR_LEN;
 1046         ifp->if_dlt = DLT_EN10MB;
 1047         ifp->if_mtu = ETHERMTU;
 1048         ifp->if_output = ether_output;
 1049         ifp->_if_input = ether_input;
 1050         ifp->if_bpf_mtap = ether_bpf_mtap;
 1051         if (ifp->if_baudrate == 0)
 1052                 ifp->if_baudrate = IF_Mbps(10);         /* just a default */
 1053 
 1054         if (lla != NULL)
 1055                 if_set_sadl(ifp, lla, ETHER_ADDR_LEN, !ETHER_IS_LOCAL(lla));
 1056 
 1057         LIST_INIT(&ec->ec_multiaddrs);
 1058         SIMPLEQ_INIT(&ec->ec_vids);
 1059         ec->ec_lock = mutex_obj_alloc(MUTEX_DEFAULT, IPL_NET);
 1060         ec->ec_flags = 0;
 1061         ifp->if_broadcastaddr = etherbroadcastaddr;
 1062         bpf_attach(ifp, DLT_EN10MB, sizeof(struct ether_header));
 1063         snprintf(xnamebuf, sizeof(xnamebuf),
 1064             "%s-ether_ifdetachhooks", ifp->if_xname);
 1065         ec->ec_ifdetach_hooks = simplehook_create(IPL_NET, xnamebuf);
 1066 #ifdef MBUFTRACE
 1067         mowner_init_owner(&ec->ec_tx_mowner, ifp->if_xname, "tx");
 1068         mowner_init_owner(&ec->ec_rx_mowner, ifp->if_xname, "rx");
 1069         MOWNER_ATTACH(&ec->ec_tx_mowner);
 1070         MOWNER_ATTACH(&ec->ec_rx_mowner);
 1071         ifp->if_mowner = &ec->ec_tx_mowner;
 1072 #endif
 1073 }
 1074 
 1075 void
 1076 ether_ifdetach(struct ifnet *ifp)
 1077 {
 1078         struct ethercom *ec = (void *) ifp;
 1079         struct ether_multi *enm;
 1080 
 1081         IFNET_ASSERT_UNLOCKED(ifp);
 1082         /*
 1083          * Prevent further calls to ioctl (for example turning off
 1084          * promiscuous mode from the bridge code), which eventually can
 1085          * call if_init() which can cause panics because the interface
 1086          * is in the process of being detached. Return device not configured
 1087          * instead.
 1088          */
 1089         ifp->if_ioctl = __FPTRCAST(int (*)(struct ifnet *, u_long, void *),
 1090             enxio);
 1091 
 1092         simplehook_dohooks(ec->ec_ifdetach_hooks);
 1093         KASSERT(!simplehook_has_hooks(ec->ec_ifdetach_hooks));
 1094         simplehook_destroy(ec->ec_ifdetach_hooks);
 1095 
 1096         bpf_detach(ifp);
 1097 
 1098         ETHER_LOCK(ec);
 1099         KASSERT(ec->ec_nvlans == 0);
 1100         while ((enm = LIST_FIRST(&ec->ec_multiaddrs)) != NULL) {
 1101                 LIST_REMOVE(enm, enm_list);
 1102                 kmem_free(enm, sizeof(*enm));
 1103                 ec->ec_multicnt--;
 1104         }
 1105         ETHER_UNLOCK(ec);
 1106 
 1107         mutex_obj_free(ec->ec_lock);
 1108         ec->ec_lock = NULL;
 1109 
 1110         ifp->if_mowner = NULL;
 1111         MOWNER_DETACH(&ec->ec_rx_mowner);
 1112         MOWNER_DETACH(&ec->ec_tx_mowner);
 1113 }
 1114 
 1115 void *
 1116 ether_ifdetachhook_establish(struct ifnet *ifp,
 1117     void (*fn)(void *), void *arg)
 1118 {
 1119         struct ethercom *ec;
 1120         khook_t *hk;
 1121 
 1122         if (ifp->if_type != IFT_ETHER)
 1123                 return NULL;
 1124 
 1125         ec = (struct ethercom *)ifp;
 1126         hk = simplehook_establish(ec->ec_ifdetach_hooks,
 1127             fn, arg);
 1128 
 1129         return (void *)hk;
 1130 }
 1131 
 1132 void
 1133 ether_ifdetachhook_disestablish(struct ifnet *ifp,
 1134     void *vhook, kmutex_t *lock)
 1135 {
 1136         struct ethercom *ec;
 1137 
 1138         if (vhook == NULL)
 1139                 return;
 1140 
 1141         ec = (struct ethercom *)ifp;
 1142         simplehook_disestablish(ec->ec_ifdetach_hooks, vhook, lock);
 1143 }
 1144 
 1145 #if 0
 1146 /*
 1147  * This is for reference.  We have a table-driven version
 1148  * of the little-endian crc32 generator, which is faster
 1149  * than the double-loop.
 1150  */
 1151 uint32_t
 1152 ether_crc32_le(const uint8_t *buf, size_t len)
 1153 {
 1154         uint32_t c, crc, carry;
 1155         size_t i, j;
 1156 
 1157         crc = 0xffffffffU;      /* initial value */
 1158 
 1159         for (i = 0; i < len; i++) {
 1160                 c = buf[i];
 1161                 for (j = 0; j < 8; j++) {
 1162                         carry = ((crc & 0x01) ? 1 : 0) ^ (c & 0x01);
 1163                         crc >>= 1;
 1164                         c >>= 1;
 1165                         if (carry)
 1166                                 crc = (crc ^ ETHER_CRC_POLY_LE);
 1167                 }
 1168         }
 1169 
 1170         return (crc);
 1171 }
 1172 #else
 1173 uint32_t
 1174 ether_crc32_le(const uint8_t *buf, size_t len)
 1175 {
 1176         static const uint32_t crctab[] = {
 1177                 0x00000000, 0x1db71064, 0x3b6e20c8, 0x26d930ac,
 1178                 0x76dc4190, 0x6b6b51f4, 0x4db26158, 0x5005713c,
 1179                 0xedb88320, 0xf00f9344, 0xd6d6a3e8, 0xcb61b38c,
 1180                 0x9b64c2b0, 0x86d3d2d4, 0xa00ae278, 0xbdbdf21c
 1181         };
 1182         uint32_t crc;
 1183         size_t i;
 1184 
 1185         crc = 0xffffffffU;      /* initial value */
 1186 
 1187         for (i = 0; i < len; i++) {
 1188                 crc ^= buf[i];
 1189                 crc = (crc >> 4) ^ crctab[crc & 0xf];
 1190                 crc = (crc >> 4) ^ crctab[crc & 0xf];
 1191         }
 1192 
 1193         return (crc);
 1194 }
 1195 #endif
 1196 
 1197 uint32_t
 1198 ether_crc32_be(const uint8_t *buf, size_t len)
 1199 {
 1200         uint32_t c, crc, carry;
 1201         size_t i, j;
 1202 
 1203         crc = 0xffffffffU;      /* initial value */
 1204 
 1205         for (i = 0; i < len; i++) {
 1206                 c = buf[i];
 1207                 for (j = 0; j < 8; j++) {
 1208                         carry = ((crc & 0x80000000U) ? 1 : 0) ^ (c & 0x01);
 1209                         crc <<= 1;
 1210                         c >>= 1;
 1211                         if (carry)
 1212                                 crc = (crc ^ ETHER_CRC_POLY_BE) | carry;
 1213                 }
 1214         }
 1215 
 1216         return (crc);
 1217 }
 1218 
 1219 #ifdef INET
 1220 const uint8_t ether_ipmulticast_min[ETHER_ADDR_LEN] =
 1221     { 0x01, 0x00, 0x5e, 0x00, 0x00, 0x00 };
 1222 const uint8_t ether_ipmulticast_max[ETHER_ADDR_LEN] =
 1223     { 0x01, 0x00, 0x5e, 0x7f, 0xff, 0xff };
 1224 #endif
 1225 #ifdef INET6
 1226 const uint8_t ether_ip6multicast_min[ETHER_ADDR_LEN] =
 1227     { 0x33, 0x33, 0x00, 0x00, 0x00, 0x00 };
 1228 const uint8_t ether_ip6multicast_max[ETHER_ADDR_LEN] =
 1229     { 0x33, 0x33, 0xff, 0xff, 0xff, 0xff };
 1230 #endif
 1231 
 1232 /*
 1233  * ether_aton implementation, not using a static buffer.
 1234  */
 1235 int
 1236 ether_aton_r(u_char *dest, size_t len, const char *str)
 1237 {
 1238         const u_char *cp = (const void *)str;
 1239         u_char *ep;
 1240 
 1241 #define atox(c) (((c) <= '9') ? ((c) - '') : ((toupper(c) - 'A') + 10))
 1242 
 1243         if (len < ETHER_ADDR_LEN)
 1244                 return ENOSPC;
 1245 
 1246         ep = dest + ETHER_ADDR_LEN;
 1247 
 1248         while (*cp) {
 1249                 if (!isxdigit(*cp))
 1250                         return EINVAL;
 1251 
 1252                 *dest = atox(*cp);
 1253                 cp++;
 1254                 if (isxdigit(*cp)) {
 1255                         *dest = (*dest << 4) | atox(*cp);
 1256                         cp++;
 1257                 }
 1258                 dest++;
 1259 
 1260                 if (dest == ep)
 1261                         return (*cp == '\0') ? 0 : ENAMETOOLONG;
 1262 
 1263                 switch (*cp) {
 1264                 case ':':
 1265                 case '-':
 1266                 case '.':
 1267                         cp++;
 1268                         break;
 1269                 }
 1270         }
 1271         return ENOBUFS;
 1272 }
 1273 
 1274 /*
 1275  * Convert a sockaddr into an Ethernet address or range of Ethernet
 1276  * addresses.
 1277  */
 1278 int
 1279 ether_multiaddr(const struct sockaddr *sa, uint8_t addrlo[ETHER_ADDR_LEN],
 1280     uint8_t addrhi[ETHER_ADDR_LEN])
 1281 {
 1282 #ifdef INET
 1283         const struct sockaddr_in *sin;
 1284 #endif
 1285 #ifdef INET6
 1286         const struct sockaddr_in6 *sin6;
 1287 #endif
 1288 
 1289         switch (sa->sa_family) {
 1290 
 1291         case AF_UNSPEC:
 1292                 memcpy(addrlo, sa->sa_data, ETHER_ADDR_LEN);
 1293                 memcpy(addrhi, addrlo, ETHER_ADDR_LEN);
 1294                 break;
 1295 
 1296 #ifdef INET
 1297         case AF_INET:
 1298                 sin = satocsin(sa);
 1299                 if (sin->sin_addr.s_addr == INADDR_ANY) {
 1300                         /*
 1301                          * An IP address of INADDR_ANY means listen to
 1302                          * or stop listening to all of the Ethernet
 1303                          * multicast addresses used for IP.
 1304                          * (This is for the sake of IP multicast routers.)
 1305                          */
 1306                         memcpy(addrlo, ether_ipmulticast_min, ETHER_ADDR_LEN);
 1307                         memcpy(addrhi, ether_ipmulticast_max, ETHER_ADDR_LEN);
 1308                 } else {
 1309                         ETHER_MAP_IP_MULTICAST(&sin->sin_addr, addrlo);
 1310                         memcpy(addrhi, addrlo, ETHER_ADDR_LEN);
 1311                 }
 1312                 break;
 1313 #endif
 1314 #ifdef INET6
 1315         case AF_INET6:
 1316                 sin6 = satocsin6(sa);
 1317                 if (IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) {
 1318                         /*
 1319                          * An IP6 address of 0 means listen to or stop
 1320                          * listening to all of the Ethernet multicast
 1321                          * address used for IP6.
 1322                          * (This is used for multicast routers.)
 1323                          */
 1324                         memcpy(addrlo, ether_ip6multicast_min, ETHER_ADDR_LEN);
 1325                         memcpy(addrhi, ether_ip6multicast_max, ETHER_ADDR_LEN);
 1326                 } else {
 1327                         ETHER_MAP_IPV6_MULTICAST(&sin6->sin6_addr, addrlo);
 1328                         memcpy(addrhi, addrlo, ETHER_ADDR_LEN);
 1329                 }
 1330                 break;
 1331 #endif
 1332 
 1333         default:
 1334                 return EAFNOSUPPORT;
 1335         }
 1336         return 0;
 1337 }
 1338 
 1339 /*
 1340  * Add an Ethernet multicast address or range of addresses to the list for a
 1341  * given interface.
 1342  */
 1343 int
 1344 ether_addmulti(const struct sockaddr *sa, struct ethercom *ec)
 1345 {
 1346         struct ether_multi *enm, *_enm;
 1347         u_char addrlo[ETHER_ADDR_LEN];
 1348         u_char addrhi[ETHER_ADDR_LEN];
 1349         int error = 0;
 1350 
 1351         /* Allocate out of lock */
 1352         enm = kmem_alloc(sizeof(*enm), KM_SLEEP);
 1353 
 1354         ETHER_LOCK(ec);
 1355         error = ether_multiaddr(sa, addrlo, addrhi);
 1356         if (error != 0)
 1357                 goto out;
 1358 
 1359         /*
 1360          * Verify that we have valid Ethernet multicast addresses.
 1361          */
 1362         if (!ETHER_IS_MULTICAST(addrlo) || !ETHER_IS_MULTICAST(addrhi)) {
 1363                 error = EINVAL;
 1364                 goto out;
 1365         }
 1366 
 1367         /*
 1368          * See if the address range is already in the list.
 1369          */
 1370         _enm = ether_lookup_multi(addrlo, addrhi, ec);
 1371         if (_enm != NULL) {
 1372                 /*
 1373                  * Found it; just increment the reference count.
 1374                  */
 1375                 ++_enm->enm_refcount;
 1376                 error = 0;
 1377                 goto out;
 1378         }
 1379 
 1380         /*
 1381          * Link a new multicast record into the interface's multicast list.
 1382          */
 1383         memcpy(enm->enm_addrlo, addrlo, ETHER_ADDR_LEN);
 1384         memcpy(enm->enm_addrhi, addrhi, ETHER_ADDR_LEN);
 1385         enm->enm_refcount = 1;
 1386         LIST_INSERT_HEAD(&ec->ec_multiaddrs, enm, enm_list);
 1387         ec->ec_multicnt++;
 1388 
 1389         /*
 1390          * Return ENETRESET to inform the driver that the list has changed
 1391          * and its reception filter should be adjusted accordingly.
 1392          */
 1393         error = ENETRESET;
 1394         enm = NULL;
 1395 
 1396 out:
 1397         ETHER_UNLOCK(ec);
 1398         if (enm != NULL)
 1399                 kmem_free(enm, sizeof(*enm));
 1400         return error;
 1401 }
 1402 
 1403 /*
 1404  * Delete a multicast address record.
 1405  */
 1406 int
 1407 ether_delmulti(const struct sockaddr *sa, struct ethercom *ec)
 1408 {
 1409         struct ether_multi *enm;
 1410         u_char addrlo[ETHER_ADDR_LEN];
 1411         u_char addrhi[ETHER_ADDR_LEN];
 1412         int error;
 1413 
 1414         ETHER_LOCK(ec);
 1415         error = ether_multiaddr(sa, addrlo, addrhi);
 1416         if (error != 0)
 1417                 goto error;
 1418 
 1419         /*
 1420          * Look up the address in our list.
 1421          */
 1422         enm = ether_lookup_multi(addrlo, addrhi, ec);
 1423         if (enm == NULL) {
 1424                 error = ENXIO;
 1425                 goto error;
 1426         }
 1427         if (--enm->enm_refcount != 0) {
 1428                 /*
 1429                  * Still some claims to this record.
 1430                  */
 1431                 error = 0;
 1432                 goto error;
 1433         }
 1434 
 1435         /*
 1436          * No remaining claims to this record; unlink and free it.
 1437          */
 1438         LIST_REMOVE(enm, enm_list);
 1439         ec->ec_multicnt--;
 1440         ETHER_UNLOCK(ec);
 1441         kmem_free(enm, sizeof(*enm));
 1442 
 1443         /*
 1444          * Return ENETRESET to inform the driver that the list has changed
 1445          * and its reception filter should be adjusted accordingly.
 1446          */
 1447         return ENETRESET;
 1448 
 1449 error:
 1450         ETHER_UNLOCK(ec);
 1451         return error;
 1452 }
 1453 
 1454 void
 1455 ether_set_ifflags_cb(struct ethercom *ec, ether_cb_t cb)
 1456 {
 1457         ec->ec_ifflags_cb = cb;
 1458 }
 1459 
 1460 void
 1461 ether_set_vlan_cb(struct ethercom *ec, ether_vlancb_t cb)
 1462 {
 1463 
 1464         ec->ec_vlan_cb = cb;
 1465 }
 1466 
 1467 static int
 1468 ether_ioctl_reinit(struct ethercom *ec)
 1469 {
 1470         struct ifnet *ifp = &ec->ec_if;
 1471         int error;
 1472 
 1473         KASSERTMSG(IFNET_LOCKED(ifp), "%s", ifp->if_xname);
 1474 
 1475         switch (ifp->if_flags & (IFF_UP | IFF_RUNNING)) {
 1476         case IFF_RUNNING:
 1477                 /*
 1478                  * If interface is marked down and it is running,
 1479                  * then stop and disable it.
 1480                  */
 1481                 if_stop(ifp, 1);
 1482                 break;
 1483         case IFF_UP:
 1484                 /*
 1485                  * If interface is marked up and it is stopped, then
 1486                  * start it.
 1487                  */
 1488                 return if_init(ifp);
 1489         case IFF_UP | IFF_RUNNING:
 1490                 error = 0;
 1491                 if (ec->ec_ifflags_cb != NULL) {
 1492                         error = (*ec->ec_ifflags_cb)(ec);
 1493                         if (error == ENETRESET) {
 1494                                 /*
 1495                                  * Reset the interface to pick up
 1496                                  * changes in any other flags that
 1497                                  * affect the hardware state.
 1498                                  */
 1499                                 return if_init(ifp);
 1500                         }
 1501                 } else
 1502                         error = if_init(ifp);
 1503                 return error;
 1504         case 0:
 1505                 break;
 1506         }
 1507 
 1508         return 0;
 1509 }
 1510 
 1511 /*
 1512  * Common ioctls for Ethernet interfaces.  Note, we must be
 1513  * called at splnet().
 1514  */
 1515 int
 1516 ether_ioctl(struct ifnet *ifp, u_long cmd, void *data)
 1517 {
 1518         struct ethercom *ec = (void *)ifp;
 1519         struct eccapreq *eccr;
 1520         struct ifreq *ifr = (struct ifreq *)data;
 1521         struct if_laddrreq *iflr = data;
 1522         const struct sockaddr_dl *sdl;
 1523         static const uint8_t zero[ETHER_ADDR_LEN];
 1524         int error;
 1525 
 1526         switch (cmd) {
 1527         case SIOCINITIFADDR:
 1528             {
 1529                 struct ifaddr *ifa = (struct ifaddr *)data;
 1530                 if (ifa->ifa_addr->sa_family != AF_LINK
 1531                     && (ifp->if_flags & (IFF_UP | IFF_RUNNING)) !=
 1532                        (IFF_UP | IFF_RUNNING)) {
 1533                         ifp->if_flags |= IFF_UP;
 1534                         if ((error = if_init(ifp)) != 0)
 1535                                 return error;
 1536                 }
 1537 #ifdef INET
 1538                 if (ifa->ifa_addr->sa_family == AF_INET)
 1539                         arp_ifinit(ifp, ifa);
 1540 #endif
 1541                 return 0;
 1542             }
 1543 
 1544         case SIOCSIFMTU:
 1545             {
 1546                 int maxmtu;
 1547 
 1548                 if (ec->ec_capabilities & ETHERCAP_JUMBO_MTU)
 1549                         maxmtu = ETHERMTU_JUMBO;
 1550                 else
 1551                         maxmtu = ETHERMTU;
 1552 
 1553                 if (ifr->ifr_mtu < ETHERMIN || ifr->ifr_mtu > maxmtu)
 1554                         return EINVAL;
 1555                 else if ((error = ifioctl_common(ifp, cmd, data)) != ENETRESET)
 1556                         return error;
 1557                 else if (ifp->if_flags & IFF_UP) {
 1558                         /* Make sure the device notices the MTU change. */
 1559                         return if_init(ifp);
 1560                 } else
 1561                         return 0;
 1562             }
 1563 
 1564         case SIOCSIFFLAGS:
 1565                 if ((error = ifioctl_common(ifp, cmd, data)) != 0)
 1566                         return error;
 1567                 return ether_ioctl_reinit(ec);
 1568         case SIOCGIFFLAGS:
 1569                 error = ifioctl_common(ifp, cmd, data);
 1570                 if (error == 0) {
 1571                         /* Set IFF_ALLMULTI for backcompat */
 1572                         ifr->ifr_flags |= (ec->ec_flags & ETHER_F_ALLMULTI) ?
 1573                             IFF_ALLMULTI : 0;
 1574                 }
 1575                 return error;
 1576         case SIOCGETHERCAP:
 1577                 eccr = (struct eccapreq *)data;
 1578                 eccr->eccr_capabilities = ec->ec_capabilities;
 1579                 eccr->eccr_capenable = ec->ec_capenable;
 1580                 return 0;
 1581         case SIOCSETHERCAP:
 1582                 eccr = (struct eccapreq *)data;
 1583                 if ((eccr->eccr_capenable & ~ec->ec_capabilities) != 0)
 1584                         return EINVAL;
 1585                 if (eccr->eccr_capenable == ec->ec_capenable)
 1586                         return 0;
 1587 #if 0 /* notyet */
 1588                 ec->ec_capenable = (ec->ec_capenable & ETHERCAP_CANTCHANGE)
 1589                     | (eccr->eccr_capenable & ~ETHERCAP_CANTCHANGE);
 1590 #else
 1591                 ec->ec_capenable = eccr->eccr_capenable;
 1592 #endif
 1593                 return ether_ioctl_reinit(ec);
 1594         case SIOCADDMULTI:
 1595                 return ether_addmulti(ifreq_getaddr(cmd, ifr), ec);
 1596         case SIOCDELMULTI:
 1597                 return ether_delmulti(ifreq_getaddr(cmd, ifr), ec);
 1598         case SIOCSIFMEDIA:
 1599         case SIOCGIFMEDIA:
 1600                 if (ec->ec_mii != NULL)
 1601                         return ifmedia_ioctl(ifp, ifr, &ec->ec_mii->mii_media,
 1602                             cmd);
 1603                 else if (ec->ec_ifmedia != NULL)
 1604                         return ifmedia_ioctl(ifp, ifr, ec->ec_ifmedia, cmd);
 1605                 else
 1606                         return ENOTTY;
 1607                 break;
 1608         case SIOCALIFADDR:
 1609                 sdl = satocsdl(sstocsa(&iflr->addr));
 1610                 if (sdl->sdl_family != AF_LINK)
 1611                         ;
 1612                 else if (ETHER_IS_MULTICAST(CLLADDR(sdl)))
 1613                         return EINVAL;
 1614                 else if (memcmp(zero, CLLADDR(sdl), sizeof(zero)) == 0)
 1615                         return EINVAL;
 1616                 /*FALLTHROUGH*/
 1617         default:
 1618                 return ifioctl_common(ifp, cmd, data);
 1619         }
 1620         return 0;
 1621 }
 1622 
 1623 /*
 1624  * Enable/disable passing VLAN packets if the parent interface supports it.
 1625  * Return:
 1626  *       0: Ok
 1627  *      -1: Parent interface does not support vlans
 1628  *      >0: Error
 1629  */
 1630 int
 1631 ether_enable_vlan_mtu(struct ifnet *ifp)
 1632 {
 1633         int error;
 1634         struct ethercom *ec = (void *)ifp;
 1635 
 1636         /* Parent does not support VLAN's */
 1637         if ((ec->ec_capabilities & ETHERCAP_VLAN_MTU) == 0)
 1638                 return -1;
 1639 
 1640         /*
 1641          * Parent supports the VLAN_MTU capability,
 1642          * i.e. can Tx/Rx larger than ETHER_MAX_LEN frames;
 1643          * enable it.
 1644          */
 1645         ec->ec_capenable |= ETHERCAP_VLAN_MTU;
 1646 
 1647         /* Interface is down, defer for later */
 1648         if ((ifp->if_flags & IFF_UP) == 0)
 1649                 return 0;
 1650 
 1651         if ((error = if_flags_set(ifp, ifp->if_flags)) == 0)
 1652                 return 0;
 1653 
 1654         ec->ec_capenable &= ~ETHERCAP_VLAN_MTU;
 1655         return error;
 1656 }
 1657 
 1658 int
 1659 ether_disable_vlan_mtu(struct ifnet *ifp)
 1660 {
 1661         int error;
 1662         struct ethercom *ec = (void *)ifp;
 1663 
 1664         /* We still have VLAN's, defer for later */
 1665         if (ec->ec_nvlans != 0)
 1666                 return 0;
 1667 
 1668         /* Parent does not support VLAB's, nothing to do. */
 1669         if ((ec->ec_capenable & ETHERCAP_VLAN_MTU) == 0)
 1670                 return -1;
 1671 
 1672         /*
 1673          * Disable Tx/Rx of VLAN-sized frames.
 1674          */
 1675         ec->ec_capenable &= ~ETHERCAP_VLAN_MTU;
 1676 
 1677         /* Interface is down, defer for later */
 1678         if ((ifp->if_flags & IFF_UP) == 0)
 1679                 return 0;
 1680 
 1681         if ((error = if_flags_set(ifp, ifp->if_flags)) == 0)
 1682                 return 0;
 1683 
 1684         ec->ec_capenable |= ETHERCAP_VLAN_MTU;
 1685         return error;
 1686 }
 1687 
 1688 /*
 1689  * Add and delete VLAN TAG
 1690  */
 1691 int
 1692 ether_add_vlantag(struct ifnet *ifp, uint16_t vtag, bool *vlanmtu_status)
 1693 {
 1694         struct ethercom *ec = (void *)ifp;
 1695         struct vlanid_list *vidp;
 1696         bool vlanmtu_enabled;
 1697         uint16_t vid = EVL_VLANOFTAG(vtag);
 1698         int error;
 1699 
 1700         vlanmtu_enabled = false;
 1701 
 1702         /* Add a vid to the list */
 1703         vidp = kmem_alloc(sizeof(*vidp), KM_SLEEP);
 1704         vidp->vid = vid;
 1705 
 1706         ETHER_LOCK(ec);
 1707         ec->ec_nvlans++;
 1708         SIMPLEQ_INSERT_TAIL(&ec->ec_vids, vidp, vid_list);
 1709         ETHER_UNLOCK(ec);
 1710 
 1711         if (ec->ec_nvlans == 1) {
 1712                 IFNET_LOCK(ifp);
 1713                 error = ether_enable_vlan_mtu(ifp);
 1714                 IFNET_UNLOCK(ifp);
 1715 
 1716                 if (error == 0) {
 1717                         vlanmtu_enabled = true;
 1718                 } else if (error != -1) {
 1719                         goto fail;
 1720                 }
 1721         }
 1722 
 1723         if (ec->ec_vlan_cb != NULL) {
 1724                 error = (*ec->ec_vlan_cb)(ec, vid, true);
 1725                 if (error != 0)
 1726                         goto fail;
 1727         }
 1728 
 1729         if (vlanmtu_status != NULL)
 1730                 *vlanmtu_status = vlanmtu_enabled;
 1731 
 1732         return 0;
 1733 fail:
 1734         ETHER_LOCK(ec);
 1735         ec->ec_nvlans--;
 1736         SIMPLEQ_REMOVE(&ec->ec_vids, vidp, vlanid_list, vid_list);
 1737         ETHER_UNLOCK(ec);
 1738 
 1739         if (vlanmtu_enabled) {
 1740                 IFNET_LOCK(ifp);
 1741                 (void)ether_disable_vlan_mtu(ifp);
 1742                 IFNET_UNLOCK(ifp);
 1743         }
 1744 
 1745         kmem_free(vidp, sizeof(*vidp));
 1746 
 1747         return error;
 1748 }
 1749 
 1750 int
 1751 ether_del_vlantag(struct ifnet *ifp, uint16_t vtag)
 1752 {
 1753         struct ethercom *ec = (void *)ifp;
 1754         struct vlanid_list *vidp;
 1755         uint16_t vid = EVL_VLANOFTAG(vtag);
 1756 
 1757         ETHER_LOCK(ec);
 1758         SIMPLEQ_FOREACH(vidp, &ec->ec_vids, vid_list) {
 1759                 if (vidp->vid == vid) {
 1760                         SIMPLEQ_REMOVE(&ec->ec_vids, vidp,
 1761                             vlanid_list, vid_list);
 1762                         ec->ec_nvlans--;
 1763                         break;
 1764                 }
 1765         }
 1766         ETHER_UNLOCK(ec);
 1767 
 1768         if (vidp == NULL)
 1769                 return ENOENT;
 1770 
 1771         if (ec->ec_vlan_cb != NULL) {
 1772                 (void)(*ec->ec_vlan_cb)(ec, vidp->vid, false);
 1773         }
 1774 
 1775         if (ec->ec_nvlans == 0) {
 1776                 IFNET_LOCK(ifp);
 1777                 (void)ether_disable_vlan_mtu(ifp);
 1778                 IFNET_UNLOCK(ifp);
 1779         }
 1780 
 1781         kmem_free(vidp, sizeof(*vidp));
 1782 
 1783         return 0;
 1784 }
 1785 
 1786 int
 1787 ether_inject_vlantag(struct mbuf **mp, uint16_t etype, uint16_t tag)
 1788 {
 1789         static const size_t min_data_len =
 1790             ETHER_MIN_LEN - ETHER_CRC_LEN + ETHER_VLAN_ENCAP_LEN;
 1791         /* Used to pad ethernet frames with < ETHER_MIN_LEN bytes */
 1792         static const char vlan_zero_pad_buff[ETHER_MIN_LEN] = { 0 };
 1793 
 1794         struct ether_vlan_header *evl;
 1795         struct mbuf *m = *mp;
 1796         int error;
 1797 
 1798         error = 0;
 1799 
 1800         M_PREPEND(m, ETHER_VLAN_ENCAP_LEN, M_DONTWAIT);
 1801         if (m == NULL) {
 1802                 error = ENOBUFS;
 1803                 goto out;
 1804         }
 1805 
 1806         if (m->m_len < sizeof(*evl)) {
 1807                 m = m_pullup(m, sizeof(*evl));
 1808                 if (m == NULL) {
 1809                         error = ENOBUFS;
 1810                         goto out;
 1811                 }
 1812         }
 1813 
 1814         /*
 1815          * Transform the Ethernet header into an
 1816          * Ethernet header with 802.1Q encapsulation.
 1817          */
 1818         memmove(mtod(m, void *),
 1819             mtod(m, char *) + ETHER_VLAN_ENCAP_LEN,
 1820             sizeof(struct ether_header));
 1821         evl = mtod(m, struct ether_vlan_header *);
 1822         evl->evl_proto = evl->evl_encap_proto;
 1823         evl->evl_encap_proto = htons(etype);
 1824         evl->evl_tag = htons(tag);
 1825 
 1826         /*
 1827          * To cater for VLAN-aware layer 2 ethernet
 1828          * switches which may need to strip the tag
 1829          * before forwarding the packet, make sure
 1830          * the packet+tag is at least 68 bytes long.
 1831          * This is necessary because our parent will
 1832          * only pad to 64 bytes (ETHER_MIN_LEN) and
 1833          * some switches will not pad by themselves
 1834          * after deleting a tag.
 1835          */
 1836         if (m->m_pkthdr.len < min_data_len) {
 1837                 m_copyback(m, m->m_pkthdr.len,
 1838                     min_data_len - m->m_pkthdr.len,
 1839                     vlan_zero_pad_buff);
 1840         }
 1841 
 1842         m->m_flags &= ~M_VLANTAG;
 1843 
 1844 out:
 1845         *mp = m;
 1846         return error;
 1847 }
 1848 
 1849 struct mbuf *
 1850 ether_strip_vlantag(struct mbuf *m)
 1851 {
 1852         struct ether_vlan_header *evl;
 1853 
 1854         if (m->m_len < sizeof(*evl) &&
 1855             (m = m_pullup(m, sizeof(*evl))) == NULL) {
 1856                 return NULL;
 1857         }
 1858 
 1859         if (m_makewritable(&m, 0, sizeof(*evl), M_DONTWAIT)) {
 1860                 m_freem(m);
 1861                 return NULL;
 1862         }
 1863 
 1864         evl = mtod(m, struct ether_vlan_header *);
 1865         KASSERT(ntohs(evl->evl_encap_proto) == ETHERTYPE_VLAN);
 1866 
 1867         vlan_set_tag(m, ntohs(evl->evl_tag));
 1868 
 1869         /*
 1870          * Restore the original ethertype.  We'll remove
 1871          * the encapsulation after we've found the vlan
 1872          * interface corresponding to the tag.
 1873          */
 1874         evl->evl_encap_proto = evl->evl_proto;
 1875 
 1876         /*
 1877          * Remove the encapsulation header and append tag.
 1878          * The original header has already been fixed up above.
 1879          */
 1880         vlan_set_tag(m, ntohs(evl->evl_tag));
 1881         memmove((char *)evl + ETHER_VLAN_ENCAP_LEN, evl,
 1882             offsetof(struct ether_vlan_header, evl_encap_proto));
 1883         m_adj(m, ETHER_VLAN_ENCAP_LEN);
 1884 
 1885         return m;
 1886 }
 1887 
 1888 static int
 1889 ether_multicast_sysctl(SYSCTLFN_ARGS)
 1890 {
 1891         struct ether_multi *enm;
 1892         struct ifnet *ifp;
 1893         struct ethercom *ec;
 1894         int error = 0;
 1895         size_t written;
 1896         struct psref psref;
 1897         int bound;
 1898         unsigned int multicnt;
 1899         struct ether_multi_sysctl *addrs;
 1900         int i;
 1901 
 1902         if (namelen != 1)
 1903                 return EINVAL;
 1904 
 1905         bound = curlwp_bind();
 1906         ifp = if_get_byindex(name[0], &psref);
 1907         if (ifp == NULL) {
 1908                 error = ENODEV;
 1909                 goto out;
 1910         }
 1911         if (ifp->if_type != IFT_ETHER) {
 1912                 if_put(ifp, &psref);
 1913                 *oldlenp = 0;
 1914                 goto out;
 1915         }
 1916         ec = (struct ethercom *)ifp;
 1917 
 1918         if (oldp == NULL) {
 1919                 if_put(ifp, &psref);
 1920                 *oldlenp = ec->ec_multicnt * sizeof(*addrs);
 1921                 goto out;
 1922         }
 1923 
 1924         /*
 1925          * ec->ec_lock is a spin mutex so we cannot call sysctl_copyout, which
 1926          * is sleepable, while holding it. Copy data to a local buffer first
 1927          * with the lock taken and then call sysctl_copyout without holding it.
 1928          */
 1929 retry:
 1930         multicnt = ec->ec_multicnt;
 1931 
 1932         if (multicnt == 0) {
 1933                 if_put(ifp, &psref);
 1934                 *oldlenp = 0;
 1935                 goto out;
 1936         }
 1937 
 1938         addrs = kmem_zalloc(sizeof(*addrs) * multicnt, KM_SLEEP);
 1939 
 1940         ETHER_LOCK(ec);
 1941         if (multicnt != ec->ec_multicnt) {
 1942                 /* The number of multicast addresses has changed */
 1943                 ETHER_UNLOCK(ec);
 1944                 kmem_free(addrs, sizeof(*addrs) * multicnt);
 1945                 goto retry;
 1946         }
 1947 
 1948         i = 0;
 1949         LIST_FOREACH(enm, &ec->ec_multiaddrs, enm_list) {
 1950                 struct ether_multi_sysctl *addr = &addrs[i];
 1951                 addr->enm_refcount = enm->enm_refcount;
 1952                 memcpy(addr->enm_addrlo, enm->enm_addrlo, ETHER_ADDR_LEN);
 1953                 memcpy(addr->enm_addrhi, enm->enm_addrhi, ETHER_ADDR_LEN);
 1954                 i++;
 1955         }
 1956         ETHER_UNLOCK(ec);
 1957 
 1958         error = 0;
 1959         written = 0;
 1960         for (i = 0; i < multicnt; i++) {
 1961                 struct ether_multi_sysctl *addr = &addrs[i];
 1962 
 1963                 if (written + sizeof(*addr) > *oldlenp)
 1964                         break;
 1965                 error = sysctl_copyout(l, addr, oldp, sizeof(*addr));
 1966                 if (error)
 1967                         break;
 1968                 written += sizeof(*addr);
 1969                 oldp = (char *)oldp + sizeof(*addr);
 1970         }
 1971         kmem_free(addrs, sizeof(*addrs) * multicnt);
 1972 
 1973         if_put(ifp, &psref);
 1974 
 1975         *oldlenp = written;
 1976 out:
 1977         curlwp_bindx(bound);
 1978         return error;
 1979 }
 1980 
 1981 static void
 1982 ether_sysctl_setup(struct sysctllog **clog)
 1983 {
 1984         const struct sysctlnode *rnode = NULL;
 1985 
 1986         sysctl_createv(clog, 0, NULL, &rnode,
 1987                        CTLFLAG_PERMANENT,
 1988                        CTLTYPE_NODE, "ether",
 1989                        SYSCTL_DESCR("Ethernet-specific information"),
 1990                        NULL, 0, NULL, 0,
 1991                        CTL_NET, CTL_CREATE, CTL_EOL);
 1992 
 1993         sysctl_createv(clog, 0, &rnode, NULL,
 1994                        CTLFLAG_PERMANENT,
 1995                        CTLTYPE_NODE, "multicast",
 1996                        SYSCTL_DESCR("multicast addresses"),
 1997                        ether_multicast_sysctl, 0, NULL, 0,
 1998                        CTL_CREATE, CTL_EOL);
 1999 
 2000         sysctl_createv(clog, 0, &rnode, NULL,
 2001                        CTLFLAG_PERMANENT | CTLFLAG_READWRITE,
 2002                        CTLTYPE_STRING, "rps_hash",
 2003                        SYSCTL_DESCR("Interface rps hash function control"),
 2004                        sysctl_pktq_rps_hash_handler, 0, (void *)&ether_pktq_rps_hash_p,
 2005                        PKTQ_RPS_HASH_NAME_LEN,
 2006                        CTL_CREATE, CTL_EOL);
 2007 }
 2008 
 2009 void
 2010 etherinit(void)
 2011 {
 2012 
 2013 #ifdef DIAGNOSTIC
 2014         mutex_init(&bigpktpps_lock, MUTEX_DEFAULT, IPL_NET);
 2015 #endif
 2016         ether_pktq_rps_hash_p = pktq_rps_hash_default;
 2017         ether_sysctl_setup(NULL);
 2018 }

Cache object: ac4cd792ccb35cc1d6ff4deadcd11835


[ source navigation ] [ diff markup ] [ identifier search ] [ freetext search ] [ file search ] [ list types ] [ track identifier ]


This page is part of the FreeBSD/Linux Linux Kernel Cross-Reference, and was automatically generated using a modified version of the LXR engine.