Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ Currently supported are:
* UDP
* TCP
* ICMP & ICMPv6 (not all message types are supported)
* IGMP (v1, v2 & v3)

Reconstruction of fragmented IP packets is also supported, but requires allocations.

Expand Down Expand Up @@ -114,6 +115,7 @@ It is also possible to only slice one packet layer:
* [`TcpSlice::from_slice`](https://docs.rs/etherparse/0.20.3/etherparse/struct.TcpSlice.html#method.from_slice)
* [`Icmpv4Slice::from_slice`](https://docs.rs/etherparse/0.20.3/etherparse/struct.Icmpv4Slice.html#method.from_slice)
* [`Icmpv6Slice::from_slice`](https://docs.rs/etherparse/0.20.3/etherparse/struct.Icmpv6Slice.html#method.from_slice)
* [`IgmpSlice::from_slice`](https://docs.rs/etherparse/0.20.3/etherparse/struct.IgmpSlice.html#method.from_slice)

The resulting data types allow access to both the header(s) and the payload of the layer
and will automatically limit the length of payload if the layer has a length field limiting the
Expand Down Expand Up @@ -158,6 +160,7 @@ And for deserialization into the corresponding header structs have a look at:
* [`TcpHeader::read`](https://docs.rs/etherparse/0.20.3/etherparse/struct.TcpHeader.html#method.read) & [`TcpHeader::from_slice`](https://docs.rs/etherparse/0.20.3/etherparse/struct.TcpHeader.html#method.from_slice)
* [`Icmpv4Header::read`](https://docs.rs/etherparse/0.20.3/etherparse/struct.Icmpv4Header.html#method.read) & [`Icmpv4Header::from_slice`](https://docs.rs/etherparse/0.20.3/etherparse/struct.Icmpv4Header.html#method.from_slice)
* [`Icmpv6Header::read`](https://docs.rs/etherparse/0.20.3/etherparse/struct.Icmpv6Header.html#method.read) & [`Icmpv6Header::from_slice`](https://docs.rs/etherparse/0.20.3/etherparse/struct.Icmpv6Header.html#method.from_slice)
* [`IgmpHeader::from_slice`](https://docs.rs/etherparse/0.20.3/etherparse/struct.IgmpHeader.html#method.from_slice)

## How to generate fake packet data?

Expand Down Expand Up @@ -216,6 +219,7 @@ Read the documentations of the different methods for a more details:
* [`TcpHeader::to_bytes`](https://docs.rs/etherparse/0.20.3/etherparse/struct.TcpHeader.html#method.to_bytes) & [`TcpHeader::write`](https://docs.rs/etherparse/0.20.3/etherparse/struct.TcpHeader.html#method.write)
* [`Icmpv4Header::to_bytes`](https://docs.rs/etherparse/0.20.3/etherparse/struct.Icmpv4Header.html#method.to_bytes) & [`Icmpv4Header::write`](https://docs.rs/etherparse/0.20.3/etherparse/struct.Icmpv4Header.html#method.write)
* [`Icmpv6Header::to_bytes`](https://docs.rs/etherparse/0.20.3/etherparse/struct.Icmpv6Header.html#method.to_bytes) & [`Icmpv6Header::write`](https://docs.rs/etherparse/0.20.3/etherparse/struct.Icmpv6Header.html#method.write)
* [`IgmpHeader::to_bytes`](https://docs.rs/etherparse/0.20.3/etherparse/struct.IgmpHeader.html#method.to_bytes) & [`IgmpHeader::write`](https://docs.rs/etherparse/0.20.3/etherparse/struct.IgmpHeader.html#method.write)

## References
* Darpa Internet Program Protocol Specification [RFC 791](https://tools.ietf.org/html/rfc791)
Expand Down
11 changes: 11 additions & 0 deletions changelog.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,16 @@
# Changelog:

## Unreleased

* Added zero-copy IGMP support (IGMPv1, IGMPv2 & IGMPv3):
* `IgmpSlice`, an enum with one zero-copy variant per message type (`MembershipQuery`, `MembershipQueryWithSources`, `MembershipReportV1`, `MembershipReportV2`, `MembershipReportV3`, `LeaveGroup` & `Unknown`), plus common accessors (`header`, `payload`, `slice`, `checksum`, `is_checksum_valid`).
* Per-variant slice types (`MembershipQuerySlice`, `MembershipQueryWithSourcesSlice`, `MembershipReportV1Slice`, `MembershipReportV2Slice`, `MembershipReportV3Slice`, `LeaveGroupSlice` & `IgmpUnknownSlice`) with typed field accessors. Variable-length data is exposed where it exists: `MembershipReportV3Slice::group_records` and `MembershipQueryWithSourcesSlice::source_addresses` / `source_addrs_bytes` (no longer `Option`-returning).
* `ReportGroupRecordV3Slice` & `ReportGroupRecordV3SliceIter` for iterating IGMPv3 group records (incl. typed `source_addresses`).
* Integrated IGMP into the transport layer parsing & building:
* New enum variants `TransportSlice::Igmp`, `TransportHeader::Igmp`, `PayloadSlice::Igmp` & `LaxPayloadSlice::Igmp`.
* `SlicedPacket`, `LaxSlicedPacket`, `PacketHeaders` & `LaxPacketHeaders` now decode `ip_number::IGMP` payloads.
* `PacketBuilder` gained `igmp` & `igmp_header` setters and `IgmpHeader::write`.

## 0.15.0

* Added Linux SLL Support (thanks to @RabadanDotDev)
Expand Down
1 change: 1 addition & 0 deletions etherparse/examples/read_by_slicing.rs
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@ fn main() {
match value.transport {
Some(Icmpv4(value)) => println!(" Icmpv4 {:?}", value),
Some(Icmpv6(value)) => println!(" Icmpv6 {:?}", value),
Some(Igmp(value)) => println!(" Igmp {:?}", value),
Some(Udp(value)) => println!(
" UDP {:?} -> {:?}",
value.source_port(),
Expand Down
1 change: 1 addition & 0 deletions etherparse/proptest-regressions/compositions_tests.txt

Large diffs are not rendered by default.

42 changes: 33 additions & 9 deletions etherparse/src/compositions_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ impl<'a> ComponentTest<'a> {
match &self.transport {
Some(TransportHeader::Icmpv6(header)) => header.write(&mut buffer).unwrap(),
Some(TransportHeader::Icmpv4(header)) => header.write(&mut buffer).unwrap(),
Some(TransportHeader::Igmp(header)) => header.write(&mut buffer).unwrap(),
Some(TransportHeader::Udp(header)) => header.write(&mut buffer).unwrap(),
Some(TransportHeader::Tcp(header)) => header.write(&mut buffer).unwrap(),
None => {}
Expand Down Expand Up @@ -405,6 +406,7 @@ impl<'a> ComponentTest<'a> {
Some(TransportHeader::Icmpv4(actual.header())),
Some(TransportSlice::Icmpv6(actual)) =>
Some(TransportHeader::Icmpv6(actual.header())),
Some(TransportSlice::Igmp(actual)) => Some(TransportHeader::Igmp(actual.header())),
Some(TransportSlice::Udp(actual)) => Some(TransportHeader::Udp(actual.to_header())),
Some(TransportSlice::Tcp(actual)) => Some(TransportHeader::Tcp(actual.to_header())),
None => None,
Expand All @@ -426,6 +428,9 @@ impl<'a> ComponentTest<'a> {
Some(TransportSlice::Icmpv6(icmpv6)) => {
assert_eq!(&self.payload[..], icmpv6.payload());
}
Some(TransportSlice::Igmp(igmp)) => {
assert_eq!(&self.payload[..], igmp.payload());
}
Some(TransportSlice::Udp(udp)) => {
assert_eq!(&self.payload[..], udp.payload());
}
Expand Down Expand Up @@ -471,6 +476,7 @@ impl<'a> ComponentTest<'a> {
tcp: &TcpHeader,
icmpv4: &Icmpv4Header,
icmpv6: &Icmpv6Header,
igmp: &IgmpHeader,
) {
// add vlan
{
Expand All @@ -489,11 +495,12 @@ impl<'a> ComponentTest<'a> {
if !test.link_exts.is_full() {
test.run_link_exts(
vlans, macsecs, arp, ipv4, ipv4_ext, ipv6, ipv6_ext, udp, tcp, icmpv4, icmpv6,
igmp,
);
}
test.run_arp(arp);
test.run_ipv4(ipv4, ipv4_ext, udp, tcp, icmpv4, icmpv6);
test.run_ipv6(ipv6, ipv6_ext, udp, tcp, icmpv4, icmpv6);
test.run_ipv4(ipv4, ipv4_ext, udp, tcp, icmpv4, icmpv6, igmp);
test.run_ipv6(ipv6, ipv6_ext, udp, tcp, icmpv4, icmpv6, igmp);
}

// add macsec
Expand All @@ -513,11 +520,12 @@ impl<'a> ComponentTest<'a> {
if !test.link_exts.is_full() {
test.run_link_exts(
vlans, macsecs, arp, ipv4, ipv4_ext, ipv6, ipv6_ext, udp, tcp, icmpv4, icmpv6,
igmp,
);
}
test.run_arp(arp);
test.run_ipv4(ipv4, ipv4_ext, udp, tcp, icmpv4, icmpv6);
test.run_ipv6(ipv6, ipv6_ext, udp, tcp, icmpv4, icmpv6);
test.run_ipv4(ipv4, ipv4_ext, udp, tcp, icmpv4, icmpv6, igmp);
test.run_ipv6(ipv6, ipv6_ext, udp, tcp, icmpv4, icmpv6, igmp);
}
}

Expand All @@ -536,6 +544,7 @@ impl<'a> ComponentTest<'a> {
tcp: &TcpHeader,
icmpv4: &Icmpv4Header,
icmpv6: &Icmpv6Header,
igmp: &IgmpHeader,
) {
// fragmenting
{
Expand Down Expand Up @@ -565,7 +574,7 @@ impl<'a> ComponentTest<'a> {
non_frag.protocol = ip_exts.set_next_headers(ip.protocol);
NetHeaders::Ipv4(non_frag, ip_exts)
});
test.run_transport(udp, tcp, icmpv4, icmpv6);
test.run_transport(udp, tcp, icmpv4, icmpv6, igmp);
}
}

Expand All @@ -577,6 +586,7 @@ impl<'a> ComponentTest<'a> {
tcp: &TcpHeader,
icmpv4: &Icmpv4Header,
icmpv6: &Icmpv6Header,
igmp: &IgmpHeader,
) {
// fragmenting
{
Expand Down Expand Up @@ -612,7 +622,7 @@ impl<'a> ComponentTest<'a> {
ip.next_header = non_frag.set_next_headers(ip.next_header);
NetHeaders::Ipv6(ip, non_frag)
});
test.run_transport(udp, tcp, icmpv4, icmpv6);
test.run_transport(udp, tcp, icmpv4, icmpv6, igmp);
}
}

Expand All @@ -622,10 +632,23 @@ impl<'a> ComponentTest<'a> {
tcp: &TcpHeader,
icmpv4: &Icmpv4Header,
icmpv6: &Icmpv6Header,
igmp: &IgmpHeader,
) {
// unknown transport layer
self.run();

// igmp
{
let mut test = self.clone();
test.net
.as_mut()
.unwrap()
.try_set_next_headers(ip_number::IGMP)
.unwrap();
test.transport = Some(TransportHeader::Igmp(igmp.clone()));
test.run()
}

// udp
{
let mut test = self.clone();
Expand Down Expand Up @@ -730,6 +753,7 @@ proptest! {
ref tcp in tcp_any(),
ref icmpv4 in icmpv4_header_any(),
ref icmpv6 in icmpv6_header_any(),
ref igmp in igmp_header_any(),
ref payload in proptest::collection::vec(any::<u8>(), 0..1024))
{
let setup_eth = || -> ComponentTest {
Expand All @@ -745,14 +769,14 @@ proptest! {
// ethernet 2: standalone, ipv4, ipv6
setup_eth().run();
setup_eth().run_arp(arp);
setup_eth().run_ipv4(ipv4, ipv4_exts, udp, tcp, icmpv4, icmpv6);
setup_eth().run_ipv6(ipv6, ipv6_exts, udp, tcp, icmpv4, icmpv6);
setup_eth().run_ipv4(ipv4, ipv4_exts, udp, tcp, icmpv4, icmpv6, igmp);
setup_eth().run_ipv6(ipv6, ipv6_exts, udp, tcp, icmpv4, icmpv6, igmp);

// link exts
{
let vlans = [vlan0.clone(), vlan1.clone(), vlan2.clone()];
let macsecs = [macsec0.clone(), macsec1.clone(), macsec2.clone()];
setup_eth().run_link_exts(&vlans[..], &macsecs[..], arp, ipv4, ipv4_exts, ipv6, ipv6_exts, udp, tcp, icmpv4, icmpv6);
setup_eth().run_link_exts(&vlans[..], &macsecs[..], arp, ipv4, ipv4_exts, ipv6, ipv6_exts, udp, tcp, icmpv4, icmpv6, igmp);
}
}
}
Expand Down
48 changes: 47 additions & 1 deletion etherparse/src/lax_packet_headers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,12 @@ impl<'a> LaxPacketHeaders<'a> {
/// println!(" Icmpv6 payload incomplete (length in IP header indicated more data should be present)");
/// }
/// }
/// LaxPayloadSlice::Igmp{ payload, incomplete } => {
/// println!("Igmp payload: {:?}", payload);
/// if incomplete {
/// println!(" Igmp payload incomplete (length in IP header indicated more data should be present)");
/// }
/// }
/// LaxPayloadSlice::LinuxSll(linux_sll) => {
/// println!("Linux SLL payload (protocol type {:?}): {:?}", linux_sll.protocol_type, linux_sll.payload);
/// }
Expand Down Expand Up @@ -258,6 +264,12 @@ impl<'a> LaxPacketHeaders<'a> {
/// println!(" Icmpv6 payload incomplete (length in IP header indicated more data should be present)");
/// }
/// }
/// LaxPayloadSlice::Igmp{ payload, incomplete } => {
/// println!("Igmp payload: {:?}", payload);
/// if incomplete {
/// println!(" Igmp payload incomplete (length in IP header indicated more data should be present)");
/// }
/// }
/// LaxPayloadSlice::LinuxSll(linux_sll) => {
/// println!("Linux SLL payload (protocol type {:?}): {:?}", linux_sll.protocol_type, linux_sll.payload);
/// }
Expand Down Expand Up @@ -516,6 +528,12 @@ impl<'a> LaxPacketHeaders<'a> {
/// println!(" Icmpv6 payload incomplete (length in IP header indicated more data should be present)");
/// }
/// }
/// LaxPayloadSlice::Igmp{ payload, incomplete } => {
/// println!("Igmp payload: {:?}", payload);
/// if incomplete {
/// println!(" Igmp payload incomplete (length in IP header indicated more data should be present)");
/// }
/// }
/// }
/// }
/// }
Expand Down Expand Up @@ -632,6 +650,12 @@ impl<'a> LaxPacketHeaders<'a> {
/// println!(" Icmpv6 payload incomplete (length in IP header indicated more data should be present)");
/// }
/// }
/// LaxPayloadSlice::Igmp{ payload, incomplete } => {
/// println!("Igmp payload: {:?}", payload);
/// if incomplete {
/// println!(" Igmp payload incomplete (length in IP header indicated more data should be present)");
/// }
/// }
/// LaxPayloadSlice::LinuxSll(linux_sll) => {
/// println!("Linux SLL payload (protocol type {:?}): {:?}", linux_sll.protocol_type, linux_sll.payload);
/// }
Expand Down Expand Up @@ -781,6 +805,18 @@ impl<'a> LaxPacketHeaders<'a> {
self.stop_err = Some((add_len_source(e), Layer::Icmpv6));
}
},
IGMP => match IgmpSlice::from_slice(ip_payload.payload) {
Ok(i) => {
self.transport = Some(TransportHeader::Igmp(i.header()));
self.payload = LaxPayloadSlice::Igmp {
payload: i.payload(),
incomplete: ip_payload.incomplete,
};
}
Err(e) => {
self.stop_err = Some((add_len_source(e), Layer::Igmp));
}
},
UDP => {
match UdpSlice::from_slice_lax(ip_payload.payload) {
Ok(u) => {
Expand Down Expand Up @@ -1423,7 +1459,7 @@ mod test {
for fragmented in [false, true] {
let ipv4 = {
let mut ipv4 =
Ipv4Header::new(0, 1, 2.into(), [3, 4, 5, 6], [7, 8, 9, 10]).unwrap();
Ipv4Header::new(0, 1, 3.into(), [3, 4, 5, 6], [7, 8, 9, 10]).unwrap();
ipv4.more_fragments = fragmented;
ipv4
};
Expand Down Expand Up @@ -2115,6 +2151,16 @@ mod test {
}
);
}
Some(H::Igmp(igmp)) => {
assert_eq!(&test.transport, &Some(H::Igmp(igmp.clone())));
assert_eq!(
actual.payload,
LaxPayloadSlice::Igmp {
payload: expected_payload,
incomplete: false
}
);
}
Some(H::Udp(s)) => {
assert_eq!(&test.transport, &Some(H::Udp(s.clone())));
assert_eq!(
Expand Down
20 changes: 20 additions & 0 deletions etherparse/src/lax_payload_slice.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,14 @@ pub enum LaxPayloadSlice<'a> {
incomplete: bool,
},

/// Payload part of an IGMP message. Check [`crate::IgmpType`]
/// for a description what will be part of the payload.
Igmp {
payload: &'a [u8],
/// True if the payload has been cut off.
incomplete: bool,
},

/// Linux SLL payload.
LinuxSll(LinuxSllPayloadSlice<'a>),
}
Expand Down Expand Up @@ -82,6 +90,10 @@ impl<'a> LaxPayloadSlice<'a> {
payload,
incomplete: _,
} => payload,
LaxPayloadSlice::Igmp {
payload,
incomplete: _,
} => payload,
LaxPayloadSlice::LinuxSll(linux_sll) => linux_sll.payload,
}
}
Expand Down Expand Up @@ -200,6 +212,14 @@ mod test {
.slice(),
&payload
);
assert_eq!(
Igmp {
payload: &payload,
incomplete: false
}
.slice(),
&payload
);
assert_eq!(
LinuxSll(LinuxSllPayloadSlice {
protocol_type: LinuxSllProtocolType::Ignored(0),
Expand Down
6 changes: 5 additions & 1 deletion etherparse/src/lax_sliced_packet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1038,7 +1038,7 @@ mod test {
for fragmented in [false, true] {
let ipv4 = {
let mut ipv4 =
Ipv4Header::new(0, 1, 2.into(), [3, 4, 5, 6], [7, 8, 9, 10]).unwrap();
Ipv4Header::new(0, 1, 3.into(), [3, 4, 5, 6], [7, 8, 9, 10]).unwrap();
ipv4.more_fragments = fragmented;
ipv4
};
Expand Down Expand Up @@ -1814,6 +1814,10 @@ mod test {
assert_eq!(&test.transport, &Some(H::Icmpv6(icmpv6.header())));
assert_eq!(icmpv6.payload(), expected_payload);
}
Some(S::Igmp(igmp)) => {
assert_eq!(&test.transport, &Some(H::Igmp(igmp.header())));
assert_eq!(igmp.payload(), expected_payload);
}
Some(S::Udp(s)) => {
assert_eq!(&test.transport, &Some(H::Udp(s.to_header())));
}
Expand Down
13 changes: 13 additions & 0 deletions etherparse/src/lax_sliced_packet_cursor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -342,6 +342,19 @@ impl<'a> LaxSlicedPacketCursor<'a> {
self.result.stop_err = Some((O::Len(err), Layer::Icmpv6));
}
},
ip_number::IGMP => match IgmpSlice::from_slice(slice.payload) {
Ok(igmp) => {
self.offset += igmp.slice().len();
self.result.transport = Some(TransportSlice::Igmp(igmp));
}
Err(mut err) => {
err.layer_start_offset += self.offset;
if LenSource::Slice == err.len_source {
err.len_source = slice.len_source;
}
self.result.stop_err = Some((O::Len(err), Layer::Igmp));
}
},
_ => {}
}
self.result
Expand Down
Loading
Loading