/*
- * Copyright (c) 2000 Apple Computer, Inc. All rights reserved.
+ * Copyright (c) 2000-2011 Apple Inc. All rights reserved.
*
* @APPLE_OSREFERENCE_LICENSE_HEADER_START@
*
#include <sys/sockio.h>
#include <sys/sysctl.h>
+#include <pexpert/pexpert.h>
+
+#define etherbroadcastaddr fugly
#include <net/if.h>
#include <net/route.h>
#include <net/if_llc.h>
#include <net/if_ether.h>
#include <netinet/if_ether.h>
#include <netinet/in.h> /* For M_LOOP */
+#include <net/kpi_interface.h>
+#include <net/kpi_protocol.h>
+#undef etherbroadcastaddr
/*
#if INET
#include <netinet/ip.h>
#endif
*/
-
+#include <net/ether_if_module.h>
#include <sys/socketvar.h>
#include <net/if_vlan_var.h>
-#include <net/if_bond_var.h>
+#if BOND
+#include <net/if_bond_internal.h>
+#endif /* BOND */
+#if IF_BRIDGE
+#include <net/if_bridgevar.h>
+#endif /* IF_BRIDGE */
#include <net/dlil.h>
#endif
-#if BRIDGE
-#include <net/bridge.h>
-#endif
-
#define memcpy(x,y,z) bcopy(y, x, z)
SYSCTL_DECL(_net_link);
-SYSCTL_NODE(_net_link, IFT_ETHER, ether, CTLFLAG_RW, 0, "Ethernet");
+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 */
-#define ETHER_DESC_BLK_SIZE (10)
+#if CONFIG_EMBEDDED
+#define ETHER_DESC_BLK_SIZE (2) /* IP, ARP */
+#else
+#define ETHER_DESC_BLK_SIZE (10)
+#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 */
-#define ETHER_DESC_HEADER_SIZE ((size_t)&(((struct ether_desc_blk_str*)0)->block_ptr[0]))
+#define ETHER_DESC_HEADER_SIZE ((size_t)offsetof(struct ether_desc_blk_str, block_ptr))
__private_extern__ u_char etherbroadcastaddr[ETHER_ADDR_LEN] =
{ 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
-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);
/*
* Release all descriptor entries owned by this protocol (there may be several).
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)
}
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 */
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) {
// 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;
*/
return ENOMEM;
}
-
- bzero(tmp + old_size, new_size - old_size);
+
+ bzero(((char *)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;
+ ifp->if_family_cookie = (uintptr_t)desc_blk;
desc_blk->n_count = new_count;
}
else {
return error;
}
-__private_extern__ int
-ether_add_proto_old(
- struct ifnet *ifp,
- u_long protocol_family,
- struct ddesc_head_str *desc_head)
-{
- 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(void)
-{
- return 0;
-}
-
-
int
ether_demux(
ifnet_t ifp,
char *frame_header,
protocol_family_t *protocol_family)
{
- struct ether_header *eh = (struct ether_header *)frame_header;
+ struct ether_header *eh = (struct ether_header *)(void *)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;
+ 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;
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)
+ if (_ether_cmp(etherbroadcastaddr, eh->ether_dhost) == 0)
m->m_flags |= M_BCAST;
else
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;
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))) {
+ * 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.
+ */
+ if (_ether_cmp(eh->ether_dhost, ifnet_lladdr(ifp))) {
m->m_flags |= M_PROMISC;
}
}
- /* 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 *)(void *)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*);
*/
if (ntohs(ether_type) <= 1500) {
- extProto1 = *(u_int32_t*)data;
+ bcopy(data, &extProto1, sizeof (u_int32_t));
// SAP or SNAP
if ((extProto1 & htonl(0xFFFFFF00)) == htonl(0xAAAA0300)) {
// SNAP
type = DLIL_DESC_SNAP;
- extProto2 = *(u_int32_t*)(data + sizeof(u_int32_t));
+ bcopy(data + sizeof(u_int32_t), &extProto2, sizeof (u_int32_t));
extProto1 &= htonl(0x000000FF);
} else {
type = DLIL_DESC_SAP;
*/
int
ether_frameout(
- struct ifnet *ifp,
- struct mbuf **m,
- const struct sockaddr *ndest,
- const char *edst,
- const char *ether_type)
+ struct ifnet *ifp,
+ struct mbuf **m,
+ const struct sockaddr *ndest,
+ const char *edst,
+ const char *ether_type
+#if KPI_INTERFACE_EMBEDDED
+ ,
+ u_int32_t *prepend_len,
+ u_int32_t *postpend_len
+#endif /* KPI_INTERFACE_EMBEDDED */
+ )
{
struct ether_header *eh;
int hlen; /* link layer header length */
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);
+ dlil_output(lo_ifp, ndest->sa_family, n, NULL, ndest, 0, NULL);
}
else {
- if (bcmp(edst, ifnet_lladdr(ifp), ETHER_ADDR_LEN) == 0) {
- dlil_output(lo_ifp, ndest->sa_family, *m, 0, ndest, 0);
+ if (_ether_cmp(edst, ifnet_lladdr(ifp)) == 0) {
+ dlil_output(lo_ifp, ndest->sa_family, *m, NULL, ndest, 0, NULL);
return EJUSTRETURN;
}
}
return (EJUSTRETURN);
}
-
+#if KPI_INTERFACE_EMBEDDED
+ *prepend_len = sizeof (struct ether_header);
+ *postpend_len = 0;
+#endif /* KPI_INTERFACE_EMBEDDED */
+
eh = mtod(*m, struct ether_header *);
(void)memcpy(&eh->ether_type, ether_type,
sizeof(eh->ether_type));
- (void)memcpy(eh->ether_dhost, edst, 6);
+ (void)memcpy(eh->ether_dhost, edst, ETHER_ADDR_LEN);
ifnet_lladdr_copy_bytes(ifp, eh->ether_shost, ETHER_ADDR_LEN);
return 0;
}
-
-__private_extern__ int
-ether_add_if(struct ifnet *ifp)
-{
- ifp->if_framer = ether_frameout;
- ifp->if_demux = ether_demux;
-
- return 0;
-}
-
-__private_extern__ int
-ether_del_if(struct ifnet *ifp)
-{
- 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,
+ __unused ifnet_t ifp,
const struct sockaddr *proto_addr)
{
errno_t result = EAFNOSUPPORT;
break;
case AF_LINK:
- e_addr = CONST_LLADDR((const struct sockaddr_dl*)proto_addr);
+ e_addr = CONST_LLADDR((const struct sockaddr_dl*)
+ (uintptr_t)(size_t)proto_addr);
if ((e_addr[0] & 0x01) != 0x01)
result = EADDRNOTAVAIL;
else
return EOPNOTSUPP;
}
-
-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)
+__private_extern__ int ether_family_init(void)
{
- int error=0;
- struct dlil_ifmod_reg_str ifmod_reg;
-
- /* 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.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_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");
- error = EIO;
- goto done;
- }
-
+ errno_t error = 0;
+
/* Register protocol registration functions */
-
- if ((error = dlil_reg_proto_module(PF_INET, APPLE_IF_FAM_ETHERNET,
+ if ((error = proto_register_plumber(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);
+ printf("proto_register_plumber failed for PF_INET error=%d\n", error);
goto done;
}
-
-
- if ((error = dlil_reg_proto_module(PF_INET6, APPLE_IF_FAM_ETHERNET,
+#if INET6
+ if ((error = proto_register_plumber(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);
+ printf("proto_register_plumber failed for PF_INET6 error=%d\n", error);
+ goto done;
+ }
+#endif /* INET6 */
+#if NETAT
+ if ((error = proto_register_plumber(PF_APPLETALK, APPLE_IF_FAM_ETHERNET,
+ ether_attach_at, ether_detach_at)) != 0) {
+ printf("proto_register_plumber failed PF_APPLETALK error=%d\n", error);
goto done;
}
+#endif /* NETAT */
+#if VLAN
vlan_family_init();
+#endif /* VLAN */
+#if BOND
bond_family_init();
+#endif /* BOND */
+#if IF_BRIDGE
+ bridgeattach(0);
+#endif /* IF_BRIDGE */
done: