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

Cache object: 591b5126b7dcf898ec6f842fc6bbba0a


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