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/5.4/sys/net/if_ethersubr.c 145335 2005-04-20 19:11:07Z cvs2svn $
   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_bdg.h"
   38 #include "opt_mac.h"
   39 #include "opt_netgraph.h"
   40 #include "opt_carp.h"
   41 
   42 #include <sys/param.h>
   43 #include <sys/systm.h>
   44 #include <sys/kernel.h>
   45 #include <sys/mac.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 
   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/bridge.h>
   64 #include <net/if_vlan_var.h>
   65 
   66 #if defined(INET) || defined(INET6)
   67 #include <netinet/in.h>
   68 #include <netinet/in_var.h>
   69 #include <netinet/if_ether.h>
   70 #include <netinet/ip_fw.h>
   71 #include <netinet/ip_dummynet.h>
   72 #endif
   73 #ifdef INET6
   74 #include <netinet6/nd6.h>
   75 #endif
   76 
   77 #ifdef DEV_CARP
   78 #include <netinet/ip_carp.h>
   79 #endif
   80 
   81 #ifdef IPX
   82 #include <netipx/ipx.h>
   83 #include <netipx/ipx_if.h>
   84 #endif
   85 int (*ef_inputp)(struct ifnet*, struct ether_header *eh, struct mbuf *m);
   86 int (*ef_outputp)(struct ifnet *ifp, struct mbuf **mp,
   87                 struct sockaddr *dst, short *tp, int *hlen);
   88 
   89 #ifdef NETATALK
   90 #include <netatalk/at.h>
   91 #include <netatalk/at_var.h>
   92 #include <netatalk/at_extern.h>
   93 
   94 #define llc_snap_org_code llc_un.type_snap.org_code
   95 #define llc_snap_ether_type llc_un.type_snap.ether_type
   96 
   97 extern u_char   at_org_code[3];
   98 extern u_char   aarp_org_code[3];
   99 #endif /* NETATALK */
  100 
  101 /* netgraph node hooks for ng_ether(4) */
  102 void    (*ng_ether_input_p)(struct ifnet *ifp, struct mbuf **mp);
  103 void    (*ng_ether_input_orphan_p)(struct ifnet *ifp, struct mbuf *m);
  104 int     (*ng_ether_output_p)(struct ifnet *ifp, struct mbuf **mp);
  105 void    (*ng_ether_attach_p)(struct ifnet *ifp);
  106 void    (*ng_ether_detach_p)(struct ifnet *ifp);
  107 
  108 void    (*vlan_input_p)(struct ifnet *, struct mbuf *);
  109 
  110 /* bridge support */
  111 int do_bridge;
  112 bridge_in_t *bridge_in_ptr;
  113 bdg_forward_t *bdg_forward_ptr;
  114 bdgtakeifaces_t *bdgtakeifaces_ptr;
  115 struct bdg_softc *ifp2sc;
  116 
  117 static const u_char etherbroadcastaddr[ETHER_ADDR_LEN] =
  118                         { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
  119 
  120 static  int ether_resolvemulti(struct ifnet *, struct sockaddr **,
  121                 struct sockaddr *);
  122 
  123 #define senderr(e) do { error = (e); goto bad;} while (0)
  124 
  125 #if defined(INET) || defined(INET6)
  126 int
  127 ether_ipfw_chk(struct mbuf **m0, struct ifnet *dst,
  128         struct ip_fw **rule, int shared);
  129 static int ether_ipfw;
  130 #endif
  131 
  132 /*
  133  * Ethernet output routine.
  134  * Encapsulate a packet of type family for the local net.
  135  * Use trailer local net encapsulation if enough data in first
  136  * packet leaves a multiple of 512 bytes of data in remainder.
  137  * Assumes that ifp is actually pointer to arpcom structure.
  138  */
  139 int
  140 ether_output(struct ifnet *ifp, struct mbuf *m,
  141         struct sockaddr *dst, struct rtentry *rt0)
  142 {
  143         short type;
  144         int error, hdrcmplt = 0;
  145         u_char esrc[ETHER_ADDR_LEN], edst[ETHER_ADDR_LEN];
  146         struct ether_header *eh;
  147         int loop_copy = 0;
  148         int hlen;       /* link layer header length */
  149 
  150 #ifdef MAC
  151         error = mac_check_ifnet_transmit(ifp, m);
  152         if (error)
  153                 senderr(error);
  154 #endif
  155 
  156         if (ifp->if_flags & IFF_MONITOR)
  157                 senderr(ENETDOWN);
  158         if ((ifp->if_flags & (IFF_UP|IFF_RUNNING)) != (IFF_UP|IFF_RUNNING))
  159                 senderr(ENETDOWN);
  160 
  161         hlen = ETHER_HDR_LEN;
  162         switch (dst->sa_family) {
  163 #ifdef INET
  164         case AF_INET:
  165                 error = arpresolve(ifp, rt0, m, dst, edst);
  166                 if (error)
  167                         return (error == EWOULDBLOCK ? 0 : error);
  168                 type = htons(ETHERTYPE_IP);
  169                 break;
  170         case AF_ARP:
  171         {
  172                 struct arphdr *ah;
  173                 ah = mtod(m, struct arphdr *);
  174                 ah->ar_hrd = htons(ARPHRD_ETHER);
  175 
  176                 loop_copy = -1; /* if this is for us, don't do it */
  177 
  178                 switch(ntohs(ah->ar_op)) {
  179                 case ARPOP_REVREQUEST:
  180                 case ARPOP_REVREPLY:
  181                         type = htons(ETHERTYPE_REVARP);
  182                         break;
  183                 case ARPOP_REQUEST:
  184                 case ARPOP_REPLY:
  185                 default:
  186                         type = htons(ETHERTYPE_ARP);
  187                         break;
  188                 }
  189 
  190                 if (m->m_flags & M_BCAST)
  191                         bcopy(ifp->if_broadcastaddr, edst, ETHER_ADDR_LEN);
  192                 else
  193                         bcopy(ar_tha(ah), edst, ETHER_ADDR_LEN);
  194 
  195         }
  196         break;
  197 #endif
  198 #ifdef INET6
  199         case AF_INET6:
  200                 error = nd6_storelladdr(ifp, rt0, m, dst, (u_char *)edst);
  201                 if (error)
  202                         return error;
  203                 type = htons(ETHERTYPE_IPV6);
  204                 break;
  205 #endif
  206 #ifdef IPX
  207         case AF_IPX:
  208                 if (ef_outputp) {
  209                     error = ef_outputp(ifp, &m, dst, &type, &hlen);
  210                     if (error)
  211                         goto bad;
  212                 } else
  213                     type = htons(ETHERTYPE_IPX);
  214                 bcopy((caddr_t)&(((struct sockaddr_ipx *)dst)->sipx_addr.x_host),
  215                     (caddr_t)edst, sizeof (edst));
  216                 break;
  217 #endif
  218 #ifdef NETATALK
  219         case AF_APPLETALK:
  220           {
  221             struct at_ifaddr *aa;
  222 
  223             if ((aa = at_ifawithnet((struct sockaddr_at *)dst)) == NULL)
  224                     senderr(EHOSTUNREACH); /* XXX */
  225             if (!aarpresolve(ifp, m, (struct sockaddr_at *)dst, edst))
  226                     return (0);
  227             /*
  228              * In the phase 2 case, need to prepend an mbuf for the llc header.
  229              */
  230             if ( aa->aa_flags & AFA_PHASE2 ) {
  231                 struct llc llc;
  232 
  233                 M_PREPEND(m, LLC_SNAPFRAMELEN, M_DONTWAIT);
  234                 if (m == NULL)
  235                         senderr(ENOBUFS);
  236                 llc.llc_dsap = llc.llc_ssap = LLC_SNAP_LSAP;
  237                 llc.llc_control = LLC_UI;
  238                 bcopy(at_org_code, llc.llc_snap_org_code, sizeof(at_org_code));
  239                 llc.llc_snap_ether_type = htons( ETHERTYPE_AT );
  240                 bcopy(&llc, mtod(m, caddr_t), LLC_SNAPFRAMELEN);
  241                 type = htons(m->m_pkthdr.len);
  242                 hlen = LLC_SNAPFRAMELEN + ETHER_HDR_LEN;
  243             } else {
  244                 type = htons(ETHERTYPE_AT);
  245             }
  246             break;
  247           }
  248 #endif /* NETATALK */
  249 
  250         case pseudo_AF_HDRCMPLT:
  251                 hdrcmplt = 1;
  252                 eh = (struct ether_header *)dst->sa_data;
  253                 (void)memcpy(esrc, eh->ether_shost, sizeof (esrc));
  254                 /* FALLTHROUGH */
  255 
  256         case AF_UNSPEC:
  257                 loop_copy = -1; /* if this is for us, don't do it */
  258                 eh = (struct ether_header *)dst->sa_data;
  259                 (void)memcpy(edst, eh->ether_dhost, sizeof (edst));
  260                 type = eh->ether_type;
  261                 break;
  262 
  263         default:
  264                 if_printf(ifp, "can't handle af%d\n", dst->sa_family);
  265                 senderr(EAFNOSUPPORT);
  266         }
  267 
  268         /*
  269          * Add local net header.  If no space in first mbuf,
  270          * allocate another.
  271          */
  272         M_PREPEND(m, ETHER_HDR_LEN, M_DONTWAIT);
  273         if (m == NULL)
  274                 senderr(ENOBUFS);
  275         eh = mtod(m, struct ether_header *);
  276         (void)memcpy(&eh->ether_type, &type,
  277                 sizeof(eh->ether_type));
  278         (void)memcpy(eh->ether_dhost, edst, sizeof (edst));
  279         if (hdrcmplt)
  280                 (void)memcpy(eh->ether_shost, esrc,
  281                         sizeof(eh->ether_shost));
  282         else
  283                 (void)memcpy(eh->ether_shost, IFP2AC(ifp)->ac_enaddr,
  284                         sizeof(eh->ether_shost));
  285 
  286         /*
  287          * If a simplex interface, and the packet is being sent to our
  288          * Ethernet address or a broadcast address, loopback a copy.
  289          * XXX To make a simplex device behave exactly like a duplex
  290          * device, we should copy in the case of sending to our own
  291          * ethernet address (thus letting the original actually appear
  292          * on the wire). However, we don't do that here for security
  293          * reasons and compatibility with the original behavior.
  294          */
  295         if ((ifp->if_flags & IFF_SIMPLEX) && (loop_copy != -1)) {
  296                 int csum_flags = 0;
  297 
  298                 if (m->m_pkthdr.csum_flags & CSUM_IP)
  299                         csum_flags |= (CSUM_IP_CHECKED|CSUM_IP_VALID);
  300                 if (m->m_pkthdr.csum_flags & CSUM_DELAY_DATA)
  301                         csum_flags |= (CSUM_DATA_VALID|CSUM_PSEUDO_HDR);
  302 
  303                 if ((m->m_flags & M_BCAST) || (loop_copy > 0)) {
  304                         struct mbuf *n;
  305 
  306                         if ((n = m_copy(m, 0, (int)M_COPYALL)) != NULL) {
  307                                 n->m_pkthdr.csum_flags |= csum_flags;
  308                                 if (csum_flags & CSUM_DATA_VALID)
  309                                         n->m_pkthdr.csum_data = 0xffff;
  310                                 (void)if_simloop(ifp, n, dst->sa_family, hlen);
  311                         } else
  312                                 ifp->if_iqdrops++;
  313                 } else if (bcmp(eh->ether_dhost, eh->ether_shost,
  314                                 ETHER_ADDR_LEN) == 0) {
  315                         m->m_pkthdr.csum_flags |= csum_flags;
  316                         if (csum_flags & CSUM_DATA_VALID)
  317                                 m->m_pkthdr.csum_data = 0xffff;
  318                         (void) if_simloop(ifp, m, dst->sa_family, hlen);
  319                         return (0);     /* XXX */
  320                 }
  321         }
  322 
  323 #ifdef DEV_CARP
  324         if (ifp->if_carp &&
  325             (error = carp_output(ifp, m, dst, NULL)))
  326                 goto bad;
  327 #endif
  328 
  329         /* Handle ng_ether(4) processing, if any */
  330         if (ng_ether_output_p != NULL) {
  331                 if ((error = (*ng_ether_output_p)(ifp, &m)) != 0) {
  332 bad:                    if (m != NULL)
  333                                 m_freem(m);
  334                         return (error);
  335                 }
  336                 if (m == NULL)
  337                         return (0);
  338         }
  339 
  340         /* Continue with link-layer output */
  341         return ether_output_frame(ifp, m);
  342 }
  343 
  344 /*
  345  * Ethernet link layer output routine to send a raw frame to the device.
  346  *
  347  * This assumes that the 14 byte Ethernet header is present and contiguous
  348  * in the first mbuf (if BRIDGE'ing).
  349  */
  350 int
  351 ether_output_frame(struct ifnet *ifp, struct mbuf *m)
  352 {
  353 #if defined(INET) || defined(INET6)
  354         struct ip_fw *rule = ip_dn_claim_rule(m);
  355 #else
  356         void *rule = NULL;
  357 #endif
  358         int error;
  359 
  360         if (rule == NULL && BDG_ACTIVE(ifp)) {
  361                 /*
  362                  * Beware, the bridge code notices the null rcvif and
  363                  * uses that identify that it's being called from
  364                  * ether_output as opposd to ether_input.  Yech.
  365                  */
  366                 m->m_pkthdr.rcvif = NULL;
  367                 m = bdg_forward_ptr(m, ifp);
  368                 if (m != NULL)
  369                         m_freem(m);
  370                 return (0);
  371         }
  372 #if defined(INET) || defined(INET6)
  373         if (IPFW_LOADED && ether_ipfw != 0) {
  374                 if (ether_ipfw_chk(&m, ifp, &rule, 0) == 0) {
  375                         if (m) {
  376                                 m_freem(m);
  377                                 return EACCES;  /* pkt dropped */
  378                         } else
  379                                 return 0;       /* consumed e.g. in a pipe */
  380                 }
  381         }
  382 #endif
  383 
  384         /*
  385          * Queue message on interface, update output statistics if
  386          * successful, and start output if interface not yet active.
  387          */
  388         IFQ_HANDOFF(ifp, m, error);
  389         return (error);
  390 }
  391 
  392 #if defined(INET) || defined(INET6)
  393 /*
  394  * ipfw processing for ethernet packets (in and out).
  395  * The second parameter is NULL from ether_demux, and ifp from
  396  * ether_output_frame. This section of code could be used from
  397  * bridge.c as well as long as we use some extra info
  398  * to distinguish that case from ether_output_frame();
  399  */
  400 int
  401 ether_ipfw_chk(struct mbuf **m0, struct ifnet *dst,
  402         struct ip_fw **rule, int shared)
  403 {
  404         struct ether_header *eh;
  405         struct ether_header save_eh;
  406         struct mbuf *m;
  407         int i;
  408         struct ip_fw_args args;
  409 
  410         if (*rule != NULL && fw_one_pass)
  411                 return 1; /* dummynet packet, already partially processed */
  412 
  413         /*
  414          * I need some amt of data to be contiguous, and in case others need
  415          * the packet (shared==1) also better be in the first mbuf.
  416          */
  417         m = *m0;
  418         i = min( m->m_pkthdr.len, max_protohdr);
  419         if ( shared || m->m_len < i) {
  420                 m = m_pullup(m, i);
  421                 if (m == NULL) {
  422                         *m0 = m;
  423                         return 0;
  424                 }
  425         }
  426         eh = mtod(m, struct ether_header *);
  427         save_eh = *eh;                  /* save copy for restore below */
  428         m_adj(m, ETHER_HDR_LEN);        /* strip ethernet header */
  429 
  430         args.m = m;             /* the packet we are looking at         */
  431         args.oif = dst;         /* destination, if any                  */
  432         args.rule = *rule;      /* matching rule to restart             */
  433         args.next_hop = NULL;   /* we do not support forward yet        */
  434         args.eh = &save_eh;     /* MAC header for bridged/MAC packets   */
  435         i = ip_fw_chk_ptr(&args);
  436         m = args.m;
  437         if (m != NULL) {
  438                 /*
  439                  * Restore Ethernet header, as needed, in case the
  440                  * mbuf chain was replaced by ipfw.
  441                  */
  442                 M_PREPEND(m, ETHER_HDR_LEN, M_DONTWAIT);
  443                 if (m == NULL) {
  444                         *m0 = m;
  445                         return 0;
  446                 }
  447                 if (eh != mtod(m, struct ether_header *))
  448                         bcopy(&save_eh, mtod(m, struct ether_header *),
  449                                 ETHER_HDR_LEN);
  450         }
  451         *m0 = m;
  452         *rule = args.rule;
  453 
  454         if ( (i & IP_FW_PORT_DENY_FLAG) || m == NULL) /* drop */
  455                 return 0;
  456 
  457         if (i == 0) /* a PASS rule.  */
  458                 return 1;
  459 
  460         if (DUMMYNET_LOADED && (i & IP_FW_PORT_DYNT_FLAG)) {
  461                 /*
  462                  * Pass the pkt to dummynet, which consumes it.
  463                  * If shared, make a copy and keep the original.
  464                  */
  465                 if (shared) {
  466                         m = m_copypacket(m, M_DONTWAIT);
  467                         if (m == NULL)
  468                                 return 0;
  469                 } else {
  470                         /*
  471                          * Pass the original to dummynet and
  472                          * nothing back to the caller
  473                          */
  474                         *m0 = NULL ;
  475                 }
  476                 ip_dn_io_ptr(m, (i & 0xffff),
  477                         dst ? DN_TO_ETH_OUT: DN_TO_ETH_DEMUX, &args);
  478                 return 0;
  479         }
  480         /*
  481          * XXX at some point add support for divert/forward actions.
  482          * If none of the above matches, we have to drop the pkt.
  483          */
  484         return 0;
  485 }
  486 #endif
  487 
  488 /*
  489  * Process a received Ethernet packet; the packet is in the
  490  * mbuf chain m with the ethernet header at the front.
  491  */
  492 static void
  493 ether_input(struct ifnet *ifp, struct mbuf *m)
  494 {
  495         struct ether_header *eh;
  496         u_short etype;
  497 
  498         /*
  499          * Do consistency checks to verify assumptions
  500          * made by code past this point.
  501          */
  502         if ((m->m_flags & M_PKTHDR) == 0) {
  503                 if_printf(ifp, "discard frame w/o packet header\n");
  504                 ifp->if_ierrors++;
  505                 m_freem(m);
  506                 return;
  507         }
  508         if (m->m_len < ETHER_HDR_LEN) {
  509                 /* XXX maybe should pullup? */
  510                 if_printf(ifp, "discard frame w/o leading ethernet "
  511                                 "header (len %u pkt len %u)\n",
  512                                 m->m_len, m->m_pkthdr.len);
  513                 ifp->if_ierrors++;
  514                 m_freem(m);
  515                 return;
  516         }
  517         eh = mtod(m, struct ether_header *);
  518         etype = ntohs(eh->ether_type);
  519         if (m->m_pkthdr.len >
  520             ETHER_MAX_FRAME(ifp, etype, m->m_flags & M_HASFCS)) {
  521                 if_printf(ifp, "discard oversize frame "
  522                                 "(ether type %x flags %x len %u > max %lu)\n",
  523                                 etype, m->m_flags, m->m_pkthdr.len,
  524                                 ETHER_MAX_FRAME(ifp, etype,
  525                                                 m->m_flags & M_HASFCS));
  526                 ifp->if_ierrors++;
  527                 m_freem(m);
  528                 return;
  529         }
  530         if (m->m_pkthdr.rcvif == NULL) {
  531                 if_printf(ifp, "discard frame w/o interface pointer\n");
  532                 ifp->if_ierrors++;
  533                 m_freem(m);
  534                 return;
  535         }
  536 #ifdef DIAGNOSTIC
  537         if (m->m_pkthdr.rcvif != ifp) {
  538                 if_printf(ifp, "Warning, frame marked as received on %s\n",
  539                         m->m_pkthdr.rcvif->if_xname);
  540         }
  541 #endif
  542 
  543 #ifdef MAC
  544         /*
  545          * Tag the mbuf with an appropriate MAC label before any other
  546          * consumers can get to it.
  547          */
  548         mac_create_mbuf_from_ifnet(ifp, m);
  549 #endif
  550 
  551         /*
  552          * Give bpf a chance at the packet.
  553          */
  554         BPF_MTAP(ifp, m);
  555 
  556         if (ifp->if_flags & IFF_MONITOR) {
  557                 /*
  558                  * Interface marked for monitoring; discard packet.
  559                  */
  560                 m_freem(m);
  561                 return;
  562         }
  563 
  564         /* If the CRC is still on the packet, trim it off. */
  565         if (m->m_flags & M_HASFCS) {
  566                 m_adj(m, -ETHER_CRC_LEN);
  567                 m->m_flags &= ~M_HASFCS;
  568         }
  569 
  570         ifp->if_ibytes += m->m_pkthdr.len;
  571 
  572         /* Handle ng_ether(4) processing, if any */
  573         if (ng_ether_input_p != NULL) {
  574                 (*ng_ether_input_p)(ifp, &m);
  575                 if (m == NULL)
  576                         return;
  577         }
  578 
  579         /* Check for bridging mode */
  580         if (BDG_ACTIVE(ifp) ) {
  581                 struct ifnet *bif;
  582 
  583                 /*
  584                  * Check with bridging code to see how the packet
  585                  * should be handled.  Possibilities are:
  586                  *
  587                  *    BDG_BCAST         broadcast
  588                  *    BDG_MCAST         multicast
  589                  *    BDG_LOCAL         for local address, don't forward
  590                  *    BDG_DROP          discard
  591                  *    ifp               forward only to specified interface(s)
  592                  *
  593                  * Non-local destinations are handled by passing the
  594                  * packet back to the bridge code.
  595                  */
  596                 bif = bridge_in_ptr(ifp, eh);
  597                 if (bif == BDG_DROP) {          /* discard packet */
  598                         m_freem(m);
  599                         return;
  600                 }
  601                 if (bif != BDG_LOCAL) {         /* non-local, forward */
  602                         m = bdg_forward_ptr(m, bif);
  603                         /*
  604                          * The bridge may consume the packet if it's not
  605                          * supposed to be passed up or if a problem occurred
  606                          * while doing its job.  This is reflected by it
  607                          * returning a NULL mbuf pointer.
  608                          */
  609                         if (m == NULL) {
  610                                 if (bif == BDG_BCAST || bif == BDG_MCAST)
  611                                         if_printf(ifp,
  612                                                 "bridge dropped %s packet\n",
  613                                                 bif == BDG_BCAST ? "broadcast" :
  614                                                                    "multicast");
  615                                 return;
  616                         }
  617                         /*
  618                          * But in some cases the bridge may return the
  619                          * packet for us to free; sigh.
  620                          */
  621                         if (bif != BDG_BCAST && bif != BDG_MCAST) {
  622                                 m_freem(m);
  623                                 return;
  624                         }
  625                 }
  626         }
  627 
  628         /* First chunk of an mbuf contains good entropy */
  629         if (harvest.ethernet)
  630                 random_harvest(m, 16, 3, 0, RANDOM_NET);
  631         ether_demux(ifp, m);
  632 }
  633 
  634 /*
  635  * Upper layer processing for a received Ethernet packet.
  636  */
  637 void
  638 ether_demux(struct ifnet *ifp, struct mbuf *m)
  639 {
  640         struct ether_header *eh;
  641         int isr;
  642         u_short ether_type;
  643 #if defined(NETATALK)
  644         struct llc *l;
  645 #endif
  646 #if defined(INET) || defined(INET6)
  647         struct ip_fw *rule = ip_dn_claim_rule(m);
  648 #endif
  649 
  650         KASSERT(ifp != NULL, ("ether_demux: NULL interface pointer"));
  651 
  652         eh = mtod(m, struct ether_header *);
  653         ether_type = ntohs(eh->ether_type);
  654 
  655 #if defined(INET) || defined(INET6)
  656         if (rule)       /* packet was already bridged */
  657                 goto post_stats;
  658 #endif
  659 
  660         if (!(BDG_ACTIVE(ifp)) &&
  661             !(ether_type == ETHERTYPE_VLAN && ifp->if_nvlans > 0)) {
  662 #ifdef DEV_CARP
  663                 /*
  664                  * XXX: Okay, we need to call carp_forus() and - if it is for
  665                  * us jump over code that does the normal check
  666                  * "ac_enaddr == ether_dhost". The check sequence is a bit
  667                  * different from OpenBSD, so we jump over as few code as
  668                  * possible, to catch _all_ sanity checks. This needs
  669                  * evaluation, to see if the carp ether_dhost values break any
  670                  * of these checks!
  671                  */
  672                 if (ifp->if_carp && carp_forus(ifp->if_carp, eh->ether_dhost))
  673                         goto pre_stats;
  674 #endif
  675                 /*
  676                  * Discard packet if upper layers shouldn't see it because it
  677                  * was unicast to a different Ethernet address. If the driver
  678                  * is working properly, then this situation can only happen
  679                  * when the interface is in promiscuous mode.
  680                  *
  681                  * If VLANs are active, and this packet has a VLAN tag, do
  682                  * not drop it here but pass it on to the VLAN layer, to
  683                  * give them a chance to consider it as well (e. g. in case
  684                  * bridging is only active on a VLAN).  They will drop it if
  685                  * it's undesired.
  686                  */
  687                 if ((ifp->if_flags & IFF_PROMISC) != 0
  688                     && (eh->ether_dhost[0] & 1) == 0
  689                     && bcmp(eh->ether_dhost,
  690                       IFP2AC(ifp)->ac_enaddr, ETHER_ADDR_LEN) != 0
  691                     && (ifp->if_flags & IFF_PPROMISC) == 0) {
  692                             m_freem(m);
  693                             return;
  694                 }
  695         }
  696 
  697 #ifdef DEV_CARP
  698 pre_stats:
  699 #endif
  700         /* Discard packet if interface is not up */
  701         if ((ifp->if_flags & IFF_UP) == 0) {
  702                 m_freem(m);
  703                 return;
  704         }
  705         if (ETHER_IS_MULTICAST(eh->ether_dhost)) {
  706                 if (bcmp(etherbroadcastaddr, eh->ether_dhost,
  707                     sizeof(etherbroadcastaddr)) == 0)
  708                         m->m_flags |= M_BCAST;
  709                 else
  710                         m->m_flags |= M_MCAST;
  711         }
  712         if (m->m_flags & (M_BCAST|M_MCAST))
  713                 ifp->if_imcasts++;
  714 
  715 #if defined(INET) || defined(INET6)
  716 post_stats:
  717         if (IPFW_LOADED && ether_ipfw != 0) {
  718                 if (ether_ipfw_chk(&m, NULL, &rule, 0) == 0) {
  719                         if (m)
  720                                 m_freem(m);
  721                         return;
  722                 }
  723         }
  724 #endif
  725 
  726         /*
  727          * If VLANs are configured on the interface, check to
  728          * see if the device performed the decapsulation and
  729          * provided us with the tag.
  730          */
  731         if (ifp->if_nvlans &&
  732             m_tag_locate(m, MTAG_VLAN, MTAG_VLAN_TAG, NULL) != NULL) {
  733                 /*
  734                  * vlan_input() will either recursively call ether_input()
  735                  * or drop the packet.
  736                  */
  737                 KASSERT(vlan_input_p != NULL,("ether_input: VLAN not loaded!"));
  738                 (*vlan_input_p)(ifp, m);
  739                 return;
  740         }
  741 
  742         /*
  743          * Handle protocols that expect to have the Ethernet header
  744          * (and possibly FCS) intact.
  745          */
  746         switch (ether_type) {
  747         case ETHERTYPE_VLAN:
  748                 if (ifp->if_nvlans != 0) {
  749                         KASSERT(vlan_input_p,("ether_input: VLAN not loaded!"));
  750                         (*vlan_input_p)(ifp, m);
  751                 } else {
  752                         ifp->if_noproto++;
  753                         m_freem(m);
  754                 }
  755                 return;
  756         }
  757 
  758         /* Strip off Ethernet header. */
  759         m_adj(m, ETHER_HDR_LEN);
  760 
  761         /* If the CRC is still on the packet, trim it off. */
  762         if (m->m_flags & M_HASFCS) {
  763                 m_adj(m, -ETHER_CRC_LEN);
  764                 m->m_flags &= ~M_HASFCS;
  765         }
  766 
  767         switch (ether_type) {
  768 #ifdef INET
  769         case ETHERTYPE_IP:
  770                 if (ip_fastforward(m))
  771                         return;
  772                 isr = NETISR_IP;
  773                 break;
  774 
  775         case ETHERTYPE_ARP:
  776                 if (ifp->if_flags & IFF_NOARP) {
  777                         /* Discard packet if ARP is disabled on interface */
  778                         m_freem(m);
  779                         return;
  780                 }
  781                 isr = NETISR_ARP;
  782                 break;
  783 #endif
  784 #ifdef IPX
  785         case ETHERTYPE_IPX:
  786                 if (ef_inputp && ef_inputp(ifp, eh, m) == 0)
  787                         return;
  788                 isr = NETISR_IPX;
  789                 break;
  790 #endif
  791 #ifdef INET6
  792         case ETHERTYPE_IPV6:
  793                 isr = NETISR_IPV6;
  794                 break;
  795 #endif
  796 #ifdef NETATALK
  797         case ETHERTYPE_AT:
  798                 isr = NETISR_ATALK1;
  799                 break;
  800         case ETHERTYPE_AARP:
  801                 isr = NETISR_AARP;
  802                 break;
  803 #endif /* NETATALK */
  804         default:
  805 #ifdef IPX
  806                 if (ef_inputp && ef_inputp(ifp, eh, m) == 0)
  807                         return;
  808 #endif /* IPX */
  809 #if defined(NETATALK)
  810                 if (ether_type > ETHERMTU)
  811                         goto discard;
  812                 l = mtod(m, struct llc *);
  813                 if (l->llc_dsap == LLC_SNAP_LSAP &&
  814                     l->llc_ssap == LLC_SNAP_LSAP &&
  815                     l->llc_control == LLC_UI) {
  816                         if (bcmp(&(l->llc_snap_org_code)[0], at_org_code,
  817                             sizeof(at_org_code)) == 0 &&
  818                             ntohs(l->llc_snap_ether_type) == ETHERTYPE_AT) {
  819                                 m_adj(m, LLC_SNAPFRAMELEN);
  820                                 isr = NETISR_ATALK2;
  821                                 break;
  822                         }
  823                         if (bcmp(&(l->llc_snap_org_code)[0], aarp_org_code,
  824                             sizeof(aarp_org_code)) == 0 &&
  825                             ntohs(l->llc_snap_ether_type) == ETHERTYPE_AARP) {
  826                                 m_adj(m, LLC_SNAPFRAMELEN);
  827                                 isr = NETISR_AARP;
  828                                 break;
  829                         }
  830                 }
  831 #endif /* NETATALK */
  832                 goto discard;
  833         }
  834         netisr_dispatch(isr, m);
  835         return;
  836 
  837 discard:
  838         /*
  839          * Packet is to be discarded.  If netgraph is present,
  840          * hand the packet to it for last chance processing;
  841          * otherwise dispose of it.
  842          */
  843         if (ng_ether_input_orphan_p != NULL) {
  844                 /*
  845                  * Put back the ethernet header so netgraph has a
  846                  * consistent view of inbound packets.
  847                  */
  848                 M_PREPEND(m, ETHER_HDR_LEN, M_DONTWAIT);
  849                 (*ng_ether_input_orphan_p)(ifp, m);
  850                 return;
  851         }
  852         m_freem(m);
  853 }
  854 
  855 /*
  856  * Convert Ethernet address to printable (loggable) representation.
  857  * This routine is for compatibility; it's better to just use
  858  *
  859  *      printf("%6D", <pointer to address>, ":");
  860  *
  861  * since there's no static buffer involved.
  862  */
  863 char *
  864 ether_sprintf(const u_char *ap)
  865 {
  866         static char etherbuf[18];
  867         snprintf(etherbuf, sizeof (etherbuf), "%6D", ap, ":");
  868         return (etherbuf);
  869 }
  870 
  871 /*
  872  * Perform common duties while attaching to interface list
  873  */
  874 void
  875 ether_ifattach(struct ifnet *ifp, const u_int8_t *llc)
  876 {
  877         int i;
  878         struct ifaddr *ifa;
  879         struct sockaddr_dl *sdl;
  880 
  881         ifp->if_type = IFT_ETHER;
  882         ifp->if_addrlen = ETHER_ADDR_LEN;
  883         ifp->if_hdrlen = ETHER_HDR_LEN;
  884         if_attach(ifp);
  885         ifp->if_mtu = ETHERMTU;
  886         ifp->if_output = ether_output;
  887         ifp->if_input = ether_input;
  888         ifp->if_resolvemulti = ether_resolvemulti;
  889         if (ifp->if_baudrate == 0)
  890                 ifp->if_baudrate = IF_Mbps(10);         /* just a default */
  891         ifp->if_broadcastaddr = etherbroadcastaddr;
  892 
  893         ifa = ifaddr_byindex(ifp->if_index);
  894         KASSERT(ifa != NULL, ("%s: no lladdr!\n", __func__));
  895         sdl = (struct sockaddr_dl *)ifa->ifa_addr;
  896         sdl->sdl_type = IFT_ETHER;
  897         sdl->sdl_alen = ifp->if_addrlen;
  898         bcopy(llc, LLADDR(sdl), ifp->if_addrlen);
  899         /*
  900          * XXX: This doesn't belong here; we do it until
  901          * XXX:  all drivers are cleaned up
  902          */
  903         if (llc != IFP2AC(ifp)->ac_enaddr)
  904                 bcopy(llc, IFP2AC(ifp)->ac_enaddr, ifp->if_addrlen);
  905 
  906         bpfattach(ifp, DLT_EN10MB, ETHER_HDR_LEN);
  907         if (ng_ether_attach_p != NULL)
  908                 (*ng_ether_attach_p)(ifp);
  909         if (BDG_LOADED)
  910                 bdgtakeifaces_ptr();
  911 
  912         /* Announce Ethernet MAC address if non-zero. */
  913         for (i = 0; i < ifp->if_addrlen; i++)
  914                 if (llc[i] != 0)
  915                         break; 
  916         if (i != ifp->if_addrlen)
  917                 if_printf(ifp, "Ethernet address: %6D\n", llc, ":");
  918         if (debug_mpsafenet && (ifp->if_flags & IFF_NEEDSGIANT) != 0)
  919                 if_printf(ifp, "if_start running deferred for Giant\n");
  920 }
  921 
  922 /*
  923  * Perform common duties while detaching an Ethernet interface
  924  */
  925 void
  926 ether_ifdetach(struct ifnet *ifp)
  927 {
  928         if (ng_ether_detach_p != NULL)
  929                 (*ng_ether_detach_p)(ifp);
  930         bpfdetach(ifp);
  931         if_detach(ifp);
  932         if (BDG_LOADED)
  933                 bdgtakeifaces_ptr();
  934 }
  935 
  936 SYSCTL_DECL(_net_link);
  937 SYSCTL_NODE(_net_link, IFT_ETHER, ether, CTLFLAG_RW, 0, "Ethernet");
  938 #if defined(INET) || defined(INET6)
  939 SYSCTL_INT(_net_link_ether, OID_AUTO, ipfw, CTLFLAG_RW,
  940             &ether_ipfw,0,"Pass ether pkts through firewall");
  941 #endif
  942 
  943 #if 0
  944 /*
  945  * This is for reference.  We have a table-driven version
  946  * of the little-endian crc32 generator, which is faster
  947  * than the double-loop.
  948  */
  949 uint32_t
  950 ether_crc32_le(const uint8_t *buf, size_t len)
  951 {
  952         size_t i;
  953         uint32_t crc;
  954         int bit;
  955         uint8_t data;
  956 
  957         crc = 0xffffffff;       /* initial value */
  958 
  959         for (i = 0; i < len; i++) {
  960                 for (data = *buf++, bit = 0; bit < 8; bit++, data >>= 1)
  961                         carry = (crc ^ data) & 1;
  962                         crc >>= 1;
  963                         if (carry)
  964                                 crc = (crc ^ ETHER_CRC_POLY_LE);
  965         }
  966 
  967         return (crc);
  968 }
  969 #else
  970 uint32_t
  971 ether_crc32_le(const uint8_t *buf, size_t len)
  972 {
  973         static const uint32_t crctab[] = {
  974                 0x00000000, 0x1db71064, 0x3b6e20c8, 0x26d930ac,
  975                 0x76dc4190, 0x6b6b51f4, 0x4db26158, 0x5005713c,
  976                 0xedb88320, 0xf00f9344, 0xd6d6a3e8, 0xcb61b38c,
  977                 0x9b64c2b0, 0x86d3d2d4, 0xa00ae278, 0xbdbdf21c
  978         };
  979         size_t i;
  980         uint32_t crc;
  981 
  982         crc = 0xffffffff;       /* initial value */
  983 
  984         for (i = 0; i < len; i++) {
  985                 crc ^= buf[i];
  986                 crc = (crc >> 4) ^ crctab[crc & 0xf];
  987                 crc = (crc >> 4) ^ crctab[crc & 0xf];
  988         }
  989 
  990         return (crc);
  991 }
  992 #endif
  993 
  994 uint32_t
  995 ether_crc32_be(const uint8_t *buf, size_t len)
  996 {
  997         size_t i;
  998         uint32_t crc, carry;
  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 & 0x80000000) ? 1 : 0) ^ (data & 0x01);
 1007                         crc <<= 1;
 1008                         if (carry)
 1009                                 crc = (crc ^ ETHER_CRC_POLY_BE) | carry;
 1010                 }
 1011         }
 1012 
 1013         return (crc);
 1014 }
 1015 
 1016 int
 1017 ether_ioctl(struct ifnet *ifp, int command, caddr_t data)
 1018 {
 1019         struct ifaddr *ifa = (struct ifaddr *) data;
 1020         struct ifreq *ifr = (struct ifreq *) data;
 1021         int error = 0;
 1022 
 1023         switch (command) {
 1024         case SIOCSIFADDR:
 1025                 ifp->if_flags |= IFF_UP;
 1026 
 1027                 switch (ifa->ifa_addr->sa_family) {
 1028 #ifdef INET
 1029                 case AF_INET:
 1030                         ifp->if_init(ifp->if_softc);    /* before arpwhohas */
 1031                         arp_ifinit(ifp, ifa);
 1032                         break;
 1033 #endif
 1034 #ifdef IPX
 1035                 /*
 1036                  * XXX - This code is probably wrong
 1037                  */
 1038                 case AF_IPX:
 1039                         {
 1040                         struct ipx_addr *ina = &(IA_SIPX(ifa)->sipx_addr);
 1041                         struct arpcom *ac = IFP2AC(ifp);
 1042 
 1043                         if (ipx_nullhost(*ina))
 1044                                 ina->x_host =
 1045                                     *(union ipx_host *)
 1046                                     ac->ac_enaddr;
 1047                         else {
 1048                                 bcopy((caddr_t) ina->x_host.c_host,
 1049                                       (caddr_t) ac->ac_enaddr,
 1050                                       sizeof(ac->ac_enaddr));
 1051                         }
 1052 
 1053                         /*
 1054                          * Set new address
 1055                          */
 1056                         ifp->if_init(ifp->if_softc);
 1057                         break;
 1058                         }
 1059 #endif
 1060                 default:
 1061                         ifp->if_init(ifp->if_softc);
 1062                         break;
 1063                 }
 1064                 break;
 1065 
 1066         case SIOCGIFADDR:
 1067                 {
 1068                         struct sockaddr *sa;
 1069 
 1070                         sa = (struct sockaddr *) & ifr->ifr_data;
 1071                         bcopy(IFP2AC(ifp)->ac_enaddr,
 1072                               (caddr_t) sa->sa_data, ETHER_ADDR_LEN);
 1073                 }
 1074                 break;
 1075 
 1076         case SIOCSIFMTU:
 1077                 /*
 1078                  * Set the interface MTU.
 1079                  */
 1080                 if (ifr->ifr_mtu > ETHERMTU) {
 1081                         error = EINVAL;
 1082                 } else {
 1083                         ifp->if_mtu = ifr->ifr_mtu;
 1084                 }
 1085                 break;
 1086         default:
 1087                 error = EINVAL;                 /* XXX netbsd has ENOTTY??? */
 1088                 break;
 1089         }
 1090         return (error);
 1091 }
 1092 
 1093 static int
 1094 ether_resolvemulti(struct ifnet *ifp, struct sockaddr **llsa,
 1095         struct sockaddr *sa)
 1096 {
 1097         struct sockaddr_dl *sdl;
 1098 #ifdef INET
 1099         struct sockaddr_in *sin;
 1100 #endif
 1101 #ifdef INET6
 1102         struct sockaddr_in6 *sin6;
 1103 #endif
 1104         u_char *e_addr;
 1105 
 1106         switch(sa->sa_family) {
 1107         case AF_LINK:
 1108                 /*
 1109                  * No mapping needed. Just check that it's a valid MC address.
 1110                  */
 1111                 sdl = (struct sockaddr_dl *)sa;
 1112                 e_addr = LLADDR(sdl);
 1113                 if (!ETHER_IS_MULTICAST(e_addr))
 1114                         return EADDRNOTAVAIL;
 1115                 *llsa = 0;
 1116                 return 0;
 1117 
 1118 #ifdef INET
 1119         case AF_INET:
 1120                 sin = (struct sockaddr_in *)sa;
 1121                 if (!IN_MULTICAST(ntohl(sin->sin_addr.s_addr)))
 1122                         return EADDRNOTAVAIL;
 1123                 MALLOC(sdl, struct sockaddr_dl *, sizeof *sdl, M_IFMADDR,
 1124                        M_WAITOK|M_ZERO);
 1125                 sdl->sdl_len = sizeof *sdl;
 1126                 sdl->sdl_family = AF_LINK;
 1127                 sdl->sdl_index = ifp->if_index;
 1128                 sdl->sdl_type = IFT_ETHER;
 1129                 sdl->sdl_alen = ETHER_ADDR_LEN;
 1130                 e_addr = LLADDR(sdl);
 1131                 ETHER_MAP_IP_MULTICAST(&sin->sin_addr, e_addr);
 1132                 *llsa = (struct sockaddr *)sdl;
 1133                 return 0;
 1134 #endif
 1135 #ifdef INET6
 1136         case AF_INET6:
 1137                 sin6 = (struct sockaddr_in6 *)sa;
 1138                 if (IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) {
 1139                         /*
 1140                          * An IP6 address of 0 means listen to all
 1141                          * of the Ethernet multicast address used for IP6.
 1142                          * (This is used for multicast routers.)
 1143                          */
 1144                         ifp->if_flags |= IFF_ALLMULTI;
 1145                         *llsa = 0;
 1146                         return 0;
 1147                 }
 1148                 if (!IN6_IS_ADDR_MULTICAST(&sin6->sin6_addr))
 1149                         return EADDRNOTAVAIL;
 1150                 MALLOC(sdl, struct sockaddr_dl *, sizeof *sdl, M_IFMADDR,
 1151                        M_WAITOK|M_ZERO);
 1152                 sdl->sdl_len = sizeof *sdl;
 1153                 sdl->sdl_family = AF_LINK;
 1154                 sdl->sdl_index = ifp->if_index;
 1155                 sdl->sdl_type = IFT_ETHER;
 1156                 sdl->sdl_alen = ETHER_ADDR_LEN;
 1157                 e_addr = LLADDR(sdl);
 1158                 ETHER_MAP_IPV6_MULTICAST(&sin6->sin6_addr, e_addr);
 1159                 *llsa = (struct sockaddr *)sdl;
 1160                 return 0;
 1161 #endif
 1162 
 1163         default:
 1164                 /*
 1165                  * Well, the text isn't quite right, but it's the name
 1166                  * that counts...
 1167                  */
 1168                 return EAFNOSUPPORT;
 1169         }
 1170 }
 1171 
 1172 static moduledata_t ether_mod = {
 1173         "ether",
 1174         NULL,
 1175         0
 1176 };
 1177 
 1178 DECLARE_MODULE(ether, ether_mod, SI_SUB_PSEUDO, SI_ORDER_ANY);
 1179 MODULE_VERSION(ether, 1);

Cache object: 0191d652a910796cd3114615e12aab0f


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