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
use super::*;
#[derive(Clone, Debug, Eq, PartialEq)]
pub enum LinkSlice<'a> {
Ethernet2(PacketSlice<'a, Ethernet2Header>)
}
#[derive(Clone, Debug, Eq, PartialEq)]
pub enum VlanSlice<'a> {
SingleVlan(PacketSlice<'a, SingleVlanHeader>),
DoubleVlan(PacketSlice<'a, DoubleVlanHeader>),
}
impl<'a> VlanSlice<'a> {
pub fn to_header(&self) -> VlanHeader {
use VlanHeader::*;
use VlanSlice::*;
match self {
SingleVlan(value) => Single(value.to_header()),
DoubleVlan(value) => Double(value.to_header())
}
}
}
#[derive(Clone, Debug, Eq, PartialEq)]
pub enum InternetSlice<'a> {
Ipv4(PacketSlice<'a, Ipv4Header>),
Ipv6(PacketSlice<'a, Ipv6Header>, [Option<(u8, PacketSlice<'a, Ipv6ExtensionHeader>)>; IPV6_MAX_NUM_HEADER_EXTENSIONS]),
}
#[derive(Clone, Debug, Eq, PartialEq)]
pub enum TransportSlice<'a> {
Udp(PacketSlice<'a, UdpHeader>)
}
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct SlicedPacket<'a> {
pub link: Option<LinkSlice<'a>>,
pub vlan: Option<VlanSlice<'a>>,
pub ip: Option<InternetSlice<'a>>,
pub transport: Option<TransportSlice<'a>>,
pub payload: &'a [u8]
}
const ETH_IPV4: u16 = EtherType::Ipv4 as u16;
const ETH_IPV6: u16 = EtherType::Ipv6 as u16;
const ETH_VLAN: u16 = EtherType::VlanTaggedFrame as u16;
const ETH_BRIDGE: u16 = EtherType::ProviderBridging as u16;
const ETH_VLAN_DOUBLE: u16 = EtherType::VlanDoubleTaggedFrame as u16;
const IP_UDP: u8 = IpTrafficClass::Udp as u8;
const IPV6_HOP_BY_HOP: u8 = IpTrafficClass::IPv6HeaderHopByHop as u8;
const IPV6_ROUTE: u8 = IpTrafficClass::IPv6RouteHeader as u8;
const IPV6_FRAG: u8 = IpTrafficClass::IPv6FragmentationHeader as u8;
const IPV6_OPTIONS: u8 = IpTrafficClass::IPv6DestinationOptions as u8;
const IPV6_AUTH: u8 = IpTrafficClass::IPv6AuthenticationHeader as u8;
const IPV6_ENCAP_SEC: u8 = IpTrafficClass::IPv6EncapSecurityPayload as u8;
impl<'a> SlicedPacket<'a> {
pub fn from_ethernet(data: &'a [u8]) -> Result<SlicedPacket, ReadError> {
let (rest, ether_type, link) = {
use LinkSlice::*;
let value = PacketSlice::<Ethernet2Header>::from_slice(data)?;
(&data[value.slice.len()..],
value.ether_type(),
Some(Ethernet2(value)))
};
let (rest, ether_type, vlan) = match ether_type {
ETH_VLAN | ETH_BRIDGE | ETH_VLAN_DOUBLE => {
use VlanSlice::*;
let single = PacketSlice::<SingleVlanHeader>::from_slice(rest)?;
let ether_type = single.ether_type();
match ether_type {
ETH_VLAN | ETH_BRIDGE | ETH_VLAN_DOUBLE => {
let double = PacketSlice::<DoubleVlanHeader>::from_slice(rest)?;
(&rest[double.slice.len()..],
double.inner().ether_type(),
Some(DoubleVlan(double)))
},
_ => (&rest[single.slice.len()..],
ether_type,
Some(SingleVlan(single)))
}
},
_ => (rest, ether_type, None)
};
let (rest, protocol, ip) = match ether_type {
ETH_IPV4 => {
use InternetSlice::*;
let value = PacketSlice::<Ipv4Header>::from_slice(rest)?;
(&rest[value.slice.len()..],
value.protocol(),
Some(Ipv4(value)))
},
ETH_IPV6 => {
use InternetSlice::*;
let value = PacketSlice::<Ipv6Header>::from_slice(rest)?;
let mut rest = &rest[value.slice.len()..];
let mut ip_extensions = [None, None, None, None, None, None, None];
let mut next_header = value.next_header();
for i in 0..IPV6_MAX_NUM_HEADER_EXTENSIONS {
match next_header {
IPV6_HOP_BY_HOP |
IPV6_ROUTE |
IPV6_FRAG |
IPV6_OPTIONS |
IPV6_AUTH |
IPV6_ENCAP_SEC => {
let value = PacketSlice::<Ipv6ExtensionHeader>::from_slice(next_header, rest)?;
let this_header = next_header;
next_header = value.next_header();
rest = &rest[value.slice.len()..];
ip_extensions[i] = Some((this_header, value));
},
_ => break
}
}
match next_header {
IPV6_HOP_BY_HOP |
IPV6_ROUTE |
IPV6_FRAG |
IPV6_OPTIONS |
IPV6_AUTH |
IPV6_ENCAP_SEC => {
return Err(ReadError::Ipv6TooManyHeaderExtensions)
},
_ => {}
}
(rest,
next_header,
Some(Ipv6(value, ip_extensions)))
},
_ => (rest, 0, None)
};
let (rest, transport) = if ip.is_some() {
match protocol {
IP_UDP => {
use TransportSlice::*;
let value = PacketSlice::<UdpHeader>::from_slice(rest)?;
(&rest[value.slice.len()..],
Some(Udp(value)))
},
_ => (rest, None)
}
} else {
(rest, None)
};
Ok(SlicedPacket{
link: link,
vlan: vlan,
ip: ip,
transport: transport,
payload: rest
})
}
}