Skip to content

Commit 3b798cb

Browse files
David Ibbitsonchifflier
David Ibbitson
authored andcommitted
add sll2 support
1 parent 43892b3 commit 3b798cb

File tree

1 file changed

+54
-1
lines changed

1 file changed

+54
-1
lines changed

src/data/mod.rs

+54-1
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,8 @@ pub use crate::data::exported_pdu::*;
2929
pub use crate::data::pcap_nflog::*;
3030
use crate::linktype::Linktype;
3131
use crate::read_u32_e;
32-
use nom::number::streaming::{be_u16, be_u64};
32+
use nom::number::complete::be_u32;
33+
use nom::number::streaming::{be_u8, be_u16, be_u64};
3334
use nom::IResult;
3435

3536
pub const ETHERTYPE_IPV4: u16 = 0x0800;
@@ -145,6 +146,57 @@ fn parse_sll_header(i: &[u8]) -> IResult<&[u8], SLLHeader> {
145146
Ok((i, header))
146147
}
147148

149+
/// Get packet data for LINKTYPE_LINUX_SLL2 (276)
150+
///
151+
/// See <https://www.tcpdump.org/linktypes/LINKTYPE_LINUX_SLL2.html>
152+
pub fn get_packetdata_linux_sll2(i: &[u8], caplen: usize) -> Option<PacketData> {
153+
if i.len() < caplen || caplen == 0 {
154+
None
155+
} else {
156+
match parse_sll2_header(i) {
157+
Err(_) => None,
158+
Ok((rem, sll)) => {
159+
match sll.arphrd_type {
160+
778 /* ARPHRD_IPGRE */ => Some(PacketData::L4(47, rem)),
161+
803 /* ARPHRD_IEEE80211_RADIOTAP */ |
162+
824 /* ARPHRD_NETLINK */ => None,
163+
_ => Some(PacketData::L3(sll.protocol_type, rem)),
164+
}
165+
}
166+
}
167+
}
168+
}
169+
170+
struct SLL2Header {
171+
protocol_type: u16,
172+
_reserved: u16,
173+
_interface_index: u32,
174+
arphrd_type: u16,
175+
_packet_type: u8,
176+
_ll_addr_len: u8,
177+
_ll_addr: u64,
178+
}
179+
180+
fn parse_sll2_header(i: &[u8]) -> IResult<&[u8], SLL2Header> {
181+
let (i, protocol_type) = be_u16(i)?;
182+
let (i, _reserved) = be_u16(i)?;
183+
let (i, _interface_index) = be_u32(i)?;
184+
let (i, arphrd_type) = be_u16(i)?;
185+
let (i, _packet_type) = be_u8(i)?;
186+
let (i, _ll_addr_len) = be_u8(i)?;
187+
let (i, _ll_addr) = be_u64(i)?;
188+
let header = SLL2Header {
189+
protocol_type,
190+
_reserved,
191+
_interface_index,
192+
arphrd_type,
193+
_packet_type,
194+
_ll_addr_len,
195+
_ll_addr,
196+
};
197+
Ok((i, header))
198+
}
199+
148200
/// Get packet data for LINKTYPE_IPV4 (228)
149201
///
150202
/// Raw IPv4; the packet begins with an IPv4 header.
@@ -169,6 +221,7 @@ pub fn get_packetdata(i: &[u8], linktype: Linktype, caplen: usize) -> Option<Pac
169221
Linktype::ETHERNET => get_packetdata_ethernet(i, caplen),
170222
Linktype::RAW => get_packetdata_raw(i, caplen),
171223
Linktype::LINUX_SLL => get_packetdata_linux_sll(i, caplen),
224+
Linktype::LINUX_SLL2 => get_packetdata_linux_sll2(i, caplen),
172225
Linktype::IPV4 => get_packetdata_ipv4(i, caplen),
173226
Linktype::IPV6 => get_packetdata_ipv6(i, caplen),
174227
Linktype::NFLOG => get_packetdata_nflog(i, caplen),

0 commit comments

Comments
 (0)