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
use crate::datalink::MacAddr;
use pnet::packet::Packet;

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

/// Represents the Ethernet types.
#[derive(Clone, Copy, Debug, Hash, Eq, PartialEq)]
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,
    Unknown(u16),
}

impl EtherType {
    /// Constructs a new EtherType from u16
    pub fn from_u16(n: u16) -> Option<EtherType> {
        match n {
            0x0800 => Some(EtherType::Ipv4),
            0x0806 => Some(EtherType::Arp),
            0x0842 => Some(EtherType::WakeOnLan),
            0x22F3 => Some(EtherType::Trill),
            0x6003 => Some(EtherType::DECnet),
            0x8035 => Some(EtherType::Rarp),
            0x809B => Some(EtherType::AppleTalk),
            0x80F3 => Some(EtherType::Aarp),
            0x8137 => Some(EtherType::Ipx),
            0x8204 => Some(EtherType::Qnx),
            0x86DD => Some(EtherType::Ipv6),
            0x8808 => Some(EtherType::FlowControl),
            0x8819 => Some(EtherType::CobraNet),
            0x8847 => Some(EtherType::Mpls),
            0x8848 => Some(EtherType::MplsMcast),
            0x8863 => Some(EtherType::PppoeDiscovery),
            0x8864 => Some(EtherType::PppoeSession),
            0x8100 => Some(EtherType::Vlan),
            0x88a8 => Some(EtherType::PBridge),
            0x88cc => Some(EtherType::Lldp),
            0x88f7 => Some(EtherType::Ptp),
            0x8902 => Some(EtherType::Cfm),
            0x9100 => Some(EtherType::QinQ),
            _ => Some(EtherType::Unknown(n)),
        }
    }
    /// Return the number of the EtherType
    pub fn number(&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::Unknown(n) => n,
        }
    }
    /// 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::Unknown(_) => "Unknown",
        }
    }
}

/// Represents an Ethernet packet.
#[derive(Clone, Debug)]
pub struct EthernetPacket {
    /// Destination MAC address
    pub destination: MacAddr,
    /// Source MAC address
    pub source: MacAddr,
    /// EtherType
    pub ethertype: EtherType,
    /// Payload. Next level protocol packet.
    pub payload: Vec<u8>,
}

impl EthernetPacket {
    /// Constructs a new EthernetPacket from pnet::packet::ethernet::EthernetPacket.
    pub(crate) fn from_pnet_packet(
        packet: &pnet::packet::ethernet::EthernetPacket,
    ) -> EthernetPacket {
        EthernetPacket {
            destination: MacAddr::new(packet.get_destination().octets()),
            source: MacAddr::new(packet.get_source().octets()),
            ethertype: EtherType::from_u16(packet.get_ethertype().0).unwrap(),
            payload: packet.payload().to_vec(),
        }
    }
    /// Constructs a new EthernetPacket from bytes
    pub fn from_bytes(packet: &[u8]) -> EthernetPacket {
        let ethernet_packet = pnet::packet::ethernet::EthernetPacket::new(packet).unwrap();
        EthernetPacket::from_pnet_packet(&ethernet_packet)
    }
}

/// Build Ethernet packet
pub(crate) fn build_ethernet_packet(
    eth_packet: &mut pnet::packet::ethernet::MutableEthernetPacket,
    src_mac: MacAddr,
    dst_mac: MacAddr,
    ether_type: EtherType,
) {
    eth_packet.set_source(pnet::datalink::MacAddr::from(src_mac.octets()));
    eth_packet.set_destination(pnet::datalink::MacAddr::from(dst_mac.octets()));
    match ether_type {
        EtherType::Arp => {
            eth_packet.set_ethertype(pnet::packet::ethernet::EtherTypes::Arp);
        }
        EtherType::Ipv4 => {
            eth_packet.set_ethertype(pnet::packet::ethernet::EtherTypes::Ipv4);
        }
        EtherType::Ipv6 => {
            eth_packet.set_ethertype(pnet::packet::ethernet::EtherTypes::Ipv6);
        }
        _ => {
            // TODO
        }
    }
}

/// Build Ethernet ARP packet
pub(crate) fn build_ethernet_arp_packet(
    eth_packet: &mut pnet::packet::ethernet::MutableEthernetPacket,
    src_mac: MacAddr,
) {
    eth_packet.set_source(pnet::datalink::MacAddr::from(src_mac.octets()));
    eth_packet.set_destination(pnet::datalink::MacAddr::broadcast());
    eth_packet.set_ethertype(pnet::packet::ethernet::EtherTypes::Arp);
}

/// Ethernet Packet Builder
#[derive(Clone, Debug)]
pub struct EthernetPacketBuilder {
    /// Source MAC address
    pub src_mac: MacAddr,
    /// Destination MAC address
    pub dst_mac: MacAddr,
    /// EtherType
    pub ether_type: EtherType,
}

impl EthernetPacketBuilder {
    /// Constructs a new EthernetPacketBuilder
    pub fn new() -> EthernetPacketBuilder {
        EthernetPacketBuilder {
            src_mac: MacAddr::new([0; 6]),
            dst_mac: MacAddr::new([0; 6]),
            ether_type: EtherType::Ipv4,
        }
    }
    /// Build Ethernet packet and return bytes
    pub fn build(&self) -> Vec<u8> {
        let mut buffer: Vec<u8> = vec![0; ETHERNET_HEADER_LEN];
        let mut eth_packet =
            pnet::packet::ethernet::MutableEthernetPacket::new(&mut buffer).unwrap();
        build_ethernet_packet(
            &mut eth_packet,
            self.src_mac.clone(),
            self.dst_mac.clone(),
            self.ether_type,
        );
        eth_packet.to_immutable().packet().to_vec()
    }
}