Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

collector: skb: handle VLAN in the payload #481

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
27 changes: 22 additions & 5 deletions retis/src/module/skb/bpf.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use std::str;
use anyhow::{anyhow, Result};
use pnet_packet::{
arp::ArpPacket, ethernet::*, icmp::IcmpPacket, icmpv6::Icmpv6Packet, ip::*, ipv4::*, ipv6::*,
tcp::TcpPacket, udp::UdpPacket, Packet,
tcp::TcpPacket, udp::UdpPacket, vlan::VlanPacket, Packet,
};

use crate::{
Expand Down Expand Up @@ -235,20 +235,37 @@ pub(super) fn unmarshal_packet(
event.eth = Some(unmarshal_eth(&eth)?);
}

match eth.get_ethertype() {
// Handle VLANs.
fn get_payload(etype: EtherType, payload: &[u8]) -> Result<(EtherType, &[u8])> {
match etype {
EtherTypes::Vlan | EtherTypes::PBridge => match VlanPacket::new(payload) {
Some(vlan) => {
if payload.len() < 4 {
bail!("Cannot parse packet: VLAN header is too small");
}
get_payload(vlan.get_ethertype(), &payload[4..])
}
None => bail!("Cannot parse VLAN header"),
},
_ => Ok((etype, payload)),
}
}
let (etype, payload) = get_payload(eth.get_ethertype(), eth.payload())?;

match etype {
EtherTypes::Arp => {
if let Some(eth) = ArpPacket::new(eth.payload()) {
if let Some(eth) = ArpPacket::new(payload) {
event.arp = unmarshal_arp(&eth)?;
};
}
EtherTypes::Ipv4 => {
if let Some(ip) = Ipv4Packet::new(eth.payload()) {
if let Some(ip) = Ipv4Packet::new(payload) {
event.ip = Some(unmarshal_ipv4(&ip)?);
unmarshal_l4(event, ip.get_next_level_protocol(), ip.payload())?;
};
}
EtherTypes::Ipv6 => {
if let Some(ip) = Ipv6Packet::new(eth.payload()) {
if let Some(ip) = Ipv6Packet::new(payload) {
event.ip = Some(unmarshal_ipv6(&ip)?);
unmarshal_l4(event, ip.get_next_header(), ip.payload())?;
};
Expand Down