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

Cache object: d301b50cb90658dc5779a110111f085e


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