]> git.saurik.com Git - apple/xnu.git/blobdiff - bsd/net/ether_if_module.c
xnu-1699.24.23.tar.gz
[apple/xnu.git] / bsd / net / ether_if_module.c
index 8a4da6c6c951a5f05941e90ce7e9d6fe6a8ba19d..a1cbfb3d1508f03ddf00db57e0d1d550d553faa8 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2000-2006 Apple Computer, Inc. All rights reserved.
+ * Copyright (c) 2000-2010 Apple Inc. All rights reserved.
  *
  * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
  * 
@@ -72,6 +72,7 @@
 
 #include <pexpert/pexpert.h>
 
+#define etherbroadcastaddr     fugly
 #include <net/if.h>
 #include <net/route.h>
 #include <net/if_llc.h>
@@ -82,6 +83,7 @@
 #include <netinet/in.h>        /* For M_LOOP */
 #include <net/kpi_interface.h>
 #include <net/kpi_protocol.h>
+#undef etherbroadcastaddr
 
 /*
 #if INET
@@ -96,6 +98,9 @@
 #include <sys/socketvar.h>
 #include <net/if_vlan_var.h>
 #include <net/if_bond_var.h>
+#if IF_BRIDGE
+#include <net/if_bridgevar.h>
+#endif /* IF_BRIDGE */
 
 #include <net/dlil.h>
 
@@ -111,10 +116,6 @@ extern struct ifqueue atalkintrq;
 #endif
 
 
-#if BRIDGE
-#include <net/bridge.h>
-#endif
-
 #define memcpy(x,y,z)  bcopy(y, x, z)
 
 
@@ -123,8 +124,8 @@ SYSCTL_NODE(_net_link, IFT_ETHER, ether, CTLFLAG_RW|CTLFLAG_LOCKED, 0, "Ethernet
 
 struct en_desc {
        u_int16_t       type;                   /* Type of protocol stored in data */
-       u_long          protocol_family;        /* Protocol family */
-       u_long          data[2];                /* Protocol data */
+       u_int32_t               protocol_family;        /* Protocol family */
+       u_int32_t               data[2];                /* Protocol data */
 };
 
 /* descriptors are allocated in blocks of ETHER_DESC_BLK_SIZE */
@@ -135,13 +136,13 @@ struct en_desc {
 #endif
 
 /*
- * Header for the demux list, hangs off of IFP at family_cookie
+ * Header for the demux list, hangs off of IFP at if_family_cookie
  */
 
 struct ether_desc_blk_str {
-       u_long  n_max_used;
-       u_long  n_count;
-       u_long  n_used;
+       u_int32_t  n_max_used;
+       u_int32_t       n_count;
+       u_int32_t       n_used;
        struct en_desc  block_ptr[1];
 };
 /* Size of the above struct before the array of struct en_desc */
@@ -149,19 +150,6 @@ struct ether_desc_blk_str {
 __private_extern__ u_char      etherbroadcastaddr[ETHER_ADDR_LEN] =
                                                                { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
 
-static __inline__ int
-_ether_cmp(const void * a, const void * b)
-{
-       const u_int16_t * a_s = (const u_int16_t *)a;
-       const u_int16_t * b_s = (const u_int16_t *)b;
-       
-       if (a_s[0] != b_s[0]
-           || a_s[1] != b_s[1]
-           || a_s[2] != b_s[2]) {
-               return (1);
-       }
-       return (0);
-}
 
 /*
  * Release all descriptor entries owned by this protocol (there may be several).
@@ -173,8 +161,8 @@ ether_del_proto(
        ifnet_t ifp,
        protocol_family_t protocol_family)
 {
-       struct ether_desc_blk_str *desc_blk = (struct ether_desc_blk_str *)ifp->family_cookie;
-       u_long  current = 0;
+       struct ether_desc_blk_str *desc_blk = (struct ether_desc_blk_str *)ifp->if_family_cookie;
+       u_int32_t       current = 0;
        int found = 0;
        
        if (desc_blk == NULL)
@@ -189,8 +177,8 @@ ether_del_proto(
        }
        
        if (desc_blk->n_used == 0) {
-               FREE(ifp->family_cookie, M_IFADDR);
-               ifp->family_cookie = 0;
+               FREE(ifp->if_family_cookie, M_IFADDR);
+               ifp->if_family_cookie = 0;
        }
        else {
                /* Decrement n_max_used */
@@ -209,7 +197,7 @@ ether_add_proto_internal(
        const struct ifnet_demux_desc   *demux)
 {
        struct en_desc *ed;
-       struct ether_desc_blk_str *desc_blk = (struct ether_desc_blk_str *)ifp->family_cookie;
+       struct ether_desc_blk_str *desc_blk = (struct ether_desc_blk_str *)ifp->if_family_cookie;
        u_int32_t i;
        
        switch (demux->type) {
@@ -265,9 +253,9 @@ ether_add_proto_internal(
        // Check for case where all of the descriptor blocks are in use
        if (desc_blk == NULL || desc_blk->n_used == desc_blk->n_count) {
                struct ether_desc_blk_str *tmp;
-               u_long  new_count = ETHER_DESC_BLK_SIZE;
-               u_long  new_size;
-               u_long  old_size = 0;
+               u_int32_t       new_count = ETHER_DESC_BLK_SIZE;
+               u_int32_t       new_size;
+               u_int32_t       old_size = 0;
                
                i = 0;
                
@@ -293,7 +281,7 @@ ether_add_proto_internal(
                        FREE(desc_blk, M_IFADDR);
                }
                desc_blk = tmp;
-               ifp->family_cookie = (u_long)desc_blk;
+               ifp->if_family_cookie = (uintptr_t)desc_blk;
                desc_blk->n_count = new_count;
        }
        else {
@@ -373,9 +361,9 @@ ether_demux(
        u_short                 ether_type = eh->ether_type;
        u_int16_t               type;
        u_int8_t                *data;
-       u_long                  i = 0;
-       struct ether_desc_blk_str *desc_blk = (struct ether_desc_blk_str *)ifp->family_cookie;
-       u_long                  maxd = desc_blk ? desc_blk->n_max_used : 0;
+       u_int32_t                       i = 0;
+       struct ether_desc_blk_str *desc_blk = (struct ether_desc_blk_str *)ifp->if_family_cookie;
+       u_int32_t                       maxd = desc_blk ? desc_blk->n_max_used : 0;
        struct en_desc  *ed = desc_blk ? desc_blk->block_ptr : NULL;
        u_int32_t               extProto1 = 0;
        u_int32_t               extProto2 = 0;
@@ -388,6 +376,16 @@ ether_demux(
                        m->m_flags |= M_MCAST;
        }
 
+       if (m->m_flags & M_HASFCS) {
+                /*
+                 * If the M_HASFCS is set by the driver we want to make sure
+                 * that we strip off the trailing FCS data before handing it
+                 * up the stack.
+                 */
+                m_adj(m, -ETHER_CRC_LEN);
+               m->m_flags &= ~M_HASFCS;
+        }
+
        if (ifp->if_eflags & IFEF_BOND) {
                /* if we're bonded, bond "protocol" gets all the packets */
                *protocol_family = PF_BOND;
@@ -406,11 +404,35 @@ ether_demux(
                }
        }
        
-       /* Quick check for VLAN */
-       if ((m->m_pkthdr.csum_flags & CSUM_VLAN_TAG_VALID) != 0 ||
-               ether_type == htons(ETHERTYPE_VLAN)) {
-               *protocol_family = PF_VLAN;
-               return 0;
+       /* check for VLAN */
+       if ((m->m_pkthdr.csum_flags & CSUM_VLAN_TAG_VALID) != 0) {
+               if (EVL_VLANOFTAG(m->m_pkthdr.vlan_tag) != 0) {
+                       *protocol_family = PF_VLAN;
+                       return (0);
+               }
+               /* the packet is just priority-tagged, clear the bit */
+               m->m_pkthdr.csum_flags &= ~CSUM_VLAN_TAG_VALID;
+       }
+       else if (ether_type == htons(ETHERTYPE_VLAN)) {
+               struct ether_vlan_header *      evl;
+
+               evl = (struct ether_vlan_header *)frame_header;
+               if (m->m_len < ETHER_VLAN_ENCAP_LEN
+                   || ntohs(evl->evl_proto) == ETHERTYPE_VLAN
+                   || EVL_VLANOFTAG(ntohs(evl->evl_tag)) != 0) {
+                       *protocol_family = PF_VLAN;
+                       return 0;
+               }
+               /* the packet is just priority-tagged */
+
+               /* make the encapsulated ethertype the actual ethertype */
+               ether_type = evl->evl_encap_proto = evl->evl_proto;
+
+               /* remove the encapsulation header */
+               m->m_len -= ETHER_VLAN_ENCAP_LEN;
+               m->m_data += ETHER_VLAN_ENCAP_LEN;
+               m->m_pkthdr.len -= ETHER_VLAN_ENCAP_LEN;
+               m->m_pkthdr.csum_flags = 0; /* can't trust hardware checksum */
        }
        
        data = mtod(m, u_int8_t*);
@@ -610,6 +632,9 @@ __private_extern__ int ether_family_init(void)
 #if BOND
        bond_family_init();
 #endif /* BOND */
+#if IF_BRIDGE
+       bridgeattach(0);
+#endif /* IF_BRIDGE */
 
  done: