2011-12-22 15:19:25 +00:00
|
|
|
/*
|
|
|
|
* \brief Ethernet protocol
|
|
|
|
* \author Stefan Kalkowski
|
|
|
|
* \date 2010-08-19
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
2017-02-20 12:23:52 +00:00
|
|
|
* Copyright (C) 2010-2017 Genode Labs GmbH
|
2011-12-22 15:19:25 +00:00
|
|
|
*
|
|
|
|
* This file is part of the Genode OS framework, which is distributed
|
2017-02-20 12:23:52 +00:00
|
|
|
* under the terms of the GNU Affero General Public License version 3.
|
2011-12-22 15:19:25 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef _NET__ETHERNET_H_
|
|
|
|
#define _NET__ETHERNET_H_
|
|
|
|
|
|
|
|
/* Genode includes */
|
|
|
|
#include <base/exception.h>
|
|
|
|
#include <util/string.h>
|
|
|
|
|
|
|
|
#include <util/endian.h>
|
2016-08-15 10:50:33 +00:00
|
|
|
#include <net/mac_address.h>
|
2011-12-22 15:19:25 +00:00
|
|
|
|
2016-08-25 15:40:45 +00:00
|
|
|
namespace Net
|
|
|
|
{
|
|
|
|
class Ethernet_frame;
|
|
|
|
|
|
|
|
template <Genode::size_t DATA_SIZE>
|
|
|
|
class Ethernet_frame_sized;
|
|
|
|
}
|
2015-03-04 20:12:14 +00:00
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Data layout of this class conforms to the Ethernet II frame
|
|
|
|
* (IEEE 802.3).
|
|
|
|
*
|
|
|
|
* Ethernet-frame-header-format:
|
|
|
|
*
|
|
|
|
* ----------------------------------------------------------
|
|
|
|
* | destination mac address | source mac address | ethertype |
|
|
|
|
* | 6 bytes | 6 bytes | 2 bytes |
|
|
|
|
* ----------------------------------------------------------
|
|
|
|
*/
|
|
|
|
class Net::Ethernet_frame
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
|
|
|
|
enum Size {
|
|
|
|
ADDR_LEN = 6, /* MAC address length in bytes */
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
static const Mac_address BROADCAST; /* broadcast address */
|
|
|
|
|
|
|
|
private:
|
|
|
|
|
|
|
|
Genode::uint8_t _dst_mac[ADDR_LEN]; /* destination mac address */
|
|
|
|
Genode::uint8_t _src_mac[ADDR_LEN]; /* source mac address */
|
|
|
|
Genode::uint16_t _type; /* encapsulated protocol */
|
|
|
|
unsigned _data[0]; /* encapsulated data */
|
|
|
|
|
|
|
|
public:
|
|
|
|
|
|
|
|
class No_ethernet_frame : Genode::Exception {};
|
|
|
|
|
2016-08-25 15:40:45 +00:00
|
|
|
enum { MIN_SIZE = 64 };
|
2015-03-04 20:12:14 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Id representing encapsulated protocol.
|
|
|
|
*/
|
|
|
|
enum Ether_type {
|
|
|
|
IPV4 = 0x0800,
|
|
|
|
ARP = 0x0806,
|
|
|
|
WAKE_ON_LAN = 0x0842,
|
|
|
|
SYN3 = 0x1337,
|
|
|
|
RARP = 0x8035,
|
|
|
|
APPLETALK = 0x809B,
|
|
|
|
AARP = 0x80F3,
|
|
|
|
VLAN_TAGGED = 0x8100,
|
|
|
|
IPX = 0x8137,
|
|
|
|
NOVELL = 0x8138,
|
|
|
|
IPV6 = 0x86DD,
|
|
|
|
MAC_CONTROL = 0x8808,
|
|
|
|
SLOW = 0x8809,
|
|
|
|
COBRANET = 0x8819,
|
|
|
|
MPLS_UNICAST = 0x8847,
|
|
|
|
MPLS_MULTICAST = 0x8848,
|
|
|
|
PPPOE_DISCOVERY = 0x8863,
|
|
|
|
PPPOE_STAGE = 0x8864,
|
|
|
|
NLB = 0x886F,
|
|
|
|
JUMBO_FRAMES = 0x8870,
|
|
|
|
EAP = 0x888E,
|
|
|
|
PROFINET = 0x8892,
|
|
|
|
HYPERSCSI = 0x889A,
|
|
|
|
ATAOE = 0x88A2,
|
|
|
|
ETHERCAT = 0x88A4,
|
|
|
|
PROVIDER_BRIDGING = 0x88A8,
|
|
|
|
POWERLINK = 0x88AB,
|
|
|
|
LLDP = 0x88CC,
|
|
|
|
SERCOS_III = 0x88CD,
|
|
|
|
CESOE = 0x88D8,
|
|
|
|
HOMEPLUG = 0x88E1,
|
|
|
|
MAC_SEC = 0x88E5,
|
|
|
|
PRECISION_TIME = 0x88F7,
|
|
|
|
CFM = 0x8902,
|
|
|
|
FCOE = 0x8906,
|
|
|
|
FCOE_Init = 0x8914,
|
|
|
|
Q_IN_Q = 0x9100,
|
|
|
|
LLT = 0xCAFE
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
/*****************
|
|
|
|
** Constructor **
|
|
|
|
*****************/
|
|
|
|
|
|
|
|
Ethernet_frame(Genode::size_t size) {
|
|
|
|
/* at least, frame header needs to fit in */
|
|
|
|
if (size < sizeof(Ethernet_frame))
|
|
|
|
throw No_ethernet_frame();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/***********************************
|
|
|
|
** Ethernet field read-accessors **
|
|
|
|
***********************************/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* \return destination MAC address of frame.
|
|
|
|
*/
|
2016-11-02 00:00:59 +00:00
|
|
|
Mac_address dst() const { return Mac_address((void *)&_dst_mac); }
|
2015-03-04 20:12:14 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* \return source MAC address of frame.
|
|
|
|
*/
|
2016-11-02 00:00:59 +00:00
|
|
|
Mac_address src() const { return Mac_address((void *)&_src_mac); }
|
2015-03-04 20:12:14 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* \return EtherType - type of encapsulated protocol.
|
|
|
|
*/
|
2016-11-02 00:00:59 +00:00
|
|
|
Genode::uint16_t type() const { return host_to_big_endian(_type); }
|
2015-03-04 20:12:14 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* \return payload data.
|
|
|
|
*/
|
2016-05-09 13:47:28 +00:00
|
|
|
template <typename T> T * data() { return (T *)(_data); }
|
|
|
|
template <typename T> T const * data() const { return (T const *)(_data); }
|
2015-03-04 20:12:14 +00:00
|
|
|
|
|
|
|
|
|
|
|
/***********************************
|
|
|
|
** Ethernet field write-accessors **
|
|
|
|
***********************************/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Set the destination MAC address of this frame.
|
|
|
|
*
|
|
|
|
* \param mac MAC address to be set.
|
|
|
|
*/
|
|
|
|
void dst(Mac_address mac) { mac.copy(&_dst_mac); }
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Set the source MAC address of this frame.
|
|
|
|
*
|
|
|
|
* \param mac MAC address to be set.
|
|
|
|
*/
|
|
|
|
void src(Mac_address mac) { mac.copy(&_src_mac); }
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Set type of encapsulated protocol.
|
|
|
|
*
|
|
|
|
* \param type the EtherType to be set.
|
|
|
|
*/
|
2015-04-29 09:36:54 +00:00
|
|
|
void type(Genode::uint16_t type) { _type = host_to_big_endian(type); }
|
2015-03-04 20:12:14 +00:00
|
|
|
|
|
|
|
|
|
|
|
/***************
|
|
|
|
** Operators **
|
|
|
|
***************/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Placement new operator.
|
|
|
|
*/
|
2016-09-15 12:40:37 +00:00
|
|
|
void * operator new(__SIZE_TYPE__ size, void* addr) { return addr; }
|
2015-03-04 20:12:14 +00:00
|
|
|
|
2016-11-02 00:00:59 +00:00
|
|
|
|
|
|
|
/*********
|
|
|
|
** log **
|
|
|
|
*********/
|
|
|
|
|
|
|
|
void print(Genode::Output &output) const;
|
|
|
|
|
2015-03-04 20:12:14 +00:00
|
|
|
} __attribute__((packed));
|
2011-12-22 15:19:25 +00:00
|
|
|
|
2016-08-25 15:40:45 +00:00
|
|
|
|
|
|
|
template <Genode::size_t DATA_SIZE>
|
|
|
|
class Net::Ethernet_frame_sized : public Ethernet_frame
|
|
|
|
{
|
|
|
|
private:
|
|
|
|
|
|
|
|
enum {
|
|
|
|
HS = sizeof(Ethernet_frame),
|
|
|
|
DS = DATA_SIZE + HS >= MIN_SIZE ? DATA_SIZE : MIN_SIZE - HS,
|
|
|
|
};
|
|
|
|
|
|
|
|
Genode::uint8_t _data[DS];
|
|
|
|
Genode::uint32_t _checksum;
|
|
|
|
|
|
|
|
public:
|
|
|
|
|
|
|
|
Ethernet_frame_sized(Mac_address dst_in, Mac_address src_in,
|
|
|
|
Ether_type type_in)
|
|
|
|
:
|
|
|
|
Ethernet_frame(sizeof(Ethernet_frame))
|
|
|
|
{
|
|
|
|
dst(dst_in);
|
|
|
|
src(src_in);
|
|
|
|
type(type_in);
|
|
|
|
}
|
|
|
|
|
|
|
|
} __attribute__((packed));
|
|
|
|
|
2011-12-22 15:19:25 +00:00
|
|
|
#endif /* _NET__ETHERNET_H_ */
|