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 /*-
    2  * Copyright (c) 1982, 1989, 1993
    3  *      The Regents of the University of California.  All rights reserved.
    4  *
    5  * Redistribution and use in source and binary forms, with or without
    6  * modification, are permitted provided that the following conditions
    7  * are met:
    8  * 1. Redistributions of source code must retain the above copyright
    9  *    notice, this list of conditions and the following disclaimer.
   10  * 2. Redistributions in binary form must reproduce the above copyright
   11  *    notice, this list of conditions and the following disclaimer in the
   12  *    documentation and/or other materials provided with the distribution.
   13  * 4. Neither the name of the University nor the names of its contributors
   14  *    may be used to endorse or promote products derived from this software
   15  *    without specific prior written permission.
   16  *
   17  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
   18  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
   19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
   20  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
   21  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
   22  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
   23  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
   24  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
   25  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
   26  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
   27  * SUCH DAMAGE.
   28  *
   29  *      @(#)if_ethersubr.c      8.1 (Berkeley) 6/10/93
   30  * $FreeBSD$
   31  */
   32 
   33 #include "opt_atalk.h"
   34 #include "opt_inet.h"
   35 #include "opt_inet6.h"
   36 #include "opt_ipx.h"
   37 #include "opt_mac.h"
   38 #include "opt_netgraph.h"
   39 #include "opt_carp.h"
   40 
   41 #include <sys/param.h>
   42 #include <sys/systm.h>
   43 #include <sys/kernel.h>
   44 #include <sys/malloc.h>
   45 #include <sys/module.h>
   46 #include <sys/mbuf.h>
   47 #include <sys/random.h>
   48 #include <sys/socket.h>
   49 #include <sys/sockio.h>
   50 #include <sys/sysctl.h>
   51 
   52 #include <net/if.h>
   53 #include <net/if_arp.h>
   54 #include <net/netisr.h>
   55 #include <net/route.h>
   56 #include <net/if_llc.h>
   57 #include <net/if_dl.h>
   58 #include <net/if_types.h>
   59 #include <net/bpf.h>
   60 #include <net/ethernet.h>
   61 #include <net/if_bridgevar.h>
   62 #include <net/if_vlan_var.h>
   63 #include <net/pf_mtag.h>
   64 
   65 #if defined(INET) || defined(INET6)
   66 #include <netinet/in.h>
   67 #include <netinet/in_var.h>
   68 #include <netinet/if_ether.h>
   69 #include <netinet/ip_fw.h>
   70 #include <netinet/ip_dummynet.h>
   71 #endif
   72 #ifdef INET6
   73 #include <netinet6/nd6.h>
   74 #endif
   75 
   76 #ifdef DEV_CARP
   77 #include <netinet/ip_carp.h>
   78 #endif
   79 
   80 #ifdef IPX
   81 #include <netipx/ipx.h>
   82 #include <netipx/ipx_if.h>
   83 #endif
   84 int (*ef_inputp)(struct ifnet*, struct ether_header *eh, struct mbuf *m);
   85 int (*ef_outputp)(struct ifnet *ifp, struct mbuf **mp,
   86                 struct sockaddr *dst, short *tp, int *hlen);
   87 
   88 #ifdef NETATALK
   89 #include <netatalk/at.h>
   90 #include <netatalk/at_var.h>
   91 #include <netatalk/at_extern.h>
   92 
   93 #define llc_snap_org_code llc_un.type_snap.org_code
   94 #define llc_snap_ether_type llc_un.type_snap.ether_type
   95 
   96 extern u_char   at_org_code[3];
   97 extern u_char   aarp_org_code[3];
   98 #endif /* NETATALK */
   99 
  100 #include <security/mac/mac_framework.h>
  101 
  102 /* netgraph node hooks for ng_ether(4) */
  103 void    (*ng_ether_input_p)(struct ifnet *ifp, struct mbuf **mp);
  104 void    (*ng_ether_input_orphan_p)(struct ifnet *ifp, struct mbuf *m);
  105 int     (*ng_ether_output_p)(struct ifnet *ifp, struct mbuf **mp);
  106 void    (*ng_ether_attach_p)(struct ifnet *ifp);
  107 void    (*ng_ether_detach_p)(struct ifnet *ifp);
  108 
  109 void    (*vlan_input_p)(struct ifnet *, struct mbuf *);
  110 
  111 /* if_bridge(4) support */
  112 struct mbuf *(*bridge_input_p)(struct ifnet *, struct mbuf *); 
  113 int     (*bridge_output_p)(struct ifnet *, struct mbuf *, 
  114                 struct sockaddr *, struct rtentry *);
  115 void    (*bridge_dn_p)(struct mbuf *, struct ifnet *);
  116 
  117 /* if_lagg(4) support */
  118 struct mbuf *(*lagg_input_p)(struct ifnet *, struct mbuf *); 
  119 
  120 static const u_char etherbroadcastaddr[ETHER_ADDR_LEN] =
  121                         { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
  122 
  123 static  int ether_resolvemulti(struct ifnet *, struct sockaddr **,
  124                 struct sockaddr *);
  125 
  126 /* XXX: should be in an arp support file, not here */
  127 MALLOC_DEFINE(M_ARPCOM, "arpcom", "802.* interface internals");
  128 
  129 #define ETHER_IS_BROADCAST(addr) \
  130         (bcmp(etherbroadcastaddr, (addr), ETHER_ADDR_LEN) == 0)
  131 
  132 #define senderr(e) do { error = (e); goto bad;} while (0)
  133 
  134 #if defined(INET) || defined(INET6)
  135 int
  136 ether_ipfw_chk(struct mbuf **m0, struct ifnet *dst,
  137         struct ip_fw **rule, int shared);
  138 static int ether_ipfw;
  139 #endif
  140 
  141 /*
  142  * Ethernet output routine.
  143  * Encapsulate a packet of type family for the local net.
  144  * Use trailer local net encapsulation if enough data in first
  145  * packet leaves a multiple of 512 bytes of data in remainder.
  146  */
  147 int
  148 ether_output(struct ifnet *ifp, struct mbuf *m,
  149         struct sockaddr *dst, struct rtentry *rt0)
  150 {
  151         short type;
  152         int error, hdrcmplt = 0;
  153         u_char esrc[ETHER_ADDR_LEN], edst[ETHER_ADDR_LEN];
  154         struct ether_header *eh;
  155         struct pf_mtag *t;
  156         int loop_copy = 1;
  157         int hlen;       /* link layer header length */
  158 
  159 #ifdef MAC
  160         error = mac_check_ifnet_transmit(ifp, m);
  161         if (error)
  162                 senderr(error);
  163 #endif
  164 
  165         if (ifp->if_flags & IFF_MONITOR)
  166                 senderr(ENETDOWN);
  167         if (!((ifp->if_flags & IFF_UP) &&
  168             (ifp->if_drv_flags & IFF_DRV_RUNNING)))
  169                 senderr(ENETDOWN);
  170 
  171         hlen = ETHER_HDR_LEN;
  172         switch (dst->sa_family) {
  173 #ifdef INET
  174         case AF_INET:
  175                 error = arpresolve(ifp, rt0, m, dst, edst);
  176                 if (error)
  177                         return (error == EWOULDBLOCK ? 0 : error);
  178                 type = htons(ETHERTYPE_IP);
  179                 break;
  180         case AF_ARP:
  181         {
  182                 struct arphdr *ah;
  183                 ah = mtod(m, struct arphdr *);
  184                 ah->ar_hrd = htons(ARPHRD_ETHER);
  185 
  186                 loop_copy = 0; /* if this is for us, don't do it */
  187 
  188                 switch(ntohs(ah->ar_op)) {
  189                 case ARPOP_REVREQUEST:
  190                 case ARPOP_REVREPLY:
  191                         type = htons(ETHERTYPE_REVARP);
  192                         break;
  193                 case ARPOP_REQUEST:
  194                 case ARPOP_REPLY:
  195                 default:
  196                         type = htons(ETHERTYPE_ARP);
  197                         break;
  198                 }
  199 
  200                 if (m->m_flags & M_BCAST)
  201                         bcopy(ifp->if_broadcastaddr, edst, ETHER_ADDR_LEN);
  202                 else
  203                         bcopy(ar_tha(ah), edst, ETHER_ADDR_LEN);
  204 
  205         }
  206         break;
  207 #endif
  208 #ifdef INET6
  209         case AF_INET6:
  210                 error = nd6_storelladdr(ifp, rt0, m, dst, (u_char *)edst);
  211                 if (error)
  212                         return error;
  213                 type = htons(ETHERTYPE_IPV6);
  214                 break;
  215 #endif
  216 #ifdef IPX
  217         case AF_IPX:
  218                 if (ef_outputp) {
  219                     error = ef_outputp(ifp, &m, dst, &type, &hlen);
  220                     if (error)
  221                         goto bad;
  222                 } else
  223                     type = htons(ETHERTYPE_IPX);
  224                 bcopy((caddr_t)&(((struct sockaddr_ipx *)dst)->sipx_addr.x_host),
  225                     (caddr_t)edst, sizeof (edst));
  226                 break;
  227 #endif
  228 #ifdef NETATALK
  229         case AF_APPLETALK:
  230           {
  231             struct at_ifaddr *aa;
  232 
  233             if ((aa = at_ifawithnet((struct sockaddr_at *)dst)) == NULL)
  234                     senderr(EHOSTUNREACH); /* XXX */
  235             if (!aarpresolve(ifp, m, (struct sockaddr_at *)dst, edst))
  236                     return (0);
  237             /*
  238              * In the phase 2 case, need to prepend an mbuf for the llc header.
  239              */
  240             if ( aa->aa_flags & AFA_PHASE2 ) {
  241                 struct llc llc;
  242 
  243                 M_PREPEND(m, LLC_SNAPFRAMELEN, M_DONTWAIT);
  244                 if (m == NULL)
  245                         senderr(ENOBUFS);
  246                 llc.llc_dsap = llc.llc_ssap = LLC_SNAP_LSAP;
  247                 llc.llc_control = LLC_UI;
  248                 bcopy(at_org_code, llc.llc_snap_org_code, sizeof(at_org_code));
  249                 llc.llc_snap_ether_type = htons( ETHERTYPE_AT );
  250                 bcopy(&llc, mtod(m, caddr_t), LLC_SNAPFRAMELEN);
  251                 type = htons(m->m_pkthdr.len);
  252                 hlen = LLC_SNAPFRAMELEN + ETHER_HDR_LEN;
  253             } else {
  254                 type = htons(ETHERTYPE_AT);
  255             }
  256             break;
  257           }
  258 #endif /* NETATALK */
  259 
  260         case pseudo_AF_HDRCMPLT:
  261                 hdrcmplt = 1;
  262                 eh = (struct ether_header *)dst->sa_data;
  263                 (void)memcpy(esrc, eh->ether_shost, sizeof (esrc));
  264                 /* FALLTHROUGH */
  265 
  266         case AF_UNSPEC:
  267                 loop_copy = 0; /* if this is for us, don't do it */
  268                 eh = (struct ether_header *)dst->sa_data;
  269                 (void)memcpy(edst, eh->ether_dhost, sizeof (edst));
  270                 type = eh->ether_type;
  271                 break;
  272 
  273         default:
  274                 if_printf(ifp, "can't handle af%d\n", dst->sa_family);
  275                 senderr(EAFNOSUPPORT);
  276         }
  277 
  278         /*
  279          * Add local net header.  If no space in first mbuf,
  280          * allocate another.
  281          */
  282         M_PREPEND(m, ETHER_HDR_LEN, M_DONTWAIT);
  283         if (m == NULL)
  284                 senderr(ENOBUFS);
  285         eh = mtod(m, struct ether_header *);
  286         (void)memcpy(&eh->ether_type, &type,
  287                 sizeof(eh->ether_type));
  288         (void)memcpy(eh->ether_dhost, edst, sizeof (edst));
  289         if (hdrcmplt)
  290                 (void)memcpy(eh->ether_shost, esrc,
  291                         sizeof(eh->ether_shost));
  292         else
  293                 (void)memcpy(eh->ether_shost, IF_LLADDR(ifp),
  294                         sizeof(eh->ether_shost));
  295 
  296         /*
  297          * If a simplex interface, and the packet is being sent to our
  298          * Ethernet address or a broadcast address, loopback a copy.
  299          * XXX To make a simplex device behave exactly like a duplex
  300          * device, we should copy in the case of sending to our own
  301          * ethernet address (thus letting the original actually appear
  302          * on the wire). However, we don't do that here for security
  303          * reasons and compatibility with the original behavior.
  304          */
  305         if ((ifp->if_flags & IFF_SIMPLEX) && loop_copy &&
  306             ((t = pf_find_mtag(m)) == NULL || !t->routed)) {
  307                 int csum_flags = 0;
  308 
  309                 if (m->m_pkthdr.csum_flags & CSUM_IP)
  310                         csum_flags |= (CSUM_IP_CHECKED|CSUM_IP_VALID);
  311                 if (m->m_pkthdr.csum_flags & CSUM_DELAY_DATA)
  312                         csum_flags |= (CSUM_DATA_VALID|CSUM_PSEUDO_HDR);
  313 
  314                 if (m->m_flags & M_BCAST) {
  315                         struct mbuf *n;
  316 
  317                         /*
  318                          * Because if_simloop() modifies the packet, we need a
  319                          * writable copy through m_dup() instead of a readonly
  320                          * one as m_copy[m] would give us. The alternative would
  321                          * be to modify if_simloop() to handle the readonly mbuf,
  322                          * but performancewise it is mostly equivalent (trading
  323                          * extra data copying vs. extra locking).
  324                          *
  325                          * XXX This is a local workaround.  A number of less
  326                          * often used kernel parts suffer from the same bug.
  327                          * See PR kern/105943 for a proposed general solution.
  328                          */
  329                         if ((n = m_dup(m, M_DONTWAIT)) != NULL) {
  330                                 n->m_pkthdr.csum_flags |= csum_flags;
  331                                 if (csum_flags & CSUM_DATA_VALID)
  332                                         n->m_pkthdr.csum_data = 0xffff;
  333                                 (void)if_simloop(ifp, n, dst->sa_family, hlen);
  334                         } else
  335                                 ifp->if_iqdrops++;
  336                 } else if (bcmp(eh->ether_dhost, eh->ether_shost,
  337                                 ETHER_ADDR_LEN) == 0) {
  338                         m->m_pkthdr.csum_flags |= csum_flags;
  339                         if (csum_flags & CSUM_DATA_VALID)
  340                                 m->m_pkthdr.csum_data = 0xffff;
  341                         (void) if_simloop(ifp, m, dst->sa_family, hlen);
  342                         return (0);     /* XXX */
  343                 }
  344         }
  345 
  346        /*
  347         * Bridges require special output handling.
  348         */
  349         if (ifp->if_bridge) {
  350                 BRIDGE_OUTPUT(ifp, m, error);
  351                 return (error);
  352         }
  353 
  354 #ifdef DEV_CARP
  355         if (ifp->if_carp &&
  356             (error = carp_output(ifp, m, dst, NULL)))
  357                 goto bad;
  358 #endif
  359 
  360         /* Handle ng_ether(4) processing, if any */
  361         if (IFP2AC(ifp)->ac_netgraph != NULL) {
  362                 KASSERT(ng_ether_output_p != NULL,
  363                     ("ng_ether_output_p is NULL"));
  364                 if ((error = (*ng_ether_output_p)(ifp, &m)) != 0) {
  365 bad:                    if (m != NULL)
  366                                 m_freem(m);
  367                         return (error);
  368                 }
  369                 if (m == NULL)
  370                         return (0);
  371         }
  372 
  373         /* Continue with link-layer output */
  374         return ether_output_frame(ifp, m);
  375 }
  376 
  377 /*
  378  * Ethernet link layer output routine to send a raw frame to the device.
  379  *
  380  * This assumes that the 14 byte Ethernet header is present and contiguous
  381  * in the first mbuf (if BRIDGE'ing).
  382  */
  383 int
  384 ether_output_frame(struct ifnet *ifp, struct mbuf *m)
  385 {
  386         int error;
  387 #if defined(INET) || defined(INET6)
  388         struct ip_fw *rule = ip_dn_claim_rule(m);
  389 
  390         if (IPFW_LOADED && ether_ipfw != 0) {
  391                 if (ether_ipfw_chk(&m, ifp, &rule, 0) == 0) {
  392                         if (m) {
  393                                 m_freem(m);
  394                                 return EACCES;  /* pkt dropped */
  395                         } else
  396                                 return 0;       /* consumed e.g. in a pipe */
  397                 }
  398         }
  399 #endif
  400 
  401         /*
  402          * Queue message on interface, update output statistics if
  403          * successful, and start output if interface not yet active.
  404          */
  405         IFQ_HANDOFF(ifp, m, error);
  406         return (error);
  407 }
  408 
  409 #if defined(INET) || defined(INET6)
  410 /*
  411  * ipfw processing for ethernet packets (in and out).
  412  * The second parameter is NULL from ether_demux, and ifp from
  413  * ether_output_frame.
  414  */
  415 int
  416 ether_ipfw_chk(struct mbuf **m0, struct ifnet *dst,
  417         struct ip_fw **rule, int shared)
  418 {
  419         struct ether_header *eh;
  420         struct ether_header save_eh;
  421         struct mbuf *m;
  422         int i;
  423         struct ip_fw_args args;
  424 
  425         if (*rule != NULL && fw_one_pass)
  426                 return 1; /* dummynet packet, already partially processed */
  427 
  428         /*
  429          * I need some amt of data to be contiguous, and in case others need
  430          * the packet (shared==1) also better be in the first mbuf.
  431          */
  432         m = *m0;
  433         i = min( m->m_pkthdr.len, max_protohdr);
  434         if ( shared || m->m_len < i) {
  435                 m = m_pullup(m, i);
  436                 if (m == NULL) {
  437                         *m0 = m;
  438                         return 0;
  439                 }
  440         }
  441         eh = mtod(m, struct ether_header *);
  442         save_eh = *eh;                  /* save copy for restore below */
  443         m_adj(m, ETHER_HDR_LEN);        /* strip ethernet header */
  444 
  445         args.m = m;             /* the packet we are looking at         */
  446         args.oif = dst;         /* destination, if any                  */
  447         args.rule = *rule;      /* matching rule to restart             */
  448         args.next_hop = NULL;   /* we do not support forward yet        */
  449         args.eh = &save_eh;     /* MAC header for bridged/MAC packets   */
  450         args.inp = NULL;        /* used by ipfw uid/gid/jail rules      */
  451         i = ip_fw_chk_ptr(&args);
  452         m = args.m;
  453         if (m != NULL) {
  454                 /*
  455                  * Restore Ethernet header, as needed, in case the
  456                  * mbuf chain was replaced by ipfw.
  457                  */
  458                 M_PREPEND(m, ETHER_HDR_LEN, M_DONTWAIT);
  459                 if (m == NULL) {
  460                         *m0 = m;
  461                         return 0;
  462                 }
  463                 if (eh != mtod(m, struct ether_header *))
  464                         bcopy(&save_eh, mtod(m, struct ether_header *),
  465                                 ETHER_HDR_LEN);
  466         }
  467         *m0 = m;
  468         *rule = args.rule;
  469 
  470         if (i == IP_FW_DENY) /* drop */
  471                 return 0;
  472 
  473         KASSERT(m != NULL, ("ether_ipfw_chk: m is NULL"));
  474 
  475         if (i == IP_FW_PASS) /* a PASS rule.  */
  476                 return 1;
  477 
  478         if (DUMMYNET_LOADED && (i == IP_FW_DUMMYNET)) {
  479                 /*
  480                  * Pass the pkt to dummynet, which consumes it.
  481                  * If shared, make a copy and keep the original.
  482                  */
  483                 if (shared) {
  484                         m = m_copypacket(m, M_DONTWAIT);
  485                         if (m == NULL)
  486                                 return 0;
  487                 } else {
  488                         /*
  489                          * Pass the original to dummynet and
  490                          * nothing back to the caller
  491                          */
  492                         *m0 = NULL ;
  493                 }
  494                 ip_dn_io_ptr(&m, dst ? DN_TO_ETH_OUT: DN_TO_ETH_DEMUX, &args);
  495                 return 0;
  496         }
  497         /*
  498          * XXX at some point add support for divert/forward actions.
  499          * If none of the above matches, we have to drop the pkt.
  500          */
  501         return 0;
  502 }
  503 #endif
  504 
  505 /*
  506  * Process a received Ethernet packet; the packet is in the
  507  * mbuf chain m with the ethernet header at the front.
  508  */
  509 static void
  510 ether_input(struct ifnet *ifp, struct mbuf *m)
  511 {
  512         struct ether_header *eh;
  513         u_short etype;
  514 
  515         if ((ifp->if_flags & IFF_UP) == 0) {
  516                 m_freem(m);
  517                 return;
  518         }
  519 #ifdef DIAGNOSTIC
  520         if ((ifp->if_drv_flags & IFF_DRV_RUNNING) == 0) {
  521                 if_printf(ifp, "discard frame at !IFF_DRV_RUNNING\n");
  522                 m_freem(m);
  523                 return;
  524         }
  525 #endif
  526         /*
  527          * Do consistency checks to verify assumptions
  528          * made by code past this point.
  529          */
  530         if ((m->m_flags & M_PKTHDR) == 0) {
  531                 if_printf(ifp, "discard frame w/o packet header\n");
  532                 ifp->if_ierrors++;
  533                 m_freem(m);
  534                 return;
  535         }
  536         if (m->m_len < ETHER_HDR_LEN) {
  537                 /* XXX maybe should pullup? */
  538                 if_printf(ifp, "discard frame w/o leading ethernet "
  539                                 "header (len %u pkt len %u)\n",
  540                                 m->m_len, m->m_pkthdr.len);
  541                 ifp->if_ierrors++;
  542                 m_freem(m);
  543                 return;
  544         }
  545         eh = mtod(m, struct ether_header *);
  546         etype = ntohs(eh->ether_type);
  547         if (m->m_pkthdr.rcvif == NULL) {
  548                 if_printf(ifp, "discard frame w/o interface pointer\n");
  549                 ifp->if_ierrors++;
  550                 m_freem(m);
  551                 return;
  552         }
  553 #ifdef DIAGNOSTIC
  554         if (m->m_pkthdr.rcvif != ifp) {
  555                 if_printf(ifp, "Warning, frame marked as received on %s\n",
  556                         m->m_pkthdr.rcvif->if_xname);
  557         }
  558 #endif
  559 
  560         if (ETHER_IS_MULTICAST(eh->ether_dhost)) {
  561                 if (ETHER_IS_BROADCAST(eh->ether_dhost))
  562                         m->m_flags |= M_BCAST;
  563                 else
  564                         m->m_flags |= M_MCAST;
  565                 ifp->if_imcasts++;
  566         }
  567 
  568 #ifdef MAC
  569         /*
  570          * Tag the mbuf with an appropriate MAC label before any other
  571          * consumers can get to it.
  572          */
  573         mac_create_mbuf_from_ifnet(ifp, m);
  574 #endif
  575 
  576         /*
  577          * Give bpf a chance at the packet.
  578          */
  579         ETHER_BPF_MTAP(ifp, m);
  580 
  581         /*
  582          * If the CRC is still on the packet, trim it off. We do this once
  583          * and once only in case we are re-entered. Nothing else on the
  584          * Ethernet receive path expects to see the FCS.
  585          */
  586         if (m->m_flags & M_HASFCS) {
  587                 m_adj(m, -ETHER_CRC_LEN);
  588                 m->m_flags &= ~M_HASFCS;
  589         }
  590 
  591         ifp->if_ibytes += m->m_pkthdr.len;
  592 
  593         /* Allow monitor mode to claim this frame, after stats are updated. */
  594         if (ifp->if_flags & IFF_MONITOR) {
  595                 m_freem(m);
  596                 return;
  597         }
  598 
  599         /* Handle input from a lagg(4) port */
  600         if (ifp->if_type == IFT_IEEE8023ADLAG) {
  601                 KASSERT(lagg_input_p != NULL,
  602                     ("%s: if_lagg not loaded!", __func__));
  603                 m = (*lagg_input_p)(ifp, m);
  604                 if (m != NULL)
  605                         ifp = m->m_pkthdr.rcvif;
  606                 else 
  607                         return;
  608         }
  609 
  610         /*
  611          * If the hardware did not process an 802.1Q tag, do this now,
  612          * to allow 802.1P priority frames to be passed to the main input
  613          * path correctly.
  614          * TODO: Deal with Q-in-Q frames, but not arbitrary nesting levels.
  615          */
  616         if ((m->m_flags & M_VLANTAG) == 0 && etype == ETHERTYPE_VLAN) {
  617                 struct ether_vlan_header *evl;
  618 
  619                 if (m->m_len < sizeof(*evl) &&
  620                     (m = m_pullup(m, sizeof(*evl))) == NULL) {
  621 #ifdef DIAGNOSTIC
  622                         if_printf(ifp, "cannot pullup VLAN header\n");
  623 #endif
  624                         ifp->if_ierrors++;
  625                         m_freem(m);
  626                         return;
  627                 }
  628 
  629                 evl = mtod(m, struct ether_vlan_header *);
  630                 m->m_pkthdr.ether_vtag = ntohs(evl->evl_tag);
  631                 m->m_flags |= M_VLANTAG;
  632 
  633                 bcopy((char *)evl, (char *)evl + ETHER_VLAN_ENCAP_LEN,
  634                     ETHER_HDR_LEN - ETHER_TYPE_LEN);
  635                 m_adj(m, ETHER_VLAN_ENCAP_LEN);
  636         }
  637 
  638         M_SETFIB(m, ifp->if_fib);
  639 
  640         /* Allow ng_ether(4) to claim this frame. */
  641         if (IFP2AC(ifp)->ac_netgraph != NULL) {
  642                 KASSERT(ng_ether_input_p != NULL,
  643                     ("%s: ng_ether_input_p is NULL", __func__));
  644                 m->m_flags &= ~M_PROMISC;
  645                 (*ng_ether_input_p)(ifp, &m);
  646                 if (m == NULL)
  647                         return;
  648         }
  649 
  650         /*
  651          * Allow if_bridge(4) to claim this frame.
  652          * The BRIDGE_INPUT() macro will update ifp if the bridge changed it
  653          * and the frame should be delivered locally.
  654          */
  655         if (ifp->if_bridge != NULL) {
  656                 m->m_flags &= ~M_PROMISC;
  657                 BRIDGE_INPUT(ifp, m);
  658                 if (m == NULL)
  659                         return;
  660         }
  661 
  662 #ifdef DEV_CARP
  663         /*
  664          * Clear M_PROMISC on frame so that carp(4) will see it when the
  665          * mbuf flows up to Layer 3.
  666          * FreeBSD's implementation of carp(4) uses the inprotosw
  667          * to dispatch IPPROTO_CARP. carp(4) also allocates its own
  668          * Ethernet addresses of the form 00:00:5e:00:01:xx, which
  669          * is outside the scope of the M_PROMISC test below.
  670          * TODO: Maintain a hash table of ethernet addresses other than
  671          * ether_dhost which may be active on this ifp.
  672          */
  673         if (ifp->if_carp && carp_forus(ifp->if_carp, eh->ether_dhost)) {
  674                 m->m_flags &= ~M_PROMISC;
  675         } else
  676 #endif
  677         {
  678                 /*
  679                  * If the frame received was not for our MAC address, set the
  680                  * M_PROMISC flag on the mbuf chain. The frame may need to
  681                  * be seen by the rest of the Ethernet input path in case of
  682                  * re-entry (e.g. bridge, vlan, netgraph) but should not be
  683                  * seen by upper protocol layers.
  684                  */
  685                 if (!ETHER_IS_MULTICAST(eh->ether_dhost) &&
  686                     bcmp(IF_LLADDR(ifp), eh->ether_dhost, ETHER_ADDR_LEN) != 0)
  687                         m->m_flags |= M_PROMISC;
  688         }
  689 
  690         /* First chunk of an mbuf contains good entropy */
  691         if (harvest.ethernet)
  692                 random_harvest(m, 16, 3, 0, RANDOM_NET);
  693 
  694         ether_demux(ifp, m);
  695 }
  696 
  697 /*
  698  * Upper layer processing for a received Ethernet packet.
  699  */
  700 void
  701 ether_demux(struct ifnet *ifp, struct mbuf *m)
  702 {
  703         struct ether_header *eh;
  704         int isr;
  705         u_short ether_type;
  706 #if defined(NETATALK)
  707         struct llc *l;
  708 #endif
  709 
  710         KASSERT(ifp != NULL, ("%s: NULL interface pointer", __func__));
  711 
  712 #if defined(INET) || defined(INET6)
  713         /*
  714          * Allow dummynet and/or ipfw to claim the frame.
  715          * Do not do this for PROMISC frames in case we are re-entered.
  716          */
  717         if (IPFW_LOADED && ether_ipfw != 0 && !(m->m_flags & M_PROMISC)) {
  718                 struct ip_fw *rule = ip_dn_claim_rule(m);
  719 
  720                 if (ether_ipfw_chk(&m, NULL, &rule, 0) == 0) {
  721                         if (m)
  722                                 m_freem(m);     /* dropped; free mbuf chain */
  723                         return;                 /* consumed */
  724                 }
  725         }
  726 #endif
  727         eh = mtod(m, struct ether_header *);
  728         ether_type = ntohs(eh->ether_type);
  729 
  730         /*
  731          * If this frame has a VLAN tag other than 0, call vlan_input()
  732          * if its module is loaded. Otherwise, drop.
  733          */
  734         if ((m->m_flags & M_VLANTAG) &&
  735             EVL_VLANOFTAG(m->m_pkthdr.ether_vtag) != 0) {
  736                 if (ifp->if_vlantrunk == NULL) {
  737                         ifp->if_noproto++;
  738                         m_freem(m);
  739                         return;
  740                 }
  741                 KASSERT(vlan_input_p != NULL,("%s: VLAN not loaded!",
  742                     __func__));
  743                 /* Clear before possibly re-entering ether_input(). */
  744                 m->m_flags &= ~M_PROMISC;
  745                 (*vlan_input_p)(ifp, m);
  746                 return;
  747         }
  748 
  749         /*
  750          * Pass promiscuously received frames to the upper layer if the user
  751          * requested this by setting IFF_PPROMISC. Otherwise, drop them.
  752          */
  753         if ((ifp->if_flags & IFF_PPROMISC) == 0 && (m->m_flags & M_PROMISC)) {
  754                 m_freem(m);
  755                 return;
  756         }
  757 
  758         /*
  759          * Reset layer specific mbuf flags to avoid confusing upper layers.
  760          * Strip off Ethernet header.
  761          */
  762         m->m_flags &= ~M_VLANTAG;
  763         m->m_flags &= ~(M_PROTOFLAGS);
  764         m_adj(m, ETHER_HDR_LEN);
  765 
  766         /*
  767          * Dispatch frame to upper layer.
  768          */
  769         switch (ether_type) {
  770 #ifdef INET
  771         case ETHERTYPE_IP:
  772                 if ((m = ip_fastforward(m)) == NULL)
  773                         return;
  774                 isr = NETISR_IP;
  775                 break;
  776 
  777         case ETHERTYPE_ARP:
  778                 if (ifp->if_flags & IFF_NOARP) {
  779                         /* Discard packet if ARP is disabled on interface */
  780                         m_freem(m);
  781                         return;
  782                 }
  783                 isr = NETISR_ARP;
  784                 break;
  785 #endif
  786 #ifdef IPX
  787         case ETHERTYPE_IPX:
  788                 if (ef_inputp && ef_inputp(ifp, eh, m) == 0)
  789                         return;
  790                 isr = NETISR_IPX;
  791                 break;
  792 #endif
  793 #ifdef INET6
  794         case ETHERTYPE_IPV6:
  795                 isr = NETISR_IPV6;
  796                 break;
  797 #endif
  798 #ifdef NETATALK
  799         case ETHERTYPE_AT:
  800                 isr = NETISR_ATALK1;
  801                 break;
  802         case ETHERTYPE_AARP:
  803                 isr = NETISR_AARP;
  804                 break;
  805 #endif /* NETATALK */
  806         default:
  807 #ifdef IPX
  808                 if (ef_inputp && ef_inputp(ifp, eh, m) == 0)
  809                         return;
  810 #endif /* IPX */
  811 #if defined(NETATALK)
  812                 if (ether_type > ETHERMTU)
  813                         goto discard;
  814                 l = mtod(m, struct llc *);
  815                 if (l->llc_dsap == LLC_SNAP_LSAP &&
  816                     l->llc_ssap == LLC_SNAP_LSAP &&
  817                     l->llc_control == LLC_UI) {
  818                         if (bcmp(&(l->llc_snap_org_code)[0], at_org_code,
  819                             sizeof(at_org_code)) == 0 &&
  820                             ntohs(l->llc_snap_ether_type) == ETHERTYPE_AT) {
  821                                 m_adj(m, LLC_SNAPFRAMELEN);
  822                                 isr = NETISR_ATALK2;
  823                                 break;
  824                         }
  825                         if (bcmp(&(l->llc_snap_org_code)[0], aarp_org_code,
  826                             sizeof(aarp_org_code)) == 0 &&
  827                             ntohs(l->llc_snap_ether_type) == ETHERTYPE_AARP) {
  828                                 m_adj(m, LLC_SNAPFRAMELEN);
  829                                 isr = NETISR_AARP;
  830                                 break;
  831                         }
  832                 }
  833 #endif /* NETATALK */
  834                 goto discard;
  835         }
  836         netisr_dispatch(isr, m);
  837         return;
  838 
  839 discard:
  840         /*
  841          * Packet is to be discarded.  If netgraph is present,
  842          * hand the packet to it for last chance processing;
  843          * otherwise dispose of it.
  844          */
  845         if (IFP2AC(ifp)->ac_netgraph != NULL) {
  846                 KASSERT(ng_ether_input_orphan_p != NULL,
  847                     ("ng_ether_input_orphan_p is NULL"));
  848                 /*
  849                  * Put back the ethernet header so netgraph has a
  850                  * consistent view of inbound packets.
  851                  */
  852                 M_PREPEND(m, ETHER_HDR_LEN, M_DONTWAIT);
  853                 (*ng_ether_input_orphan_p)(ifp, m);
  854                 return;
  855         }
  856         m_freem(m);
  857 }
  858 
  859 /*
  860  * Convert Ethernet address to printable (loggable) representation.
  861  * This routine is for compatibility; it's better to just use
  862  *
  863  *      printf("%6D", <pointer to address>, ":");
  864  *
  865  * since there's no static buffer involved.
  866  */
  867 char *
  868 ether_sprintf(const u_char *ap)
  869 {
  870         static char etherbuf[18];
  871         snprintf(etherbuf, sizeof (etherbuf), "%6D", ap, ":");
  872         return (etherbuf);
  873 }
  874 
  875 /*
  876  * Perform common duties while attaching to interface list
  877  */
  878 void
  879 ether_ifattach(struct ifnet *ifp, const u_int8_t *lla)
  880 {
  881         int i;
  882         struct ifaddr *ifa;
  883         struct sockaddr_dl *sdl;
  884 
  885         ifp->if_addrlen = ETHER_ADDR_LEN;
  886         ifp->if_hdrlen = ETHER_HDR_LEN;
  887         if_attach(ifp);
  888         ifp->if_mtu = ETHERMTU;
  889         ifp->if_output = ether_output;
  890         ifp->if_input = ether_input;
  891         ifp->if_resolvemulti = ether_resolvemulti;
  892         if (ifp->if_baudrate == 0)
  893                 ifp->if_baudrate = IF_Mbps(10);         /* just a default */
  894         ifp->if_broadcastaddr = etherbroadcastaddr;
  895 
  896         ifa = ifp->if_addr;
  897         KASSERT(ifa != NULL, ("%s: no lladdr!\n", __func__));
  898         sdl = (struct sockaddr_dl *)ifa->ifa_addr;
  899         sdl->sdl_type = IFT_ETHER;
  900         sdl->sdl_alen = ifp->if_addrlen;
  901         bcopy(lla, LLADDR(sdl), ifp->if_addrlen);
  902 
  903         bpfattach(ifp, DLT_EN10MB, ETHER_HDR_LEN);
  904         if (ng_ether_attach_p != NULL)
  905                 (*ng_ether_attach_p)(ifp);
  906 
  907         /* Announce Ethernet MAC address if non-zero. */
  908         for (i = 0; i < ifp->if_addrlen; i++)
  909                 if (lla[i] != 0)
  910                         break; 
  911         if (i != ifp->if_addrlen)
  912                 if_printf(ifp, "Ethernet address: %6D\n", lla, ":");
  913 }
  914 
  915 /*
  916  * Perform common duties while detaching an Ethernet interface
  917  */
  918 void
  919 ether_ifdetach(struct ifnet *ifp)
  920 {
  921         if (IFP2AC(ifp)->ac_netgraph != NULL) {
  922                 KASSERT(ng_ether_detach_p != NULL,
  923                     ("ng_ether_detach_p is NULL"));
  924                 (*ng_ether_detach_p)(ifp);
  925         }
  926 
  927         bpfdetach(ifp);
  928         if_detach(ifp);
  929 }
  930 
  931 SYSCTL_DECL(_net_link);
  932 SYSCTL_NODE(_net_link, IFT_ETHER, ether, CTLFLAG_RW, 0, "Ethernet");
  933 #if defined(INET) || defined(INET6)
  934 SYSCTL_INT(_net_link_ether, OID_AUTO, ipfw, CTLFLAG_RW,
  935             &ether_ipfw,0,"Pass ether pkts through firewall");
  936 #endif
  937 
  938 #if 0
  939 /*
  940  * This is for reference.  We have a table-driven version
  941  * of the little-endian crc32 generator, which is faster
  942  * than the double-loop.
  943  */
  944 uint32_t
  945 ether_crc32_le(const uint8_t *buf, size_t len)
  946 {
  947         size_t i;
  948         uint32_t crc;
  949         int bit;
  950         uint8_t data;
  951 
  952         crc = 0xffffffff;       /* initial value */
  953 
  954         for (i = 0; i < len; i++) {
  955                 for (data = *buf++, bit = 0; bit < 8; bit++, data >>= 1) {
  956                         carry = (crc ^ data) & 1;
  957                         crc >>= 1;
  958                         if (carry)
  959                                 crc = (crc ^ ETHER_CRC_POLY_LE);
  960                 }
  961         }
  962 
  963         return (crc);
  964 }
  965 #else
  966 uint32_t
  967 ether_crc32_le(const uint8_t *buf, size_t len)
  968 {
  969         static const uint32_t crctab[] = {
  970                 0x00000000, 0x1db71064, 0x3b6e20c8, 0x26d930ac,
  971                 0x76dc4190, 0x6b6b51f4, 0x4db26158, 0x5005713c,
  972                 0xedb88320, 0xf00f9344, 0xd6d6a3e8, 0xcb61b38c,
  973                 0x9b64c2b0, 0x86d3d2d4, 0xa00ae278, 0xbdbdf21c
  974         };
  975         size_t i;
  976         uint32_t crc;
  977 
  978         crc = 0xffffffff;       /* initial value */
  979 
  980         for (i = 0; i < len; i++) {
  981                 crc ^= buf[i];
  982                 crc = (crc >> 4) ^ crctab[crc & 0xf];
  983                 crc = (crc >> 4) ^ crctab[crc & 0xf];
  984         }
  985 
  986         return (crc);
  987 }
  988 #endif
  989 
  990 uint32_t
  991 ether_crc32_be(const uint8_t *buf, size_t len)
  992 {
  993         size_t i;
  994         uint32_t crc, carry;
  995         int bit;
  996         uint8_t data;
  997 
  998         crc = 0xffffffff;       /* initial value */
  999 
 1000         for (i = 0; i < len; i++) {
 1001                 for (data = *buf++, bit = 0; bit < 8; bit++, data >>= 1) {
 1002                         carry = ((crc & 0x80000000) ? 1 : 0) ^ (data & 0x01);
 1003                         crc <<= 1;
 1004                         if (carry)
 1005                                 crc = (crc ^ ETHER_CRC_POLY_BE) | carry;
 1006                 }
 1007         }
 1008 
 1009         return (crc);
 1010 }
 1011 
 1012 int
 1013 ether_ioctl(struct ifnet *ifp, u_long command, caddr_t data)
 1014 {
 1015         struct ifaddr *ifa = (struct ifaddr *) data;
 1016         struct ifreq *ifr = (struct ifreq *) data;
 1017         int error = 0;
 1018 
 1019         switch (command) {
 1020         case SIOCSIFADDR:
 1021                 ifp->if_flags |= IFF_UP;
 1022 
 1023                 switch (ifa->ifa_addr->sa_family) {
 1024 #ifdef INET
 1025                 case AF_INET:
 1026                         ifp->if_init(ifp->if_softc);    /* before arpwhohas */
 1027                         arp_ifinit(ifp, ifa);
 1028                         break;
 1029 #endif
 1030 #ifdef IPX
 1031                 /*
 1032                  * XXX - This code is probably wrong
 1033                  */
 1034                 case AF_IPX:
 1035                         {
 1036                         struct ipx_addr *ina = &(IA_SIPX(ifa)->sipx_addr);
 1037 
 1038                         if (ipx_nullhost(*ina))
 1039                                 ina->x_host =
 1040                                     *(union ipx_host *)
 1041                                     IF_LLADDR(ifp);
 1042                         else {
 1043                                 bcopy((caddr_t) ina->x_host.c_host,
 1044                                       (caddr_t) IF_LLADDR(ifp),
 1045                                       ETHER_ADDR_LEN);
 1046                         }
 1047 
 1048                         /*
 1049                          * Set new address
 1050                          */
 1051                         ifp->if_init(ifp->if_softc);
 1052                         break;
 1053                         }
 1054 #endif
 1055                 default:
 1056                         ifp->if_init(ifp->if_softc);
 1057                         break;
 1058                 }
 1059                 break;
 1060 
 1061         case SIOCGIFADDR:
 1062                 {
 1063                         struct sockaddr *sa;
 1064 
 1065                         sa = (struct sockaddr *) & ifr->ifr_data;
 1066                         bcopy(IF_LLADDR(ifp),
 1067                               (caddr_t) sa->sa_data, ETHER_ADDR_LEN);
 1068                 }
 1069                 break;
 1070 
 1071         case SIOCSIFMTU:
 1072                 /*
 1073                  * Set the interface MTU.
 1074                  */
 1075                 if (ifr->ifr_mtu > ETHERMTU) {
 1076                         error = EINVAL;
 1077                 } else {
 1078                         ifp->if_mtu = ifr->ifr_mtu;
 1079                 }
 1080                 break;
 1081         default:
 1082                 error = EINVAL;                 /* XXX netbsd has ENOTTY??? */
 1083                 break;
 1084         }
 1085         return (error);
 1086 }
 1087 
 1088 static int
 1089 ether_resolvemulti(struct ifnet *ifp, struct sockaddr **llsa,
 1090         struct sockaddr *sa)
 1091 {
 1092         struct sockaddr_dl *sdl;
 1093 #ifdef INET
 1094         struct sockaddr_in *sin;
 1095 #endif
 1096 #ifdef INET6
 1097         struct sockaddr_in6 *sin6;
 1098 #endif
 1099         u_char *e_addr;
 1100 
 1101         switch(sa->sa_family) {
 1102         case AF_LINK:
 1103                 /*
 1104                  * No mapping needed. Just check that it's a valid MC address.
 1105                  */
 1106                 sdl = (struct sockaddr_dl *)sa;
 1107                 e_addr = LLADDR(sdl);
 1108                 if (!ETHER_IS_MULTICAST(e_addr))
 1109                         return EADDRNOTAVAIL;
 1110                 *llsa = 0;
 1111                 return 0;
 1112 
 1113 #ifdef INET
 1114         case AF_INET:
 1115                 sin = (struct sockaddr_in *)sa;
 1116                 if (!IN_MULTICAST(ntohl(sin->sin_addr.s_addr)))
 1117                         return EADDRNOTAVAIL;
 1118                 MALLOC(sdl, struct sockaddr_dl *, sizeof *sdl, M_IFMADDR,
 1119                        M_NOWAIT|M_ZERO);
 1120                 if (sdl == NULL)
 1121                         return ENOMEM;
 1122                 sdl->sdl_len = sizeof *sdl;
 1123                 sdl->sdl_family = AF_LINK;
 1124                 sdl->sdl_index = ifp->if_index;
 1125                 sdl->sdl_type = IFT_ETHER;
 1126                 sdl->sdl_alen = ETHER_ADDR_LEN;
 1127                 e_addr = LLADDR(sdl);
 1128                 ETHER_MAP_IP_MULTICAST(&sin->sin_addr, e_addr);
 1129                 *llsa = (struct sockaddr *)sdl;
 1130                 return 0;
 1131 #endif
 1132 #ifdef INET6
 1133         case AF_INET6:
 1134                 sin6 = (struct sockaddr_in6 *)sa;
 1135                 if (IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) {
 1136                         /*
 1137                          * An IP6 address of 0 means listen to all
 1138                          * of the Ethernet multicast address used for IP6.
 1139                          * (This is used for multicast routers.)
 1140                          */
 1141                         ifp->if_flags |= IFF_ALLMULTI;
 1142                         *llsa = 0;
 1143                         return 0;
 1144                 }
 1145                 if (!IN6_IS_ADDR_MULTICAST(&sin6->sin6_addr))
 1146                         return EADDRNOTAVAIL;
 1147                 MALLOC(sdl, struct sockaddr_dl *, sizeof *sdl, M_IFMADDR,
 1148                        M_NOWAIT|M_ZERO);
 1149                 if (sdl == NULL)
 1150                         return (ENOMEM);
 1151                 sdl->sdl_len = sizeof *sdl;
 1152                 sdl->sdl_family = AF_LINK;
 1153                 sdl->sdl_index = ifp->if_index;
 1154                 sdl->sdl_type = IFT_ETHER;
 1155                 sdl->sdl_alen = ETHER_ADDR_LEN;
 1156                 e_addr = LLADDR(sdl);
 1157                 ETHER_MAP_IPV6_MULTICAST(&sin6->sin6_addr, e_addr);
 1158                 *llsa = (struct sockaddr *)sdl;
 1159                 return 0;
 1160 #endif
 1161 
 1162         default:
 1163                 /*
 1164                  * Well, the text isn't quite right, but it's the name
 1165                  * that counts...
 1166                  */
 1167                 return EAFNOSUPPORT;
 1168         }
 1169 }
 1170 
 1171 static void*
 1172 ether_alloc(u_char type, struct ifnet *ifp)
 1173 {
 1174         struct arpcom   *ac;
 1175         
 1176         ac = malloc(sizeof(struct arpcom), M_ARPCOM, M_WAITOK | M_ZERO);
 1177         ac->ac_ifp = ifp;
 1178 
 1179         return (ac);
 1180 }
 1181 
 1182 static void
 1183 ether_free(void *com, u_char type)
 1184 {
 1185 
 1186         free(com, M_ARPCOM);
 1187 }
 1188 
 1189 static int
 1190 ether_modevent(module_t mod, int type, void *data)
 1191 {
 1192 
 1193         switch (type) {
 1194         case MOD_LOAD:
 1195                 if_register_com_alloc(IFT_ETHER, ether_alloc, ether_free);
 1196                 break;
 1197         case MOD_UNLOAD:
 1198                 if_deregister_com_alloc(IFT_ETHER);
 1199                 break;
 1200         default:
 1201                 return EOPNOTSUPP;
 1202         }
 1203 
 1204         return (0);
 1205 }
 1206 
 1207 static moduledata_t ether_mod = {
 1208         "ether",
 1209         ether_modevent,
 1210         0
 1211 };
 1212 
 1213 void
 1214 ether_vlan_mtap(struct bpf_if *bp, struct mbuf *m, void *data, u_int dlen)
 1215 {
 1216         struct ether_vlan_header vlan;
 1217         struct mbuf mv, mb;
 1218 
 1219         KASSERT((m->m_flags & M_VLANTAG) != 0,
 1220             ("%s: vlan information not present", __func__));
 1221         KASSERT(m->m_len >= sizeof(struct ether_header),
 1222             ("%s: mbuf not large enough for header", __func__));
 1223         bcopy(mtod(m, char *), &vlan, sizeof(struct ether_header));
 1224         vlan.evl_proto = vlan.evl_encap_proto;
 1225         vlan.evl_encap_proto = htons(ETHERTYPE_VLAN);
 1226         vlan.evl_tag = htons(m->m_pkthdr.ether_vtag);
 1227         m->m_len -= sizeof(struct ether_header);
 1228         m->m_data += sizeof(struct ether_header);
 1229         /*
 1230          * If a data link has been supplied by the caller, then we will need to
 1231          * re-create a stack allocated mbuf chain with the following structure:
 1232          *
 1233          * (1) mbuf #1 will contain the supplied data link
 1234          * (2) mbuf #2 will contain the vlan header
 1235          * (3) mbuf #3 will contain the original mbuf's packet data
 1236          *
 1237          * Otherwise, submit the packet and vlan header via bpf_mtap2().
 1238          */
 1239         if (data != NULL) {
 1240                 mv.m_next = m;
 1241                 mv.m_data = (caddr_t)&vlan;
 1242                 mv.m_len = sizeof(vlan);
 1243                 mb.m_next = &mv;
 1244                 mb.m_data = data;
 1245                 mb.m_len = dlen;
 1246                 bpf_mtap(bp, &mb);
 1247         } else
 1248                 bpf_mtap2(bp, &vlan, sizeof(vlan), m);
 1249         m->m_len += sizeof(struct ether_header);
 1250         m->m_data -= sizeof(struct ether_header);
 1251 }
 1252 
 1253 struct mbuf *
 1254 ether_vlanencap(struct mbuf *m, uint16_t tag)
 1255 {
 1256         struct ether_vlan_header *evl;
 1257 
 1258         M_PREPEND(m, ETHER_VLAN_ENCAP_LEN, M_DONTWAIT);
 1259         if (m == NULL)
 1260                 return (NULL);
 1261         /* M_PREPEND takes care of m_len, m_pkthdr.len for us */
 1262 
 1263         if (m->m_len < sizeof(*evl)) {
 1264                 m = m_pullup(m, sizeof(*evl));
 1265                 if (m == NULL)
 1266                         return (NULL);
 1267         }
 1268 
 1269         /*
 1270          * Transform the Ethernet header into an Ethernet header
 1271          * with 802.1Q encapsulation.
 1272          */
 1273         evl = mtod(m, struct ether_vlan_header *);
 1274         bcopy((char *)evl + ETHER_VLAN_ENCAP_LEN,
 1275             (char *)evl, ETHER_HDR_LEN - ETHER_TYPE_LEN);
 1276         evl->evl_encap_proto = htons(ETHERTYPE_VLAN);
 1277         evl->evl_tag = htons(tag);
 1278         return (m);
 1279 }
 1280 
 1281 DECLARE_MODULE(ether, ether_mod, SI_SUB_INIT_IF, SI_ORDER_ANY);
 1282 MODULE_VERSION(ether, 1);

Cache object: e263bc0387e0519b059f7f2deceba516


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