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
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
use crate::*;
use core::slice::from_raw_parts;
/// Slice containing an IPv6 fragment header.
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct Ipv6FragmentHeaderSlice<'a> {
/// Slice containing the packet data.
slice: &'a [u8],
}
impl<'a> Ipv6FragmentHeaderSlice<'a> {
/// Creates a hop by hop header slice from a slice.
pub fn from_slice(slice: &'a [u8]) -> Result<Ipv6FragmentHeaderSlice<'a>, err::LenError> {
// the fragmentation header has the exact size of 8 bytes
if slice.len() < 8 {
Err(err::LenError {
required_len: 8,
len: slice.len(),
len_source: LenSource::Slice,
layer: err::Layer::Ipv6FragHeader,
layer_start_offset: 0,
})
} else {
Ok(Ipv6FragmentHeaderSlice {
// SAFETY:
// Safe as slice length is checked to be at least 8 before this
// code can be reached.
slice: unsafe { from_raw_parts(slice.as_ptr(), 8) },
})
}
}
/// Creates a hop by hop header slice from a slice (assumes slice size & content was validated before).
///
/// # Safety
///
/// This function assumes that the passed slice has at least the length
/// of 8. If a slice with length less then 8 is passed to this function
/// the behavior will be undefined.
pub unsafe fn from_slice_unchecked(slice: &'a [u8]) -> Ipv6FragmentHeaderSlice<'a> {
debug_assert!(slice.len() >= Ipv6FragmentHeader::LEN);
// the fragmentation header has the exact size of 8 bytes
Ipv6FragmentHeaderSlice {
slice: from_raw_parts(slice.as_ptr(), Ipv6FragmentHeader::LEN),
}
}
/// Returns the slice containing the ipv6 fragment header.
#[inline]
pub fn slice(&self) -> &'a [u8] {
self.slice
}
/// Returns the IP protocol number of the next header.
///
/// See [IpNumber] or [ip_number] for a definition of the known values.
#[inline]
pub fn next_header(&self) -> IpNumber {
// SAFETY:
// Slice size checked to be at least 8 bytes in constructor.
IpNumber(unsafe { *self.slice.get_unchecked(0) })
}
/// Fragment offset
#[inline]
pub fn fragment_offset(&self) -> IpFragOffset {
unsafe {
// SAFETY: Safe as the resulting number is guaranteed to be only
// 13 bit long.
IpFragOffset::new_unchecked(u16::from_be_bytes([
// SAFETY:
// Slice size checked to be at least 8 bytes in constructor.
(*self.slice.get_unchecked(2) >> 3) & 0b0001_1111u8,
((*self.slice.get_unchecked(2) << 5) & 0b1110_0000u8)
| (*self.slice.get_unchecked(3) & 0b0001_1111u8),
]))
}
}
/// True if more fragment packets will follow. False if this is the last packet.
#[inline]
pub fn more_fragments(&self) -> bool {
// SAFETY:
// Slice size checked to be at least 8 bytes in constructor.
unsafe { 0 != *self.slice.get_unchecked(3) & 0b1000_0000u8 }
}
/// Identifcation value generated by the source
pub fn identification(&self) -> u32 {
// SAFETY:
// Slice size checked to be at least 8 bytes in constructor.
unsafe { get_unchecked_be_u32(self.slice.as_ptr().add(4)) }
}
/// Checks if the fragment header actually fragments the packet.
///
/// Returns false if the fragment offset is 0 and the more flag
/// is not set. Otherwise returns true.
///
/// [RFC8200](https://datatracker.ietf.org/doc/html/rfc8200) explicitly
/// states that fragment headers that don't fragment the packet payload are
/// allowed. See the following quote from
/// RFC8200 page 32:
///
/// > Revised the text to handle the case of fragments that are whole
/// > datagrams (i.e., both the Fragment Offset field and the M flag
/// > are zero). If received, they should be processed as a
/// > reassembled packet. Any other fragments that match should be
/// > processed independently. The Fragment creation process was
/// > modified to not create whole datagram fragments (Fragment
/// > Offset field and the M flag are zero). See
/// > [RFC6946](https://datatracker.ietf.org/doc/html/6946) and
/// > [RFC8021](https://datatracker.ietf.org/doc/html/rfc8021) for more
/// > information."
///
/// ```
/// use etherparse::Ipv6FragmentHeaderSlice;
///
/// {
/// let slice = Ipv6FragmentHeaderSlice::from_slice(&[
/// 0, 0, 0, 0, // offset 0 & more_fragments not set
/// 1, 2, 3, 4,
/// ]).unwrap();
/// assert!(false == slice.is_fragmenting_payload());
/// }
///
/// {
/// let slice = Ipv6FragmentHeaderSlice::from_slice(&[
/// 0, 0, 0, 0b1000_0000u8, // more_fragments set
/// 1, 2, 3, 4,
/// ]).unwrap();
/// assert!(slice.is_fragmenting_payload());
/// }
///
/// {
/// let slice = Ipv6FragmentHeaderSlice::from_slice(&[
/// 0, 0, 1, 0, // non zero offset
/// 1, 2, 3, 4,
/// ]).unwrap();
/// assert!(slice.is_fragmenting_payload());
/// }
/// ```
#[inline]
pub fn is_fragmenting_payload(&self) -> bool {
// SAFETY:
// Slice size checked to be at least 8 bytes in constructor.
unsafe {
0 != *self.slice.get_unchecked(2) || 0 != (*self.slice.get_unchecked(3) & 0b1001_1111u8)
// exclude the reserved bytes
}
}
/// Decode some of the fields and copy the results to a
/// Ipv6FragmentHeader struct.
pub fn to_header(&self) -> Ipv6FragmentHeader {
Ipv6FragmentHeader {
next_header: self.next_header(),
fragment_offset: self.fragment_offset(),
more_fragments: self.more_fragments(),
identification: self.identification(),
}
}
}
#[cfg(test)]
mod test {
use crate::{test_gens::*, *};
use alloc::{format, vec::Vec};
use proptest::prelude::*;
proptest! {
#[test]
fn debug(input in ipv6_fragment_any()) {
let bytes = input.to_bytes();
let slice = Ipv6FragmentHeaderSlice::from_slice(
&bytes
).unwrap();
assert_eq!(
&format!(
"Ipv6FragmentHeaderSlice {{ slice: {:?} }}",
slice.slice()
),
&format!("{:?}", slice)
);
}
}
proptest! {
#[test]
fn clone_eq(input in ipv6_fragment_any()) {
let bytes = input.to_bytes();
let slice = Ipv6FragmentHeaderSlice::from_slice(
&bytes
).unwrap();
assert_eq!(slice, slice.clone());
}
}
proptest! {
#[test]
fn from_slice(
input in ipv6_fragment_any(),
dummy_data in proptest::collection::vec(any::<u8>(), 0..20)
) {
// serialize
let mut buffer: Vec<u8> = Vec::with_capacity(8 + dummy_data.len());
input.write(&mut buffer).unwrap();
buffer.extend(&dummy_data[..]);
// calls with a valid result
{
let slice = Ipv6FragmentHeaderSlice::from_slice(&buffer[..]).unwrap();
assert_eq!(slice.slice(), &buffer[..8]);
}
// call with not enough data in the slice
for len in 0..Ipv6FragmentHeader::LEN {
assert_eq!(
Ipv6FragmentHeaderSlice::from_slice(&buffer[0..len]).unwrap_err(),
err::LenError{
required_len: 8,
len: len,
len_source: LenSource::Slice,
layer: err::Layer::Ipv6FragHeader,
layer_start_offset: 0,
}
);
}
}
}
proptest! {
#[test]
fn from_slice_unchecked(
input in ipv6_fragment_any(),
dummy_data in proptest::collection::vec(any::<u8>(), 0..20)
) {
// serialize
let mut buffer: Vec<u8> = Vec::with_capacity(8 + dummy_data.len());
input.write(&mut buffer).unwrap();
buffer.extend(&dummy_data[..]);
// calls with a valid result
unsafe {
let slice = Ipv6FragmentHeaderSlice::from_slice_unchecked(&buffer[..]);
assert_eq!(slice.slice(), &buffer[..8]);
}
}
}
proptest! {
#[test]
fn getters(input in ipv6_fragment_any()) {
let buffer = input.to_bytes();
let slice = Ipv6FragmentHeaderSlice::from_slice(&buffer[..]).unwrap();
assert_eq!(input.next_header, slice.next_header());
assert_eq!(input.fragment_offset, slice.fragment_offset());
assert_eq!(input.more_fragments, slice.more_fragments());
assert_eq!(input.identification, slice.identification());
}
}
proptest! {
#[test]
fn is_fragmenting_payload(
non_zero_offset in 1u16..0b0001_1111_1111_1111u16,
identification in any::<u32>(),
next_header in ip_number_any(),
) {
// negative case
{
let header = Ipv6FragmentHeader {
next_header,
fragment_offset: 0.try_into().unwrap(),
more_fragments: false,
identification
};
// slice
let buffer = header.to_bytes();
let slice = Ipv6FragmentHeaderSlice::from_slice(&buffer).unwrap();
assert!(false == slice.is_fragmenting_payload());
}
// positive case (non zero offset)
{
let header = Ipv6FragmentHeader {
next_header,
fragment_offset: non_zero_offset.try_into().unwrap(),
more_fragments: false,
identification
};
// slice
let buffer = header.to_bytes();
let slice = Ipv6FragmentHeaderSlice::from_slice(&buffer).unwrap();
assert!(slice.is_fragmenting_payload());
}
// positive case (more fragments)
{
let header = Ipv6FragmentHeader {
next_header,
fragment_offset: 0.try_into().unwrap(),
more_fragments: true,
identification
};
// slice
let buffer = header.to_bytes();
let slice = Ipv6FragmentHeaderSlice::from_slice(&buffer).unwrap();
assert!(slice.is_fragmenting_payload());
}
// positive case (non zero offset & more fragments)
{
let header = Ipv6FragmentHeader {
next_header,
fragment_offset: non_zero_offset.try_into().unwrap(),
more_fragments: true,
identification
};
// slice
let buffer = header.to_bytes();
let slice = Ipv6FragmentHeaderSlice::from_slice(&buffer).unwrap();
assert!(slice.is_fragmenting_payload());
}
}
}
proptest! {
#[test]
fn to_header(input in ipv6_fragment_any()) {
let buffer = input.to_bytes();
let slice = Ipv6FragmentHeaderSlice::from_slice(&buffer).unwrap();
assert_eq!(input, slice.to_header());
}
}
}