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
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
use crate::*;
use core::{cmp::min, slice::from_raw_parts};
///A slice containing an Linux Cooked Capture (SLL) header of a network package.
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct LinuxSllHeaderSlice<'a> {
slice: &'a [u8],
}
impl<'a> LinuxSllHeaderSlice<'a> {
/// Creates a SLL header slice from an other slice.
pub fn from_slice(
slice: &'a [u8],
) -> Result<LinuxSllHeaderSlice<'a>, err::linux_sll::HeaderSliceError> {
//check length
if slice.len() < LinuxSllHeader::LEN {
return Err(err::linux_sll::HeaderSliceError::Len(err::LenError {
required_len: LinuxSllHeader::LEN,
len: slice.len(),
len_source: LenSource::Slice,
layer: err::Layer::LinuxSllHeader,
layer_start_offset: 0,
}));
}
// check valid packet type
// SAFETY:
// Safe as it is checked at the start of the function that the
// length of the slice is at least LinuxSllHeader::LEN (16).
let packet_type_val = unsafe { get_unchecked_be_u16(slice.as_ptr()) };
if let Err(err) = LinuxSllPacketType::try_from(packet_type_val) {
return Err(err::linux_sll::HeaderSliceError::Content(err));
}
// check supported ArpHardwareId
// SAFETY:
// Safe as it is checked at the start of the function that the
// length of the slice is at least LinuxSllHeader::LEN (16).
let arp_hardware_id = unsafe { get_unchecked_be_u16(slice.as_ptr().add(2)) };
let arp_hardware_id = ArpHardwareId::from(arp_hardware_id);
// SAFETY:
// Safe as it is checked at the start of the function that the
// length of the slice is at least LinuxSllHeader::LEN (16).
let protocol_type = unsafe { get_unchecked_be_u16(slice.as_ptr().add(14)) };
if let Err(err) = LinuxSllProtocolType::try_from((arp_hardware_id, protocol_type)) {
return Err(err::linux_sll::HeaderSliceError::Content(err));
}
//all done
Ok(LinuxSllHeaderSlice {
// SAFETY:
// Safe as slice length is checked to be at least
// LinuxSllHeader::LEN (16) before this.
slice: unsafe { from_raw_parts(slice.as_ptr(), LinuxSllHeader::LEN) },
})
}
/// Converts the given slice into a SLL header slice WITHOUT any checks to
/// ensure that the data present is an sll header or that the slice length
/// is matching the header length.
///
/// If you are not sure what this means, use [`LinuxSllHeaderSlice::from_slice`]
/// instead.
///
/// # Safety
///
/// The caller must ensured that the given slice has the length of
/// [`LinuxSllHeader::LEN`] and the fields are valid
#[inline]
#[cfg(feature = "std")]
pub(crate) unsafe fn from_slice_unchecked(slice: &[u8]) -> LinuxSllHeaderSlice {
debug_assert!(slice.len() == LinuxSllHeader::LEN);
LinuxSllHeaderSlice { slice }
}
/// Returns the slice containing the SLL header
#[inline]
pub fn slice(&self) -> &'a [u8] {
self.slice
}
/// Read the packet type field.
#[inline]
pub fn packet_type(&self) -> LinuxSllPacketType {
// SAFETY:
// Safe as the constructor checks that the slice has
// at least the length of LinuxSllHeader::LEN (16).
let packet_type_raw = unsafe { get_unchecked_be_u16(self.slice.as_ptr()) };
// SAFETY:
// Safe as the constructor checks that the packet type is valid
unsafe { LinuxSllPacketType::try_from(packet_type_raw).unwrap_unchecked() }
}
/// Read the arp hardware type field
#[inline]
pub fn arp_hardware_type(&self) -> ArpHardwareId {
// SAFETY:
// Safe as the constructor checks that the slice has
// at least the length of LinuxSllHeader::LEN (16).
let arp_hardware_type_raw = unsafe { get_unchecked_be_u16(self.slice.as_ptr().add(2)) };
ArpHardwareId::from(arp_hardware_type_raw)
}
/// Read the link layer address length field.
#[inline]
pub fn sender_address_valid_length(&self) -> u16 {
// SAFETY:
// Safe as the constructor checks that the slice has
// at least the length of LinuxSllHeader::LEN (16).
unsafe { get_unchecked_be_u16(self.slice.as_ptr().add(4)) }
}
/// Read the link layer address field. Only the first
/// `LinuxSllHeaderSlice::link_layer_address_length` bytes are meaningful
#[inline]
pub fn sender_address_full(&self) -> [u8; 8] {
// SAFETY:
// Safe as the constructor checks that the slice has
// at least the length of LinuxSllHeader::LEN (16).
unsafe { get_unchecked_8_byte_array(self.slice.as_ptr().add(6)) }
}
/// Get the meaningful bytes of the slice of the link layer address
#[inline]
pub fn sender_address(&self) -> &'a [u8] {
let length = self.sender_address_valid_length() as usize;
&self.slice[6..min(6 + length, 6 + 8)]
}
/// Read the protocol type field
#[inline]
pub fn protocol_type(&self) -> LinuxSllProtocolType {
let arp_hardware_type = self.arp_hardware_type();
// SAFETY:
// Safe as the constructor checks that the slice has
// at least the length of LinuxSllHeader::LEN (16).
let protocol_type_raw = unsafe { get_unchecked_be_u16(self.slice.as_ptr().add(14)) };
// SAFETY:
// Safe as the constructor checks that the arphw + protocol are supported
unsafe {
LinuxSllProtocolType::try_from((arp_hardware_type, protocol_type_raw))
.unwrap_unchecked()
}
}
/// Decode all the fields and copy the results to a [`LinuxSllHeader`] struct
pub fn to_header(&self) -> LinuxSllHeader {
LinuxSllHeader {
packet_type: self.packet_type(),
arp_hrd_type: self.arp_hardware_type(),
sender_address_valid_length: self.sender_address_valid_length(),
sender_address: self.sender_address_full(),
protocol_type: self.protocol_type(),
}
}
}
#[cfg(test)]
mod test {
use super::*;
use crate::test_gens::*;
use alloc::{format, vec::Vec};
use proptest::prelude::*;
proptest! {
#[test]
fn from_slice(
input in linux_sll_any(),
dummy_data in proptest::collection::vec(any::<u8>(), 0..20),
bad_packet_type in LinuxSllPacketType::MAX_VAL + 1..=u16::MAX,
bad_hw_type in any::<u16>().prop_filter(
"hw id must be unknown",
|v| ![
ArpHardwareId::NETLINK,
ArpHardwareId::IPGRE,
ArpHardwareId::IEEE80211_RADIOTAP,
ArpHardwareId::FRAD,
ArpHardwareId::ETHERNET,
].iter().any(|&x| *v == x.0)
)
) {
// serialize
let buffer = {
let mut buffer: Vec<u8> = Vec::with_capacity(LinuxSllHeader::LEN + dummy_data.len());
input.write(&mut buffer).unwrap();
buffer.extend(&dummy_data[..]);
buffer
};
// calls with a valid result
{
let result = LinuxSllHeaderSlice::from_slice(&buffer[..]).unwrap();
assert_eq!(&buffer[..LinuxSllHeader::LEN], result.slice());
}
// call with not enough data in the slice
for len in 0..=13 {
assert_eq!(
LinuxSllHeaderSlice::from_slice(&buffer[..len]),
Err(err::linux_sll::HeaderSliceError::Len(err::LenError{
required_len: LinuxSllHeader::LEN,
len: len,
len_source: LenSource::Slice,
layer: err::Layer::LinuxSllHeader,
layer_start_offset: 0,
}))
);
}
// packet_type_val error
{
let mut modbuf = buffer.clone();
let p_be = bad_packet_type.to_be_bytes();
modbuf[0] = p_be[0];
modbuf[1] = p_be[1];
assert_eq!(
LinuxSllHeaderSlice::from_slice(&modbuf),
Err(err::linux_sll::HeaderSliceError::Content(
err::linux_sll::HeaderError::UnsupportedPacketTypeField { packet_type: bad_packet_type }
))
);
}
// hardware_id error
{
let mut modbuf = buffer.clone();
let p_be = bad_hw_type.to_be_bytes();
modbuf[2] = p_be[0];
modbuf[3] = p_be[1];
assert_eq!(
LinuxSllHeaderSlice::from_slice(&modbuf),
Err(err::linux_sll::HeaderSliceError::Content(
err::linux_sll::HeaderError::UnsupportedArpHardwareId { arp_hardware_type: ArpHardwareId(bad_hw_type) }
))
);
}
}
}
proptest! {
#[test]
fn getters(input in linux_sll_any()) {
let buffer = input.to_bytes();
let slice = LinuxSllHeaderSlice::from_slice(&buffer).unwrap();
assert_eq!(input.packet_type, slice.packet_type());
assert_eq!(input.arp_hrd_type, slice.arp_hardware_type());
assert_eq!(input.sender_address_valid_length, slice.sender_address_valid_length());
assert_eq!(input.sender_address, slice.sender_address_full());
assert_eq!(&input.sender_address[..usize::from(input.sender_address_valid_length)], slice.sender_address());
assert_eq!(input.protocol_type, slice.protocol_type());
}
}
proptest! {
#[test]
fn to_header(input in linux_sll_any()) {
let buffer = input.to_bytes();
let slice = LinuxSllHeaderSlice::from_slice(&buffer).unwrap();
assert_eq!(input, slice.to_header());
}
}
proptest! {
#[test]
fn clone_eq(input in linux_sll_any()) {
let buffer = input.to_bytes();
let slice = LinuxSllHeaderSlice::from_slice(&buffer).unwrap();
assert_eq!(slice, slice.clone());
}
}
proptest! {
#[test]
fn dbg(input in linux_sll_any()) {
let buffer = input.to_bytes();
let slice = LinuxSllHeaderSlice::from_slice(&buffer).unwrap();
assert_eq!(
&format!(
"LinuxSllHeaderSlice {{ slice: {:?} }}",
slice.slice()
),
&format!("{:?}", slice)
);
}
}
}