refactor(proto): share Ethernet safety classification
Safety filtering now applies at several tunnel boundaries. The relay remains the trust boundary, while the client and gateway also drop unsafe frames before spending relay bandwidth. Duplicating EtherType and IPv4/IPv6 parsers across crates would make those rules drift as the MVP grows. Move the Ethernet safety classifiers into lanparty-proto, expose typed safety drop reasons, and map them back into the existing DropReason vocabulary. The relay now uses the shared client and gateway classifiers, the gateway keeps its local LAN-send drops through the shared classifier, and the client drops the same remote-to-LAN safety cases before QUIC DATAGRAM encoding. Document the client-side local drops and list the additional suspicious drop reasons in the manual MVP test guide. Test Plan: - cargo test -p lanparty-proto safety - cargo test -p lanparty-client-core connects_to_relay_control_stream_as_client - cargo test -p lanparty-gateway connects_to_relay_control_stream_as_gateway - cargo test -p lanparty-relay - cargo fmt --check - cargo test --workspace - cargo clippy --workspace --all-targets -- -D warnings - cargo check -p lanparty-client-tap --target x86_64-pc-windows-gnu --tests - cargo check -p lanparty-client-route --target x86_64-pc-windows-gnu --tests - cargo check -p lanparty-client-tap --target x86_64-pc-windows-msvc --tests - cargo check -p lanparty-client-route --target x86_64-pc-windows-msvc --tests - git diff --check Refs: PLAN.md safety filters and client source-MAC isolation
This commit is contained in:
@@ -13,7 +13,10 @@ use lanparty_ctrl::{
|
||||
ControlError, EndpointHello, PeerInfo, Reject, RejectReason, Role, RoomCode, ServerWelcome,
|
||||
};
|
||||
use lanparty_obs::{DropReason, FrameAction};
|
||||
use lanparty_proto::{ETHERNET_HEADER_LEN, EthernetFrame, MacAddr, recommended_tap_mtu};
|
||||
use lanparty_proto::{
|
||||
EthernetFrame, MacAddr, gateway_lan_safety_drop_reason, recommended_tap_mtu,
|
||||
remote_client_safety_drop_reason,
|
||||
};
|
||||
use thiserror::Error;
|
||||
|
||||
pub use config::{ConfigError, DEFAULT_RELAY_PORT, ListenEndpoint, RelayArgs, RelayConfig};
|
||||
@@ -27,26 +30,35 @@ const CLIENT_UNKNOWN_UNICAST_BURST_FRAMES: u32 = 64;
|
||||
const CLIENT_UNKNOWN_UNICAST_REFILL_FRAMES_PER_SECOND: u32 = 32;
|
||||
const CLIENT_TOTAL_BANDWIDTH_BURST_BYTES: u64 = 4 * MEBIBYTE;
|
||||
const CLIENT_TOTAL_BANDWIDTH_REFILL_BYTES_PER_SECOND: u64 = 2 * MEBIBYTE;
|
||||
#[cfg(test)]
|
||||
use lanparty_proto::ETHERNET_HEADER_LEN;
|
||||
#[cfg(test)]
|
||||
const ETHERTYPE_IPV4: u16 = 0x0800;
|
||||
const ETHERTYPE_EAPOL: u16 = 0x888e;
|
||||
const ETHERTYPE_SLOW_PROTOCOLS: u16 = 0x8809;
|
||||
#[cfg(test)]
|
||||
const ETHERTYPE_LLDP: u16 = 0x88cc;
|
||||
#[cfg(test)]
|
||||
const ETHERTYPE_IPV6: u16 = 0x86dd;
|
||||
#[cfg(test)]
|
||||
const ETHERTYPE_8021Q: u16 = 0x8100;
|
||||
#[cfg(test)]
|
||||
const ETHERTYPE_8021AD: u16 = 0x88a8;
|
||||
const ETHERTYPE_QINQ: u16 = 0x9100;
|
||||
#[cfg(test)]
|
||||
const IP_PROTOCOL_UDP: u8 = 17;
|
||||
const IPV6_NEXT_HEADER_HOP_BY_HOP: u8 = 0;
|
||||
const IPV6_NEXT_HEADER_ROUTING: u8 = 43;
|
||||
#[cfg(test)]
|
||||
const IPV6_NEXT_HEADER_FRAGMENT: u8 = 44;
|
||||
const IPV6_NEXT_HEADER_AH: u8 = 51;
|
||||
const IPV6_NEXT_HEADER_NO_NEXT: u8 = 59;
|
||||
#[cfg(test)]
|
||||
const IPV6_NEXT_HEADER_DESTINATION_OPTIONS: u8 = 60;
|
||||
#[cfg(test)]
|
||||
const IPV6_NEXT_HEADER_ICMPV6: u8 = 58;
|
||||
#[cfg(test)]
|
||||
const DHCPV4_SERVER_PORT: u16 = 67;
|
||||
#[cfg(test)]
|
||||
const DHCPV4_CLIENT_PORT: u16 = 68;
|
||||
#[cfg(test)]
|
||||
const DHCPV6_CLIENT_PORT: u16 = 546;
|
||||
#[cfg(test)]
|
||||
const DHCPV6_SERVER_PORT: u16 = 547;
|
||||
#[cfg(test)]
|
||||
const ICMPV6_ROUTER_ADVERTISEMENT: u8 = 134;
|
||||
|
||||
#[derive(Debug, Clone, PartialEq, Eq)]
|
||||
@@ -542,8 +554,12 @@ impl Room {
|
||||
|
||||
self.mark_peer_seen(ingress_peer_id, now);
|
||||
|
||||
if let Some(drop_reason) = safety_drop_reason(ingress_role, frame) {
|
||||
return Ok(ForwardingDecision::filtered(drop_reason));
|
||||
let safety_drop_reason = match ingress_role {
|
||||
Role::Client => remote_client_safety_drop_reason(frame),
|
||||
Role::Gateway => gateway_lan_safety_drop_reason(frame),
|
||||
};
|
||||
if let Some(drop_reason) = safety_drop_reason {
|
||||
return Ok(ForwardingDecision::filtered(DropReason::from(drop_reason)));
|
||||
}
|
||||
|
||||
if ingress_role == Role::Client
|
||||
@@ -726,177 +742,6 @@ fn client_total_bandwidth_limit() -> TokenBucket {
|
||||
)
|
||||
}
|
||||
|
||||
fn safety_drop_reason(ingress_role: Role, frame: EthernetFrame<'_>) -> Option<DropReason> {
|
||||
if frame.is_jumbo() {
|
||||
return Some(DropReason::JumboFrame);
|
||||
}
|
||||
|
||||
if is_control_plane_frame(frame) {
|
||||
return Some(DropReason::ControlPlaneEtherType);
|
||||
}
|
||||
|
||||
if ingress_role == Role::Client && is_vlan_tagged_frame(frame) {
|
||||
return Some(DropReason::VlanTaggedFrame);
|
||||
}
|
||||
|
||||
if ingress_role == Role::Client && is_dhcp_server_reply(frame) {
|
||||
return Some(DropReason::DhcpServerReply);
|
||||
}
|
||||
|
||||
if ingress_role == Role::Client && is_ipv6_router_advertisement(frame) {
|
||||
return Some(DropReason::Ipv6RouterAdvertisement);
|
||||
}
|
||||
|
||||
if ingress_role == Role::Client && is_ipv6_fragment(frame) {
|
||||
return Some(DropReason::Ipv6Fragment);
|
||||
}
|
||||
|
||||
None
|
||||
}
|
||||
|
||||
fn is_control_plane_frame(frame: EthernetFrame<'_>) -> bool {
|
||||
matches!(
|
||||
frame.ethertype_or_len(),
|
||||
ETHERTYPE_EAPOL | ETHERTYPE_SLOW_PROTOCOLS | ETHERTYPE_LLDP
|
||||
) || is_link_local_control_destination(frame.destination())
|
||||
}
|
||||
|
||||
fn is_vlan_tagged_frame(frame: EthernetFrame<'_>) -> bool {
|
||||
matches!(
|
||||
frame.ethertype_or_len(),
|
||||
ETHERTYPE_8021Q | ETHERTYPE_8021AD | ETHERTYPE_QINQ
|
||||
)
|
||||
}
|
||||
|
||||
fn is_link_local_control_destination(mac: MacAddr) -> bool {
|
||||
let [a, b, c, d, e, f] = mac.octets();
|
||||
|
||||
[a, b, c, d, e] == [0x01, 0x80, 0xc2, 0x00, 0x00] && f <= 0x0f
|
||||
}
|
||||
|
||||
fn is_dhcp_server_reply(frame: EthernetFrame<'_>) -> bool {
|
||||
is_ipv4_dhcp_server_reply(frame) || is_ipv6_dhcp_server_reply(frame)
|
||||
}
|
||||
|
||||
fn is_ipv4_dhcp_server_reply(frame: EthernetFrame<'_>) -> bool {
|
||||
let bytes = frame.bytes();
|
||||
if frame.ethertype_or_len() != ETHERTYPE_IPV4 || bytes.len() < ETHERNET_HEADER_LEN + 20 {
|
||||
return false;
|
||||
}
|
||||
|
||||
let ipv4 = &bytes[ETHERNET_HEADER_LEN..];
|
||||
let version = ipv4[0] >> 4;
|
||||
let header_len = usize::from(ipv4[0] & 0x0f) * 4;
|
||||
if version != 4 || header_len < 20 || ipv4.len() < header_len + 8 || ipv4[9] != IP_PROTOCOL_UDP
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
let udp = &ipv4[header_len..];
|
||||
let source_port = u16::from_be_bytes([udp[0], udp[1]]);
|
||||
let destination_port = u16::from_be_bytes([udp[2], udp[3]]);
|
||||
|
||||
source_port == DHCPV4_SERVER_PORT && destination_port == DHCPV4_CLIENT_PORT
|
||||
}
|
||||
|
||||
fn is_ipv6_dhcp_server_reply(frame: EthernetFrame<'_>) -> bool {
|
||||
let bytes = frame.bytes();
|
||||
if frame.ethertype_or_len() != ETHERTYPE_IPV6 || bytes.len() < ETHERNET_HEADER_LEN + 40 {
|
||||
return false;
|
||||
}
|
||||
|
||||
let ipv6 = &bytes[ETHERNET_HEADER_LEN..];
|
||||
if !is_ipv6_packet(ipv6) {
|
||||
return false;
|
||||
}
|
||||
|
||||
let Some(udp_offset) = ipv6_upper_layer_payload_offset(ipv6, IP_PROTOCOL_UDP) else {
|
||||
return false;
|
||||
};
|
||||
let Some(udp) = ipv6.get(udp_offset..udp_offset.saturating_add(4)) else {
|
||||
return false;
|
||||
};
|
||||
let source_port = u16::from_be_bytes([udp[0], udp[1]]);
|
||||
let destination_port = u16::from_be_bytes([udp[2], udp[3]]);
|
||||
|
||||
source_port == DHCPV6_SERVER_PORT && destination_port == DHCPV6_CLIENT_PORT
|
||||
}
|
||||
|
||||
fn is_ipv6_router_advertisement(frame: EthernetFrame<'_>) -> bool {
|
||||
let bytes = frame.bytes();
|
||||
if frame.ethertype_or_len() != ETHERTYPE_IPV6 || bytes.len() < ETHERNET_HEADER_LEN + 40 {
|
||||
return false;
|
||||
}
|
||||
|
||||
let ipv6 = &bytes[ETHERNET_HEADER_LEN..];
|
||||
if !is_ipv6_packet(ipv6) {
|
||||
return false;
|
||||
}
|
||||
|
||||
let Some(icmpv6_offset) = ipv6_upper_layer_payload_offset(ipv6, IPV6_NEXT_HEADER_ICMPV6) else {
|
||||
return false;
|
||||
};
|
||||
|
||||
ipv6.get(icmpv6_offset)
|
||||
.is_some_and(|message_type| *message_type == ICMPV6_ROUTER_ADVERTISEMENT)
|
||||
}
|
||||
|
||||
fn is_ipv6_fragment(frame: EthernetFrame<'_>) -> bool {
|
||||
let bytes = frame.bytes();
|
||||
if frame.ethertype_or_len() != ETHERTYPE_IPV6 || bytes.len() < ETHERNET_HEADER_LEN + 40 {
|
||||
return false;
|
||||
}
|
||||
|
||||
let ipv6 = &bytes[ETHERNET_HEADER_LEN..];
|
||||
is_ipv6_packet(ipv6) && ipv6_has_extension_header(ipv6, IPV6_NEXT_HEADER_FRAGMENT)
|
||||
}
|
||||
|
||||
fn is_ipv6_packet(ipv6: &[u8]) -> bool {
|
||||
ipv6.first().is_some_and(|first| first >> 4 == 6)
|
||||
}
|
||||
|
||||
fn ipv6_has_extension_header(ipv6: &[u8], expected_next_header: u8) -> bool {
|
||||
ipv6_upper_layer_payload_offset(ipv6, expected_next_header).is_some()
|
||||
}
|
||||
|
||||
fn ipv6_upper_layer_payload_offset(ipv6: &[u8], expected_next_header: u8) -> Option<usize> {
|
||||
let mut next_header = *ipv6.get(6)?;
|
||||
let mut offset = 40;
|
||||
|
||||
loop {
|
||||
match next_header {
|
||||
next_header if next_header == expected_next_header => return Some(offset),
|
||||
IPV6_NEXT_HEADER_NO_NEXT => return None,
|
||||
IPV6_NEXT_HEADER_HOP_BY_HOP
|
||||
| IPV6_NEXT_HEADER_ROUTING
|
||||
| IPV6_NEXT_HEADER_DESTINATION_OPTIONS => {
|
||||
let extension = ipv6.get(offset..offset.checked_add(2)?)?;
|
||||
next_header = extension[0];
|
||||
let extension_len = (usize::from(extension[1]) + 1) * 8;
|
||||
offset = offset.checked_add(extension_len)?;
|
||||
if offset > ipv6.len() {
|
||||
return None;
|
||||
}
|
||||
}
|
||||
IPV6_NEXT_HEADER_FRAGMENT => {
|
||||
let fragment = ipv6.get(offset..offset.checked_add(8)?)?;
|
||||
next_header = fragment[0];
|
||||
offset = offset.checked_add(8)?;
|
||||
}
|
||||
IPV6_NEXT_HEADER_AH => {
|
||||
let extension = ipv6.get(offset..offset.checked_add(2)?)?;
|
||||
next_header = extension[0];
|
||||
let extension_len = (usize::from(extension[1]) + 2) * 4;
|
||||
offset = offset.checked_add(extension_len)?;
|
||||
if offset > ipv6.len() {
|
||||
return None;
|
||||
}
|
||||
}
|
||||
_ => return None,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn reject_control_error(error: ControlError) -> Reject {
|
||||
let reason = match error {
|
||||
ControlError::UnsupportedVersion { .. } => RejectReason::UnsupportedVersion,
|
||||
|
||||
Reference in New Issue
Block a user