]> git.saurik.com Git - apple/xnu.git/blobdiff - bsd/net/ether_if_module.c
xnu-792.10.96.tar.gz
[apple/xnu.git] / bsd / net / ether_if_module.c
index 3964774c4a94a3d8fa2090fc36bbe016c9be02d5..0e50b415b248d146f05c8ac8caf6574047cedd0a 100644 (file)
 #include <sys/sysctl.h>
 
 #include <net/if.h>
-#include <net/netisr.h>
 #include <net/route.h>
 #include <net/if_llc.h>
 #include <net/if_dl.h>
 #include <net/if_types.h>
-#include <net/ndrv.h>
+#include <net/if_ether.h>
 #include <netinet/if_ether.h>
+#include <netinet/in.h>        /* For M_LOOP */
 
 /*
 #if INET
 */
 
 #include <sys/socketvar.h>
+#include <net/if_vlan_var.h>
+#include <net/if_bond_var.h>
 
 #include <net/dlil.h>
 
-
 #if LLC && CCITT
 extern struct ifqueue pkintrq;
 #endif
@@ -104,338 +105,422 @@ extern struct ifqueue atalkintrq;
 #include <net/bridge.h>
 #endif
 
-/* #include "vlan.h" */
-#if NVLAN > 0
-#include <net/if_vlan_var.h>
-#endif /* NVLAN > 0 */
-
-static u_long lo_dlt = 0;
-static ivedonethis = 0;
-
-#define IFP2AC(IFP) ((struct arpcom *)IFP)
+#define memcpy(x,y,z)  bcopy(y, x, z)
 
-u_char etherbroadcastaddr[6] = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
 
+SYSCTL_DECL(_net_link);
+SYSCTL_NODE(_net_link, IFT_ETHER, ether, CTLFLAG_RW, 0, "Ethernet");
 
-#define DB_HEADER_SIZE 20
 struct en_desc {
-    short           total_len;
-    u_short         ethertype;
-    u_long         dl_tag;
-    struct ifnet    *ifp;
-    struct if_proto *proto;
-    u_long          proto_id_length;
-    u_long           proto_id_data[8]; /* probably less - proto-id and bitmasks */
+       u_int16_t       type;                   /* Type of protocol stored in data */
+       u_long          protocol_family;        /* Protocol family */
+       u_long          data[2];                /* Protocol data */
 };
-    
-#define LITMUS_SIZE 16
-#define ETHER_DESC_BLK_SIZE 50
-#define MAX_INTERFACES 50
+/* descriptors are allocated in blocks of ETHER_DESC_BLK_SIZE */
+#define ETHER_DESC_BLK_SIZE (10) 
 
 /*
- * Statics for demux module
+ * Header for the demux list, hangs off of IFP at family_cookie
  */
 
 struct ether_desc_blk_str {
-    u_long   n_blocks;
-    u_long   *block_ptr;
-};
-
-struct dl_es_at_entry 
-{
-     struct ifnet *ifp;
-     u_long      dl_tag;
-     int    ref_count;
+       u_long  n_max_used;
+       u_long  n_count;
+       u_long  n_used;
+       struct en_desc  block_ptr[1];
 };
+/* Size of the above struct before the array of struct en_desc */
+#define ETHER_DESC_HEADER_SIZE ((size_t)&(((struct ether_desc_blk_str*)0)->block_ptr[0]))
+__private_extern__ u_char      etherbroadcastaddr[ETHER_ADDR_LEN] =
+                                                               { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
 
-
-static struct ether_desc_blk_str ether_desc_blk[MAX_INTERFACES];
-static u_long  litmus_mask[LITMUS_SIZE];
-static u_long  litmus_length = 0;
-
+int ether_add_proto_old(struct ifnet *ifp, u_long protocol_family, struct ddesc_head_str *desc_head);
+int ether_add_if(struct ifnet *ifp);
+int ether_del_if(struct ifnet *ifp);
+int ether_init_if(struct ifnet *ifp);
+int ether_family_init(void);
 
 /*
- * Temp static for protocol registration XXX
+ * Release all descriptor entries owned by this protocol (there may be several).
+ * Setting the type to 0 releases the entry. Eventually we should compact-out
+ * the unused entries.
  */
+int
+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;
+       int found = 0;
+       
+       if (desc_blk == NULL)
+               return 0;
+       
+       for (current = desc_blk->n_max_used; current > 0; current--) {
+               if (desc_blk->block_ptr[current - 1].protocol_family == protocol_family) {
+                       found = 1;
+                       desc_blk->block_ptr[current - 1].type = 0;
+                       desc_blk->n_used--;
+               }
+       }
+       
+       if (desc_blk->n_used == 0) {
+               FREE(ifp->family_cookie, M_IFADDR);
+               ifp->family_cookie = 0;
+       }
+       else {
+               /* Decrement n_max_used */
+               for (; desc_blk->n_max_used > 0 && desc_blk->block_ptr[desc_blk->n_max_used - 1].type == 0; desc_blk->n_max_used--)
+                       ;
+       }
+       
+       return 0;
+ }
 
-#define MAX_EN_COUNT 30
-
-static struct dl_es_at_entry en_at_array[MAX_EN_COUNT];
-
-/*
- * This could be done below in-line with heavy casting, but the pointer arithmetic is 
- * prone to error.
- */
 
-static
-int  desc_in_bounds(block, current_ptr, offset_length)
-    u_int  block;
-    char   *current_ptr;
-    u_long offset_length;
+static int
+ether_add_proto_internal(
+       struct ifnet                                    *ifp,
+       protocol_family_t                               protocol,
+       const struct ifnet_demux_desc   *demux)
 {
-    u_long end_of_block;
-    u_long current_ptr_tmp;
-
-    current_ptr_tmp = (u_long) current_ptr;
-    end_of_block = (u_long) ether_desc_blk[block].block_ptr;
-    end_of_block += (ETHER_DESC_BLK_SIZE * ether_desc_blk[block].n_blocks);
-    if ((current_ptr_tmp + offset_length) < end_of_block)
-       return 1;
-    else
+       struct en_desc *ed;
+       struct ether_desc_blk_str *desc_blk = (struct ether_desc_blk_str *)ifp->family_cookie;
+       u_int32_t i;
+       
+       switch (demux->type) {
+               /* These types are supported */
+               /* Top three are preferred */
+               case DLIL_DESC_ETYPE2:
+                       if (demux->datalen != 2) {
+                               return EINVAL;
+                       }
+                       break;
+               
+               case DLIL_DESC_SAP:
+                       if (demux->datalen != 3) {
+                               return EINVAL;
+                       }
+                       break;
+               
+               case DLIL_DESC_SNAP:
+                       if (demux->datalen != 5) {
+                               return EINVAL;
+                       }
+                       break;
+                       
+               default:
+                       return ENOTSUP;
+       }
+       
+       // Verify a matching descriptor does not exist.
+       if (desc_blk != NULL) {
+               switch (demux->type) {
+                       case DLIL_DESC_ETYPE2:
+                               for (i = 0; i < desc_blk->n_max_used; i++) {
+                                       if (desc_blk->block_ptr[i].type == DLIL_DESC_ETYPE2 &&
+                                               desc_blk->block_ptr[i].data[0] ==
+                                               *(u_int16_t*)demux->data) {
+                                               return EADDRINUSE;
+                                       }
+                               }
+                               break;
+                       case DLIL_DESC_SAP:
+                       case DLIL_DESC_SNAP:
+                               for (i = 0; i < desc_blk->n_max_used; i++) {
+                                       if (desc_blk->block_ptr[i].type == demux->type &&
+                                               bcmp(desc_blk->block_ptr[i].data, demux->data,
+                                                        demux->datalen) == 0) {
+                                               return EADDRINUSE;
+                                       }
+                               }
+                               break;
+               }
+       }
+       
+       // 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;
+               
+               i = 0;
+               
+               if (desc_blk) {
+                       new_count += desc_blk->n_count;
+                       old_size = desc_blk->n_count * sizeof(struct en_desc) + ETHER_DESC_HEADER_SIZE;
+                       i = desc_blk->n_used;
+               }
+               
+               new_size = new_count * sizeof(struct en_desc) + ETHER_DESC_HEADER_SIZE;
+               
+               tmp = _MALLOC(new_size, M_IFADDR, M_WAITOK);
+               if (tmp  == 0) {
+                       /*
+                        * Remove any previous descriptors set in the call.
+                        */
+                       return ENOMEM;
+               }
+               
+               bzero(tmp + old_size, new_size - old_size);
+               if (desc_blk) {
+                       bcopy(desc_blk, tmp, old_size);
+                       FREE(desc_blk, M_IFADDR);
+               }
+               desc_blk = tmp;
+               ifp->family_cookie = (u_long)desc_blk;
+               desc_blk->n_count = new_count;
+       }
+       else {
+               /* Find a free entry */
+               for (i = 0; i < desc_blk->n_count; i++) {
+                       if (desc_blk->block_ptr[i].type == 0) {
+                               break;
+                       }
+               }
+       }
+       
+       /* Bump n_max_used if appropriate */
+       if (i + 1 > desc_blk->n_max_used) {
+               desc_blk->n_max_used = i + 1;
+       }
+       
+       ed = &desc_blk->block_ptr[i];
+       ed->protocol_family = protocol;
+       ed->data[0] = 0;
+       ed->data[1] = 0;
+       
+       switch (demux->type) {
+               case DLIL_DESC_ETYPE2:
+                       /* 2 byte ethernet raw protocol type is at native_type */
+                       /* prtocol must be in network byte order */
+                       ed->type = DLIL_DESC_ETYPE2;
+                       ed->data[0] = *(u_int16_t*)demux->data;
+                       break;
+               
+               case DLIL_DESC_SAP:
+                       ed->type = DLIL_DESC_SAP;
+                       bcopy(demux->data, &ed->data[0], 3);
+                       break;
+               
+               case DLIL_DESC_SNAP: {
+                       u_int8_t*       pDest = ((u_int8_t*)&ed->data[0]) + 3;
+                       ed->type = DLIL_DESC_SNAP;
+                       bcopy(demux->data, pDest, 5);
+                       }
+                       break;
+       }
+       
+       desc_blk->n_used++;
+       
        return 0;
 }
 
-
-/*
- * Release all descriptor entries owned by this dl_tag (there may be several).
- * Setting the dl_tag to 0 releases the entry. Eventually we should compact-out
- * the unused entries.
- */
-static
-int  ether_del_proto(struct if_proto *proto, u_long dl_tag)
+int
+ether_add_proto(
+       ifnet_t                         ifp,
+       protocol_family_t       protocol,
+       const struct ifnet_demux_desc *demux_list,
+       u_int32_t                       demux_count)
 {
-    char *current_ptr = (char *) ether_desc_blk[proto->ifp->family_cookie].block_ptr;
-    struct en_desc        *ed;
-    int i;
-    int found = 0;
-
-    ed = (struct en_desc *) current_ptr;
-
-    while(ed->total_len) {
-       if (ed->dl_tag == dl_tag) {
-           found = 1;
-           ed->dl_tag = 0;
+       int                     error = 0;
+       u_int32_t       i;
+       
+       for (i = 0; i < demux_count; i++) {
+               error = ether_add_proto_internal(ifp, protocol, &demux_list[i]);
+               if (error) {
+                       ether_del_proto(ifp, protocol);
+                       break;
+               }
        }
+       
+       return error;
+}
 
-       current_ptr += ed->total_len;
-       ed = (struct en_desc *) current_ptr;
-    }
- }
-
-
-
-static
-int  ether_add_proto(struct ddesc_head_str *desc_head, struct if_proto *proto, u_long dl_tag)
+__private_extern__ int
+ether_add_proto_old(
+       struct ifnet *ifp,
+       u_long protocol_family,
+       struct ddesc_head_str *desc_head)
 {
-   char *current_ptr;
-   struct dlil_demux_desc  *desc;
-   u_long                 id_length; /* IN LONGWORDS!!! */
-   struct en_desc         *ed;
-   u_long                 *bitmask;
-   u_long                 *proto_id;
-   int                    i;
-   short                  total_length;
-   u_long                 block_count;
-   u_long                  *tmp;
-
-
-   TAILQ_FOREACH(desc, desc_head, next) {
-       switch (desc->type) 
-       {
-       case DLIL_DESC_RAW:
-          id_length   = desc->variants.bitmask.proto_id_length;
-          break;
-          
-       case DLIL_DESC_802_2:
-          id_length = 1; 
-          break;
-          
-       case DLIL_DESC_802_2_SNAP:
-          id_length = 2;
-          break;
-          
-       default:
-          return EINVAL;
-       }
-
-restart:
-       block_count = ether_desc_blk[proto->ifp->family_cookie].n_blocks;
-       current_ptr =  (char *) ether_desc_blk[proto->ifp->family_cookie].block_ptr;
-       ed = (struct en_desc *) current_ptr;
-       total_length = ((id_length << 2) * 2) + DB_HEADER_SIZE;
-
-       while ((ed->total_len) && (desc_in_bounds(proto->ifp->family_cookie, 
-                             current_ptr, total_length))) {
-          if ((ed->dl_tag == 0) && (total_length <= ed->total_len)) 
-              break;
-          else
-              current_ptr += *(short *)current_ptr;
-          
-          ed = (struct en_desc *) current_ptr;
-       }
-
-       if (!desc_in_bounds(proto->ifp->family_cookie, current_ptr, total_length)) {
-
-          tmp = _MALLOC((ETHER_DESC_BLK_SIZE * (block_count + 1)), 
-                        M_IFADDR, M_WAITOK);
-          if (tmp  == 0) {
-              /*
-               * Remove any previous descriptors set in the call.
-               */
-              ether_del_proto(proto, dl_tag);
-              return ENOMEM;
-          }
-
-          bzero(tmp, ETHER_DESC_BLK_SIZE * (block_count + 1));
-          bcopy(ether_desc_blk[proto->ifp->family_cookie].block_ptr, 
-                tmp, (ETHER_DESC_BLK_SIZE * block_count));
-          FREE(ether_desc_blk[proto->ifp->family_cookie].block_ptr, M_IFADDR);
-          ether_desc_blk[proto->ifp->family_cookie].n_blocks = block_count + 1;
-          ether_desc_blk[proto->ifp->family_cookie].block_ptr = tmp;
-          goto restart;
-       }
-
-       if (ed->total_len == 0)
-          ed->total_len = total_length;
-       ed->ethertype = *((u_short *) desc->native_type);
-
-       ed->dl_tag    = dl_tag;
-       ed->proto     = proto;
-       ed->proto_id_length = id_length;
-       ed->ifp       = proto->ifp;
-
-       switch (desc->type)
-       {
-       case DLIL_DESC_RAW:
-          bcopy(desc->variants.bitmask.proto_id, &ed->proto_id_data[0], (id_length << 2) );
-          bcopy(desc->variants.bitmask.proto_id_mask, &ed->proto_id_data[id_length],
-                (id_length << 2));
-          break;
-
-       case DLIL_DESC_802_2:
-          ed->proto_id_data[0] = 0;
-          bcopy(&desc->variants.desc_802_2, &ed->proto_id_data[0], 3);
-          ed->proto_id_data[1] = 0xffffff00;
-          break;
-
-       case DLIL_DESC_802_2_SNAP:
-          /* XXX Add verification of fixed values here */
-
-          ed->proto_id_data[0] = 0;
-          ed->proto_id_data[1] = 0;
-          bcopy(&desc->variants.desc_802_2_SNAP, &ed->proto_id_data[0], 8);
-          ed->proto_id_data[2] = 0xffffffff;
-          ed->proto_id_data[3] = 0xffffffff;;
-          break;  
-       }
-       
-       if (id_length) {
-          proto_id = (u_long *) &ed->proto_id_data[0];
-          bitmask  = (u_long *) &ed->proto_id_data[id_length];
-          for (i=0; i < (id_length); i++) {
-              litmus_mask[i] &= bitmask[i];
-              litmus_mask[i] &= proto_id[i];
-          }
-          if (id_length > litmus_length)
-              litmus_length = id_length;
-       }
-   }   
-
-   return 0;
+       struct dlil_demux_desc  *desc;
+       int error = 0;
+       
+       TAILQ_FOREACH(desc, desc_head, next) {
+               struct ifnet_demux_desc dmx;
+               int swapped = 0;
+               
+               // Convert dlil_demux_desc to ifnet_demux_desc
+               dmx.type = desc->type;
+               dmx.datalen = desc->variants.native_type_length;
+               dmx.data = desc->native_type;
+               
+#ifdef DLIL_DESC_RAW
+               if (dmx.type == DLIL_DESC_RAW) {
+                       swapped = 1;
+                       dmx.type = DLIL_DESC_ETYPE2;
+                       dmx.datalen = 2;
+                       *(u_int16_t*)dmx.data = htons(*(u_int16_t*)dmx.data);
+               }
+#endif
+               
+               error = ether_add_proto_internal(ifp, protocol_family, &dmx);
+               if (swapped) {
+                       *(u_int16_t*)dmx.data = ntohs(*(u_int16_t*)dmx.data);
+                       swapped = 0;
+               }
+               if (error) {
+                       ether_del_proto(ifp, protocol_family);
+                       break;
+               }
+       }
+       
+       return error;
 } 
 
 
-static
-int  ether_shutdown()
+static int
+ether_shutdown(void)
 {
     return 0;
 }
 
 
-
-
-
-int ether_demux(ifp, m, frame_header, proto)
-    struct ifnet *ifp;
-    struct mbuf  *m;
-    char         *frame_header;
-    struct if_proto **proto;
-
+int
+ether_demux(
+       ifnet_t                         ifp,
+       mbuf_t                          m,
+       char                            *frame_header,
+       protocol_family_t       *protocol_family)
 {
-    register struct ether_header *eh = (struct ether_header *)frame_header;
-    u_short ether_type;
-    char *current_ptr = (char *) ether_desc_blk[ifp->family_cookie].block_ptr;
-    struct dlil_demux_desc  *desc;
-    register u_long          temp;
-    u_long                 *data;
-    register struct if_proto *ifproto;
-    u_long                  i;
-    struct en_desc          *ed;
-
-
-    if (eh->ether_dhost[0] & 1) {
-       if (bcmp((caddr_t)etherbroadcastaddr, (caddr_t)eh->ether_dhost,
-                sizeof(etherbroadcastaddr)) == 0)
-           m->m_flags |= M_BCAST;
-       else
-           m->m_flags |= M_MCAST;
-    }
-
-    ether_type = ntohs(eh->ether_type);
-
-    /* 
-     * Search through the connected protocols for a match. 
-     */
-
-
-    data = mtod(m, u_long *);
-    ed = (struct en_desc *) current_ptr;
-    while (desc_in_bounds(ifp->family_cookie, current_ptr, DB_HEADER_SIZE)) {
-       if (ed->total_len == 0)
-           break;
+       struct ether_header *eh = (struct ether_header *)frame_header;
+       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;
+       struct en_desc  *ed = desc_blk ? desc_blk->block_ptr : NULL;
+       u_int32_t               extProto1 = 0;
+       u_int32_t               extProto2 = 0;
+
+       if (eh->ether_dhost[0] & 1) {
+               /* Check for broadcast */
+               if (*(u_int32_t*)eh->ether_dhost == 0xFFFFFFFF &&
+                       *(u_int16_t*)(eh->ether_dhost + sizeof(u_int32_t)) == 0xFFFF)
+                       m->m_flags |= M_BCAST;
+               else
+                       m->m_flags |= M_MCAST;
+       }
 
-       if ((ed->dl_tag !=  0) && (ed->ifp == ifp) && 
-           ((ed->ethertype == ntohs(eh->ether_type)) || (ed->ethertype == 0))) {
-           if (ed->proto_id_length) {
-               for (i=0; i < (ed->proto_id_length); i++) {
-                   temp = ntohs(data[i]) & ed->proto_id_data[ed->proto_id_length + i];
-                   if ((temp ^ ed->proto_id_data[i]))
-                       break;
-               }
+       if (ifp->if_eflags & IFEF_BOND) {
+               /* if we're bonded, bond "protocol" gets all the packets */
+               *protocol_family = PF_BOND;
+               return (0);
+       }
 
-               if (i >= (ed->proto_id_length)) {
-                   *proto = ed->proto;
-                   return 0;
+       if ((eh->ether_dhost[0] & 1) == 0) {
+               /*
+               * When the driver is put into promiscuous mode we may receive unicast
+               * frames that are not intended for our interfaces.  They are marked here
+               * as being promiscuous so the caller may dispose of them after passing
+               * the packets to any interface filters.
+               */
+               #define ETHER_CMP(x, y) ( ((u_int16_t *) x)[0] != ((u_int16_t *) y)[0] || \
+                                                                 ((u_int16_t *) x)[1] != ((u_int16_t *) y)[1] || \
+                                                                 ((u_int16_t *) x)[2] != ((u_int16_t *) y)[2] )
+               
+               if (ETHER_CMP(eh->ether_dhost, ifnet_lladdr(ifp))) {
+                       m->m_flags |= M_PROMISC;
                }
-           }
-           else {
-               *proto = ed->proto;
+       }
+       
+       /* 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;
-           }
        }
-       current_ptr += ed->total_len;
-       ed = (struct en_desc *) current_ptr;
-    }
-
-/*
-    kprintf("ether_demux - No match for <%x><%x><%x><%x><%x><%x><%x<%x>\n",
-           eh->ether_type,data[0], data[1], data[2], data[3], data[4],data[5],data[6]);
-*/
-
-    return ENOENT;
+       
+       data = mtod(m, u_int8_t*);
+       
+       /*
+       * Determine the packet's protocol type and stuff the protocol into
+       * longs for quick compares.
+       */
+       
+       if (ntohs(ether_type) <= 1500) {
+               extProto1 = *(u_int32_t*)data;
+               
+               // SAP or SNAP
+               if ((extProto1 & htonl(0xFFFFFF00)) == htonl(0xAAAA0300)) {
+                       // SNAP
+                       type = DLIL_DESC_SNAP;
+                       extProto2 = *(u_int32_t*)(data + sizeof(u_int32_t));
+                       extProto1 &= htonl(0x000000FF);
+               } else {
+                       type = DLIL_DESC_SAP;
+                       extProto1 &= htonl(0xFFFFFF00);
+               }
+       } else {
+               type = DLIL_DESC_ETYPE2;
+       }
+       
+       /* 
+       * Search through the connected protocols for a match. 
+       */
+       
+       switch (type) {
+               case DLIL_DESC_ETYPE2:
+                       for (i = 0; i < maxd; i++) {
+                               if ((ed[i].type == type) && (ed[i].data[0] == ether_type)) {
+                                       *protocol_family = ed[i].protocol_family;
+                                       return 0;
+                               }
+                       }
+                       break;
+               
+               case DLIL_DESC_SAP:
+                       for (i = 0; i < maxd; i++) {
+                               if ((ed[i].type == type) && (ed[i].data[0] == extProto1)) {
+                                       *protocol_family = ed[i].protocol_family;
+                                       return 0;
+                               }
+                       }
+                       break;
+               
+               case DLIL_DESC_SNAP:
+                       for (i = 0; i < maxd; i++) {
+                               if ((ed[i].type == type) && (ed[i].data[0] == extProto1) &&
+                                       (ed[i].data[1] == extProto2)) {
+                                       *protocol_family = ed[i].protocol_family;
+                                       return 0;
+                               }
+                       }
+               break;
+       }
+       
+       return ENOENT;
 }                      
 
-
-
 /*
  * Ethernet output routine.
  * Encapsulate a packet of type family for the local net.
  * Use trailer local net encapsulation if enough data in first
  * packet leaves a multiple of 512 bytes of data in remainder.
- * Assumes that ifp is actually pointer to arpcom structure.
  */
 int
-ether_frameout(ifp, m, ndest, edst, ether_type)
-       register struct ifnet   *ifp;
-       struct mbuf             **m;
-       struct sockaddr         *ndest;
-       char                    *edst;
-       char                    *ether_type;
+ether_frameout(
+       struct ifnet                    *ifp,
+       struct mbuf                             **m,
+       const struct sockaddr   *ndest,
+       const char                              *edst,
+       const char                              *ether_type)
 {
-       register struct ether_header *eh;
-       int hlen;       /* link layer header lenght */
-       struct arpcom *ac = IFP2AC(ifp);
-
+       struct ether_header *eh;
+       int hlen;       /* link layer header length */
 
        hlen = ETHER_HDR_LEN;
 
@@ -450,25 +535,21 @@ ether_frameout(ifp, m, ndest, edst, ether_type)
         */
        if ((ifp->if_flags & IFF_SIMPLEX) &&
            ((*m)->m_flags & M_LOOP)) {
-           if (lo_dlt == 0) 
-               dlil_find_dltag(APPLE_IF_FAM_LOOPBACK, 0, PF_INET, &lo_dlt);
-
-           if (lo_dlt) {
-               if ((*m)->m_flags & M_BCAST) {
-                   struct mbuf *n = m_copy(*m, 0, (int)M_COPYALL);
-                   dlil_output(lo_dlt, n, 0, ndest, 0);
-               } 
-               else 
-               {
-                   if (bcmp(edst,  ac->ac_enaddr, ETHER_ADDR_LEN) == 0) {
-                       dlil_output(lo_dlt, *m, 0, ndest, 0);
-                       return EJUSTRETURN;
-                   }
-               }
+           if (lo_ifp) {
+            if ((*m)->m_flags & M_BCAST) {
+                struct mbuf *n = m_copy(*m, 0, (int)M_COPYALL);
+                if (n != NULL)
+                    dlil_output(lo_ifp, ndest->sa_family, n, 0, ndest, 0);
+            }
+            else {
+                if (bcmp(edst, ifnet_lladdr(ifp), ETHER_ADDR_LEN) == 0) {
+                    dlil_output(lo_ifp, ndest->sa_family, *m, 0, ndest, 0);
+                    return EJUSTRETURN;
+                }
+            }
            }
        }
-
-
+    
        /*
         * Add local net header.  If no space in first mbuf,
         * allocate another.
@@ -483,136 +564,139 @@ ether_frameout(ifp, m, ndest, edst, ether_type)
        (void)memcpy(&eh->ether_type, ether_type,
                sizeof(eh->ether_type));
        (void)memcpy(eh->ether_dhost, edst, 6);
-       (void)memcpy(eh->ether_shost, ac->ac_enaddr,
-           sizeof(eh->ether_shost));
+       ifnet_lladdr_copy_bytes(ifp, eh->ether_shost, ETHER_ADDR_LEN);
 
        return 0;
 }
 
 
-static
-int  ether_add_if(struct ifnet *ifp)
+__private_extern__ int
+ether_add_if(struct ifnet *ifp)
 {
-    u_long  i;
-
-    ifp->if_framer = ether_frameout;
-    ifp->if_demux  = ether_demux;
-    ifp->if_event  = 0;
-
-    for (i=0; i < MAX_INTERFACES; i++)
-       if (ether_desc_blk[i].n_blocks == 0)
-           break;
-
-    if (i == MAX_INTERFACES)
-       return ENOMEM;
-
-    ether_desc_blk[i].block_ptr = _MALLOC(ETHER_DESC_BLK_SIZE, M_IFADDR, M_WAITOK);
-    if (ether_desc_blk[i].block_ptr == 0)
-       return ENOMEM;
-
-    ether_desc_blk[i].n_blocks = 1;
-    bzero(ether_desc_blk[i].block_ptr, ETHER_DESC_BLK_SIZE);
-
-    ifp->family_cookie = i;
+       ifp->if_framer = ether_frameout;
+       ifp->if_demux = ether_demux;
     
     return 0;
 }
 
-static
-int  ether_del_if(struct ifnet *ifp)
+__private_extern__ int
+ether_del_if(struct ifnet *ifp)
 {
-    if ((ifp->family_cookie < MAX_INTERFACES) &&
-       (ether_desc_blk[ifp->family_cookie].n_blocks)) {
-       FREE(ether_desc_blk[ifp->family_cookie].block_ptr, M_IFADDR);
-       ether_desc_blk[ifp->family_cookie].n_blocks = 0;
-       return 0;
-    }
-    else
-       return ENOENT;
+       if (ifp->family_cookie) {
+               FREE(ifp->family_cookie, M_IFADDR);
+               return 0;
+       }
+       else
+               return ENOENT;
 }
 
+__private_extern__ int
+ether_init_if(struct ifnet *ifp)
+{
+       /*
+        * Copy ethernet address out of old style arpcom. New
+        * interfaces created using the KPIs will not have an
+        * interface family. Those interfaces will have the
+        * lladdr passed in when the interface is created.
+        */
+       u_char *enaddr = ((u_char*)ifp) + sizeof(struct ifnet);
+       ifnet_set_lladdr(ifp, enaddr, 6);
+       bzero(enaddr, 6);
+       
+    return 0;
+}
 
 
+errno_t
+ether_check_multi(
+       ifnet_t                                 ifp,
+       const struct sockaddr   *proto_addr)
+{
+       errno_t result = EAFNOSUPPORT;
+       const u_char *e_addr;
+       
+       /*
+        * AF_SPEC and AF_LINK don't require translation. We do
+        * want to verify that they specify a valid multicast.
+        */
+       switch(proto_addr->sa_family) {
+               case AF_UNSPEC:
+                       e_addr = (const u_char*)&proto_addr->sa_data[0];
+                       if ((e_addr[0] & 0x01) != 0x01)
+                               result = EADDRNOTAVAIL;
+                       else
+                               result = 0;
+                       break;
+               
+               case AF_LINK:
+                       e_addr = CONST_LLADDR((const struct sockaddr_dl*)proto_addr); 
+                       if ((e_addr[0] & 0x01) != 0x01)
+                               result = EADDRNOTAVAIL;
+                       else
+                               result = 0;
+                       break;
+       }
+       
+       return result;
+}
+
 int
-ether_ifmod_ioctl(ifp, command, data)
-    struct ifnet *ifp;
-    u_long       command;
-    caddr_t      data;
+ether_ioctl(
+    __unused ifnet_t   ifp,
+    __unused u_int32_t command,
+    __unused void*             data)
 {
-    struct rslvmulti_req *rsreq = (struct rslvmulti_req *) data;
-    int error = 0;
-    struct sockaddr_dl *sdl;
-    struct sockaddr_in *sin;
-    u_char *e_addr;
-
-
-    switch (command) 
-    {
-    case SIOCRSLVMULTI: 
-        switch(rsreq->sa->sa_family) 
-        {
-        case AF_UNSPEC:
-             /* AppleTalk uses AF_UNSPEC for multicast registration.
-              * No mapping needed. Just check that it's a valid MC address.
-              */
-             e_addr = &rsreq->sa->sa_data[0];
-             if ((e_addr[0] & 1) != 1)
-                  return EADDRNOTAVAIL;
-             *rsreq->llsa = 0;
-             return EJUSTRETURN;
-
-
-        case AF_LINK:
-             /* 
-              * No mapping needed. Just check that it's a valid MC address.
-              */
-             sdl = (struct sockaddr_dl *)rsreq->sa;
-             e_addr = LLADDR(sdl);
-             if ((e_addr[0] & 1) != 1)
-                  return EADDRNOTAVAIL;
-             *rsreq->llsa = 0;
-             return EJUSTRETURN;
-             
-        default:
-             return EAFNOSUPPORT;
-        }
-
-    default:
-        return EOPNOTSUPP;
-    }
+       return EOPNOTSUPP;
 }
 
 
-int ether_family_init()
+extern int ether_attach_inet(struct ifnet *ifp, u_long proto_family);
+extern int ether_detach_inet(struct ifnet *ifp, u_long proto_family);
+extern int ether_attach_inet6(struct ifnet *ifp, u_long proto_family);
+extern int ether_detach_inet6(struct ifnet *ifp, u_long proto_family);
+
+extern void kprintf(const char *, ...);
+
+int ether_family_init(void)
 {
-    int  i;
+    int  error=0;
     struct dlil_ifmod_reg_str  ifmod_reg;
 
-    if (ivedonethis)
-       return 0;
-
-    ivedonethis = 1;
+    /* ethernet family is built-in, called from bsd_init */
 
+    bzero(&ifmod_reg, sizeof(ifmod_reg));
     ifmod_reg.add_if = ether_add_if;
     ifmod_reg.del_if = ether_del_if;
-    ifmod_reg.add_proto = ether_add_proto;
+    ifmod_reg.init_if = ether_init_if;
+    ifmod_reg.add_proto = ether_add_proto_old;
     ifmod_reg.del_proto = ether_del_proto;
-    ifmod_reg.ifmod_ioctl = ether_ifmod_ioctl;
+    ifmod_reg.ifmod_ioctl = ether_ioctl;
     ifmod_reg.shutdown    = ether_shutdown;
 
     if (dlil_reg_if_modules(APPLE_IF_FAM_ETHERNET, &ifmod_reg)) {
-       printf("WARNING: ether_family_init -- Can't register if family modules\n");
-       return EIO;
+        printf("WARNING: ether_family_init -- Can't register if family modules\n");
+        error = EIO;
+       goto done;
     }
 
-    for (i=0; i < (LITMUS_SIZE/4); i++)
-       litmus_mask[i] = 0xffffffff;
+       /* Register protocol registration functions */
+
+       if ((error = dlil_reg_proto_module(PF_INET, APPLE_IF_FAM_ETHERNET,
+                                                                         ether_attach_inet, ether_detach_inet)) != 0) {
+               kprintf("dlil_reg_proto_module failed for AF_INET6 error=%d\n", error);
+               goto done;
+       }
+
 
-    for (i=0; i < MAX_INTERFACES; i++)
-       ether_desc_blk[i].n_blocks = 0;
+       if ((error = dlil_reg_proto_module(PF_INET6, APPLE_IF_FAM_ETHERNET,
+                                                                         ether_attach_inet6, ether_detach_inet6)) != 0) {
+               kprintf("dlil_reg_proto_module failed for AF_INET6 error=%d\n", error);
+               goto done;
+       }
+       vlan_family_init();
+       bond_family_init();
 
-    for (i=0; i < MAX_EN_COUNT; i++)
-        en_at_array[i].ifp = 0;
+ done:
 
-    return 0;
+    return (error);
 }