forked from smoltcp-rs/smoltcp
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathpacket.rs
More file actions
256 lines (236 loc) · 9.44 KB
/
Copy pathpacket.rs
File metadata and controls
256 lines (236 loc) · 9.44 KB
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
use crate::phy::DeviceCapabilities;
use crate::wire::*;
#[allow(clippy::large_enum_variant)]
#[derive(Debug, PartialEq)]
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
#[cfg(feature = "medium-ethernet")]
pub enum EthernetPacket<'a> {
#[cfg(feature = "proto-ipv4")]
Arp(ArpRepr),
Ip(Packet<'a>),
}
#[derive(Debug, PartialEq)]
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
pub enum Packet<'p> {
#[cfg(feature = "proto-ipv4")]
Ipv4(PacketV4<'p>),
#[cfg(feature = "proto-ipv6")]
Ipv6(PacketV6<'p>),
}
impl<'p> Packet<'p> {
pub fn new(ip_repr: IpRepr, payload: IpPayload<'p>) -> Self {
match ip_repr {
#[cfg(feature = "proto-ipv4")]
IpRepr::Ipv4(header) => Self::new_ipv4(header, payload),
#[cfg(feature = "proto-ipv6")]
IpRepr::Ipv6(header) => Self::new_ipv6(header, payload),
}
}
#[cfg(feature = "proto-ipv4")]
pub fn new_ipv4(ip_repr: Ipv4Repr, payload: IpPayload<'p>) -> Self {
Self::Ipv4(PacketV4 {
header: ip_repr,
payload,
})
}
#[cfg(feature = "proto-ipv6")]
pub fn new_ipv6(ip_repr: Ipv6Repr, payload: IpPayload<'p>) -> Self {
Self::Ipv6(PacketV6 {
header: ip_repr,
#[cfg(feature = "proto-ipv6-hbh")]
hop_by_hop: None,
#[cfg(feature = "proto-ipv6-fragmentation")]
fragment: None,
#[cfg(feature = "proto-ipv6-routing")]
routing: None,
payload,
})
}
pub fn ip_repr(&self) -> IpRepr {
match self {
#[cfg(feature = "proto-ipv4")]
Packet::Ipv4(p) => IpRepr::Ipv4(p.header),
#[cfg(feature = "proto-ipv6")]
Packet::Ipv6(p) => IpRepr::Ipv6(p.header),
}
}
pub fn payload(&self) -> &IpPayload<'p> {
match self {
#[cfg(feature = "proto-ipv4")]
Packet::Ipv4(p) => &p.payload,
#[cfg(feature = "proto-ipv6")]
Packet::Ipv6(p) => &p.payload,
}
}
pub fn emit_payload(&self, _ip_repr: &IpRepr, payload: &mut [u8], caps: &DeviceCapabilities) {
match self.payload() {
#[cfg(feature = "proto-ipv4")]
IpPayload::Icmpv4(icmpv4_repr) => {
icmpv4_repr.emit(&mut Icmpv4Packet::new_unchecked(payload), &caps.checksum)
}
#[cfg(all(feature = "proto-ipv4", feature = "multicast"))]
IpPayload::Igmp(igmp_repr) => igmp_repr.emit(&mut IgmpPacket::new_unchecked(payload)),
#[cfg(feature = "proto-ipv6")]
IpPayload::Icmpv6(icmpv6_repr) => {
let ipv6_repr = match _ip_repr {
#[cfg(feature = "proto-ipv4")]
IpRepr::Ipv4(_) => unreachable!(),
IpRepr::Ipv6(repr) => repr,
};
icmpv6_repr.emit(
&ipv6_repr.src_addr,
&ipv6_repr.dst_addr,
&mut Icmpv6Packet::new_unchecked(payload),
&caps.checksum,
)
}
#[cfg(feature = "proto-ipv6")]
IpPayload::HopByHopIcmpv6(hbh_repr, icmpv6_repr) => {
let ipv6_repr = match _ip_repr {
#[cfg(feature = "proto-ipv4")]
IpRepr::Ipv4(_) => unreachable!(),
IpRepr::Ipv6(repr) => repr,
};
let ipv6_ext_hdr = Ipv6ExtHeaderRepr {
next_header: IpProtocol::Icmpv6,
length: 0,
data: &[],
};
ipv6_ext_hdr.emit(&mut Ipv6ExtHeader::new_unchecked(
&mut payload[..ipv6_ext_hdr.header_len()],
));
let hbh_start = ipv6_ext_hdr.header_len();
let hbh_end = hbh_start + hbh_repr.buffer_len();
hbh_repr.emit(&mut Ipv6HopByHopHeader::new_unchecked(
&mut payload[hbh_start..hbh_end],
));
icmpv6_repr.emit(
&ipv6_repr.src_addr,
&ipv6_repr.dst_addr,
&mut Icmpv6Packet::new_unchecked(&mut payload[hbh_end..]),
&caps.checksum,
);
}
#[cfg(feature = "socket-raw")]
IpPayload::Raw(raw_packet) => payload.copy_from_slice(raw_packet),
#[cfg(any(feature = "socket-udp", feature = "socket-dns"))]
IpPayload::Udp(udp_repr, inner_payload) => udp_repr.emit(
&mut UdpPacket::new_unchecked(payload),
&_ip_repr.src_addr(),
&_ip_repr.dst_addr(),
inner_payload.len(),
|buf| buf.copy_from_slice(inner_payload),
&caps.checksum,
),
#[cfg(feature = "socket-tcp")]
IpPayload::Tcp(mut tcp_repr) => {
// This is a terrible hack to make TCP performance more acceptable on systems
// where the TCP buffers are significantly larger than network buffers,
// e.g. a 64 kB TCP receive buffer (and so, when empty, a 64k window)
// together with four 1500 B Ethernet receive buffers. If left untreated,
// this would result in our peer pushing our window and sever packet loss.
//
// I'm really not happy about this "solution" but I don't know what else to do.
if let Some(max_burst_size) = caps.max_burst_size {
let mut max_segment_size = caps.max_transmission_unit;
max_segment_size -= _ip_repr.header_len();
max_segment_size -= tcp_repr.header_len();
let max_window_size = max_burst_size * max_segment_size;
if tcp_repr.window_len as usize > max_window_size {
tcp_repr.window_len = max_window_size as u16;
}
}
tcp_repr.emit(
&mut TcpPacket::new_unchecked(payload),
&_ip_repr.src_addr(),
&_ip_repr.dst_addr(),
&caps.checksum,
);
}
#[cfg(feature = "socket-dhcpv4")]
IpPayload::Dhcpv4(udp_repr, dhcp_repr) => udp_repr.emit(
&mut UdpPacket::new_unchecked(payload),
&_ip_repr.src_addr(),
&_ip_repr.dst_addr(),
dhcp_repr.buffer_len(),
|buf| dhcp_repr.emit(&mut DhcpPacket::new_unchecked(buf)).unwrap(),
&caps.checksum,
),
}
}
}
#[derive(Debug, PartialEq)]
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
#[cfg(feature = "proto-ipv4")]
pub struct PacketV4<'p> {
header: Ipv4Repr,
payload: IpPayload<'p>,
}
#[derive(Debug, PartialEq)]
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
#[cfg(feature = "proto-ipv6")]
pub struct PacketV6<'p> {
pub header: Ipv6Repr,
#[cfg(feature = "proto-ipv6-hbh")]
pub hop_by_hop: Option<Ipv6HopByHopRepr<'p>>,
#[cfg(feature = "proto-ipv6-fragmentation")]
pub fragment: Option<Ipv6FragmentRepr>,
#[cfg(feature = "proto-ipv6-routing")]
pub routing: Option<Ipv6RoutingRepr<'p>>,
pub payload: IpPayload<'p>,
}
#[derive(Debug, PartialEq)]
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
pub enum IpPayload<'p> {
#[cfg(feature = "proto-ipv4")]
Icmpv4(Icmpv4Repr<'p>),
#[cfg(all(feature = "proto-ipv4", feature = "multicast"))]
Igmp(IgmpRepr),
#[cfg(feature = "proto-ipv6")]
Icmpv6(Icmpv6Repr<'p>),
#[cfg(feature = "proto-ipv6")]
HopByHopIcmpv6(Ipv6HopByHopRepr<'p>, Icmpv6Repr<'p>),
#[cfg(feature = "socket-raw")]
Raw(&'p [u8]),
#[cfg(any(feature = "socket-udp", feature = "socket-dns"))]
Udp(UdpRepr, &'p [u8]),
#[cfg(feature = "socket-tcp")]
Tcp(TcpRepr<'p>),
#[cfg(feature = "socket-dhcpv4")]
Dhcpv4(UdpRepr, DhcpRepr<'p>),
}
impl<'p> IpPayload<'p> {
#[cfg(feature = "proto-sixlowpan")]
pub fn as_sixlowpan_next_header(&self) -> SixlowpanNextHeader {
match self {
#[cfg(feature = "proto-ipv4")]
Self::Icmpv4(_) => unreachable!(),
#[cfg(feature = "socket-dhcpv4")]
Self::Dhcpv4(..) => unreachable!(),
#[cfg(feature = "proto-ipv6")]
Self::Icmpv6(_) => SixlowpanNextHeader::Uncompressed(IpProtocol::Icmpv6),
#[cfg(feature = "proto-ipv6")]
Self::HopByHopIcmpv6(_, _) => unreachable!(),
#[cfg(all(feature = "proto-ipv4", feature = "multicast"))]
Self::Igmp(_) => unreachable!(),
#[cfg(feature = "socket-tcp")]
Self::Tcp(_) => SixlowpanNextHeader::Uncompressed(IpProtocol::Tcp),
#[cfg(feature = "socket-udp")]
Self::Udp(..) => SixlowpanNextHeader::Compressed,
#[cfg(feature = "socket-raw")]
Self::Raw(_) => todo!(),
}
}
}
#[cfg(any(feature = "proto-ipv4", feature = "proto-ipv6"))]
pub fn icmp_reply_payload_len(len: usize, mtu: usize, header_len: usize) -> usize {
// Send back as much of the original payload as will fit within
// the minimum MTU required by IPv4. See RFC 1812 § 4.3.2.3 for
// more details.
//
// Since the entire network layer packet must fit within the minimum
// MTU supported, the payload must not exceed the following:
//
// <min mtu> - IP Header Size * 2 - ICMPv4 DstUnreachable hdr size
len.min(mtu - header_len * 2 - 8)
}