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
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
/*! Low-level packet access and construction.
# An overview over packet representations
The `wire` module deals with the packet *representation*. It provides three levels of
functionality.
* First, it provides functions to extract fields from sequences of octets, and to insert fields
into sequences of octets. This happens in the lowercase structures e.g. [`ethernet_frame`] or
[`udp_packet`] [^tcp].
* Second, it provides a compact, high-level representation of header data that can be created from
parsing and emitted into a sequence of octets. This happens through the `Repr` family of structs
and enums, e.g. [`ArpRepr`] or [`Ipv4Repr`].
* Third, it provides an type wrapper around sequences of octets valid as a particular packet
format which potentially owns its data. This can memoize parts of the layout, avoiding
re-calculating it on every access. While this restricts mutability of header data, fixed-length
checksum fields and the payload can still be accessed normally. This happens in the uppercase
Frame or Packet family of structs, e.g. [`ArpPacket`] or [`UdpPacket`].
[`ethernet_frame`]: struct.ethernet_frame.html
[`udp_packet`]: struct.udp_packet.html
[`ArpRepr`]: enum.ArpRepr.html
[`Ipv4Repr`]: struct.Ipv4Repr.html
[`ArpPacket`]: struct.ArpPacket.html
[`UdpPacket`]: struct.UdpPacket.html
[^tcp]: The TCP structures differ since I haven't gotten around to reworking them. It does not have
a dynamically sized byte wrapper so its `Packet` implements this functionality as well but come
with all downsides. In particular, its accessors may panic.
An important part is also the underlying trait for byte containers, [`Payload`] and [`PayloadMut`].
None of the standard reference traits accurately captures the relationship of a framing outer
packet with its payload. It should be the case that the payload content changes only when accessed
directly and changing its length should be possible. These two traits model such a relationship,
providing a few methods to efficiently request layout changes of the payload from the container.
This makes it possible to recursively parse packets while being able to resize the innermost packet
or to insert additional data into an intermediate layer without mutating the payload.
[`Payload`]: trait.Payload.html
[`PayloadMut`]: trait.Payload.html
The `packet` family of data structures guarantees that, if the `packet::check_len()` method
returned `Ok(())`, then no field accessor or setter method will panic; however, the guarantee only
hold while specific fields are mutated, which are listed in the documentation for the specific
packet.
The owning `Packet` family makes a stronger guarantee. It only exposes fields for which mutation
will not *cause* panics (some panics are still possible, read on), as long as `Packet::new_checked`
constructor was used or the `new_unchecked` constructor with previously parsed data. Where such a
mutation causes the layout to change (i.e. payload length) the underlying container is first asked
to perform the necessary reframing and then dependent fields and offsets are updated. Note that
this ties panicking to the container: A misbehaving implementation that implements the `PayloadMut`
trait incorrectly or a fallible allocation could still cause panics.
The `packet::new_unchecked` method is a shorthand for combining `new_unchecked` and `check_len`
while the `Packet::new_checked` method is a shorthand for a combination of `Packet::new_unchecked`
and `Repr::parse`. When parsing untrusted input, it is *necessary* to use either of the checked
methods; so long as the buffer is not modified, no accessor will fail. When emitting output,
though, it is *incorrect* to use `Packet::new_checked()`; the length check is likely to succeed on
a zeroed buffer, but fail on a buffer filled with data from a previous packet, such as when reusing
buffers, resulting in nondeterministic panics with some network devices but not others. The buffer
length for emission is often calculated by the `Repr` struct but not in general provided by the
`Packet` layer.
In the `Repr` family of data structures, the `Repr::parse()` method never panics and the
`Repr::emit()` method never panics as long as the underlying buffer is exactly `Repr::buffer_len()`
octets long if provided.
# Examples
To emit an IP packet header into an octet buffer, and then parse it back:
```rust
#
# {
use ethox::wire::{ip::v4, Checksum, ip::Protocol};
let repr = v4::Repr {
src_addr: v4::Address::new(10, 0, 0, 1),
dst_addr: v4::Address::new(10, 0, 0, 2),
protocol: Protocol::Tcp,
payload_len: 10,
hop_limit: 64
};
let mut buffer = vec![0; repr.buffer_len() + repr.payload_len];
{ // emission
let packet = v4::packet::new_unchecked_mut(&mut buffer);
repr.emit(packet, Checksum::Manual);
}
{ // parsing
let packet = v4::packet::new_checked(&buffer)
.expect("truncated packet");
let parsed = v4::Repr::parse(packet, Checksum::Manual)
.expect("malformed packet");
assert_eq!(repr, parsed);
}
# }
```
*/
// Copyright (C) 2016 whitequark@whitequark.org
// Copyright (C) 2019 Andreas Molzer <andreas.molzer@tum.de>
//
// in large parts from `smoltcp` originally distributed under 0-clause BSD
//
// Applies to files in this folder unless otherwise noted. These are:
// * `arp.rs`
// * `dhcpv4.rs`
// * `error.rs`
// * `ethernet.rs`
// * `icmp.rs`
// * `icmpv4.rs`
// * `icmpv6.rs`
// * `igmp.rs`
// * `ip.rs`
// * `ipv4.rs`
// * `ipv6fragment.rs`
// * `ipv6hopbyhop.rs`
// * `ipv6option.rs`
// * `ipv6routing.rs`
// * `ipv6.rs`
// * `mld.rs`
// * `mod.rs` (this file)
// * `ndiscoption.rs`
// * `ndisc.rs`
// * `pretty_print.rs`
// * `tcp.rs`
// * `udp.rs`
// FIXME: Most fields should be self-explanatory and there is the general guide but enable once the
// other issues have been resolved.
// mod ethernet;
// pub(crate) mod dhcpv4;
/// Describes how to handle checksums.
pub use ;
pub use ;
/// The result type of a reframing operation on [`PayloadMut`].
///
/// [`PayloadMut`]: trait.PayloadMut.html
pub type PayloadResult<T> = Result;
pub use PrettyPrinter;
// Public re-exports with the wanted structure.
pub use ;
/*
#[cfg(feature = "proto-igmp")]
pub use self::igmp::{
Packet as IgmpPacket,
Repr as IgmpRepr,
IgmpVersion};
pub use self::icmpv6::{
Message as Icmpv6Message,
DstUnreachable as Icmpv6DstUnreachable,
TimeExceeded as Icmpv6TimeExceeded,
ParamProblem as Icmpv6ParamProblem,
Packet as Icmpv6Packet,
Repr as Icmpv6Repr};
pub use self::icmp::Repr as IcmpRepr;
*/
/*
pub use self::ndisc::{
Repr as NdiscRepr,
RouterFlags as NdiscRouterFlags,
NeighborFlags as NdiscNeighborFlags};
pub use self::ndiscoption::{
NdiscOption,
Repr as NdiscOptionRepr,
Type as NdiscOptionType,
PrefixInformation as NdiscPrefixInformation,
RedirectedHeader as NdiscRedirectedHeader,
PrefixInfoFlags as NdiscPrefixInfoFlags};
*/
/*
pub use self::mld::{
AddressRecord as MldAddressRecord,
Repr as MldRepr};
*/
pub use ;