1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
//! An ethernet packet abstraction.

use crate::PrimitiveValues;

use alloc::vec::Vec;
use core::fmt;

use xenet_core::mac::MacAddr;
use xenet_macro::packet;

#[cfg(feature = "serde")]
use serde::{Deserialize, Serialize};

/// Represents the Ethernet header length.
pub const ETHERNET_HEADER_LEN: usize = 14;

/// Represents the MAC address length.
pub const MAC_ADDR_LEN: usize = 6;

/// Represents the Ethernet Header.
#[derive(Clone, Debug, PartialEq, Eq)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
pub struct EthernetHeader {
    /// Destination MAC address
    pub destination: MacAddr,
    /// Source MAC address
    pub source: MacAddr,
    /// EtherType
    pub ethertype: EtherType,
}

impl EthernetHeader {
    /// Construct an Ethernet header from a byte slice.
    pub fn from_bytes(packet: &[u8]) -> Result<EthernetHeader, String> {
        if packet.len() < ETHERNET_HEADER_LEN {
            return Err("Packet is too small for Ethernet header".to_string());
        }
        match EthernetPacket::new(packet) {
            Some(ethernet_packet) => Ok(EthernetHeader {
                destination: ethernet_packet.get_destination(),
                source: ethernet_packet.get_source(),
                ethertype: ethernet_packet.get_ethertype(),
            }),
            None => Err("Failed to parse Ethernet packet".to_string()),
        }
    }
    /// Construct an Ethernet header from a EthernetPacket.
    pub(crate) fn from_packet(ethernet_packet: &EthernetPacket) -> EthernetHeader {
        EthernetHeader {
            destination: ethernet_packet.get_destination(),
            source: ethernet_packet.get_source(),
            ethertype: ethernet_packet.get_ethertype(),
        }
    }
}

/// Represents an Ethernet packet.
#[packet]
pub struct Ethernet {
    #[construct_with(u8, u8, u8, u8, u8, u8)]
    pub destination: MacAddr,
    #[construct_with(u8, u8, u8, u8, u8, u8)]
    pub source: MacAddr,
    #[construct_with(u16)]
    pub ethertype: EtherType,
    #[payload]
    pub payload: Vec<u8>,
}

#[test]
fn ethernet_header_test() {
    let mut packet = [0u8; 14];
    {
        let mut ethernet_header = MutableEthernetPacket::new(&mut packet[..]).unwrap();

        let source = MacAddr(0x12, 0x34, 0x56, 0x78, 0x9a, 0xbc);
        ethernet_header.set_source(source);
        assert_eq!(ethernet_header.get_source(), source);

        let dest = MacAddr(0xde, 0xf0, 0x12, 0x34, 0x45, 0x67);
        ethernet_header.set_destination(dest);
        assert_eq!(ethernet_header.get_destination(), dest);

        ethernet_header.set_ethertype(EtherType::Ipv6);
        assert_eq!(ethernet_header.get_ethertype(), EtherType::Ipv6);
    }

    let ref_packet = [
        0xde, 0xf0, 0x12, 0x34, 0x45, 0x67, /* destination */
        0x12, 0x34, 0x56, 0x78, 0x9a, 0xbc, /* source */
        0x86, 0xdd, /* ethertype */
    ];
    assert_eq!(&ref_packet[..], &packet[..]);
}

/// Represents the Ethernet types.
#[derive(Clone, Copy, Debug, Hash, Eq, PartialEq)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
pub enum EtherType {
    Ipv4,
    Arp,
    WakeOnLan,
    Trill,
    DECnet,
    Rarp,
    AppleTalk,
    Aarp,
    Ipx,
    Qnx,
    Ipv6,
    FlowControl,
    CobraNet,
    Mpls,
    MplsMcast,
    PppoeDiscovery,
    PppoeSession,
    Vlan,
    PBridge,
    Lldp,
    Ptp,
    Cfm,
    QinQ,
    Rldp,
    Unknown(u16),
}

impl EtherType {
    /// Constructs a new EtherType from u16
    pub fn new(value: u16) -> EtherType {
        match value {
            0x0800 => EtherType::Ipv4,
            0x0806 => EtherType::Arp,
            0x0842 => EtherType::WakeOnLan,
            0x22F3 => EtherType::Trill,
            0x6003 => EtherType::DECnet,
            0x8035 => EtherType::Rarp,
            0x809B => EtherType::AppleTalk,
            0x80F3 => EtherType::Aarp,
            0x8137 => EtherType::Ipx,
            0x8204 => EtherType::Qnx,
            0x86DD => EtherType::Ipv6,
            0x8808 => EtherType::FlowControl,
            0x8819 => EtherType::CobraNet,
            0x8847 => EtherType::Mpls,
            0x8848 => EtherType::MplsMcast,
            0x8863 => EtherType::PppoeDiscovery,
            0x8864 => EtherType::PppoeSession,
            0x8100 => EtherType::Vlan,
            0x88a8 => EtherType::PBridge,
            0x88cc => EtherType::Lldp,
            0x88f7 => EtherType::Ptp,
            0x8902 => EtherType::Cfm,
            0x9100 => EtherType::QinQ,
            0x8899 => EtherType::Rldp,
            _ => EtherType::Unknown(value),
        }
    }
    /// Return the name of the EtherType
    pub fn name(&self) -> &str {
        match *self {
            EtherType::Ipv4 => "IPv4",
            EtherType::Arp => "ARP",
            EtherType::WakeOnLan => "WakeOnLan",
            EtherType::Trill => "Trill",
            EtherType::DECnet => "DECnet",
            EtherType::Rarp => "RARP",
            EtherType::AppleTalk => "AppleTalk",
            EtherType::Aarp => "AARP",
            EtherType::Ipx => "IPX",
            EtherType::Qnx => "QNX",
            EtherType::Ipv6 => "IPv6",
            EtherType::FlowControl => "FlowControl",
            EtherType::CobraNet => "CobraNet",
            EtherType::Mpls => "MPLS",
            EtherType::MplsMcast => "MPLS Multicast",
            EtherType::PppoeDiscovery => "PPPoE Discovery",
            EtherType::PppoeSession => "PPPoE Session",
            EtherType::Vlan => "VLAN",
            EtherType::PBridge => "Provider Bridging",
            EtherType::Lldp => "LLDP",
            EtherType::Ptp => "PTP",
            EtherType::Cfm => "CFM",
            EtherType::QinQ => "QinQ",
            EtherType::Rldp => "RLDP",
            EtherType::Unknown(_) => "Unknown",
        }
    }
}

impl PrimitiveValues for EtherType {
    type T = (u16,);
    fn to_primitive_values(&self) -> (u16,) {
        match *self {
            EtherType::Ipv4 => (0x0800,),
            EtherType::Arp => (0x0806,),
            EtherType::WakeOnLan => (0x0842,),
            EtherType::Trill => (0x22F3,),
            EtherType::DECnet => (0x6003,),
            EtherType::Rarp => (0x8035,),
            EtherType::AppleTalk => (0x809B,),
            EtherType::Aarp => (0x80F3,),
            EtherType::Ipx => (0x8137,),
            EtherType::Qnx => (0x8204,),
            EtherType::Ipv6 => (0x86DD,),
            EtherType::FlowControl => (0x8808,),
            EtherType::CobraNet => (0x8819,),
            EtherType::Mpls => (0x8847,),
            EtherType::MplsMcast => (0x8848,),
            EtherType::PppoeDiscovery => (0x8863,),
            EtherType::PppoeSession => (0x8864,),
            EtherType::Vlan => (0x8100,),
            EtherType::PBridge => (0x88a8,),
            EtherType::Lldp => (0x88cc,),
            EtherType::Ptp => (0x88f7,),
            EtherType::Cfm => (0x8902,),
            EtherType::QinQ => (0x9100,),
            EtherType::Rldp => (0x8899,),
            EtherType::Unknown(n) => (n,),
        }
    }
}

impl fmt::Display for EtherType {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        write!(
            f,
            "{}",
            match self {
                EtherType::Ipv4 => "Ipv4",
                EtherType::Arp => "Arp",
                EtherType::WakeOnLan => "WakeOnLan",
                EtherType::Trill => "Trill",
                EtherType::DECnet => "DECnet",
                EtherType::Rarp => "Rarp",
                EtherType::AppleTalk => "AppleTalk",
                EtherType::Aarp => "Aarp",
                EtherType::Ipx => "Ipx",
                EtherType::Qnx => "Qnx",
                EtherType::Ipv6 => "Ipv6",
                EtherType::FlowControl => "FlowControl",
                EtherType::CobraNet => "CobraNet",
                EtherType::Mpls => "Mpls",
                EtherType::MplsMcast => "MplsMcast",
                EtherType::PppoeDiscovery => "PppoeDiscovery",
                EtherType::PppoeSession => "PppoeSession",
                EtherType::Vlan => "Vlan",
                EtherType::PBridge => "PBridge",
                EtherType::Lldp => "Lldp",
                EtherType::Ptp => "Ptp",
                EtherType::Cfm => "Cfm",
                EtherType::QinQ => "QinQ",
                EtherType::Rldp => "Rldp",
                EtherType::Unknown(_) => "unknown",
            }
        )
    }
}

#[test]
fn ether_type_to_str() {
    use std::format;
    let ipv4 = EtherType::new(0x0800);
    assert_eq!(format!("{}", ipv4), "Ipv4");
    let arp = EtherType::new(0x0806);
    assert_eq!(format!("{}", arp), "Arp");
    let unknown = EtherType::new(0x0666);
    assert_eq!(format!("{}", unknown), "unknown");
}