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
1 change: 1 addition & 0 deletions crates/kernel/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,7 @@ pub mod sched;
#[allow(dead_code, clippy::all)]
pub mod sched_syscalls;
pub mod sctp;
pub(crate) mod sctp_packet;
pub mod seccomp;
pub mod seccomp_filter;
pub mod seccomp_notifier;
Expand Down
55 changes: 50 additions & 5 deletions crates/kernel/src/net.rs
Original file line number Diff line number Diff line change
Expand Up @@ -350,6 +350,9 @@ pub const PROTO_UDP: u8 = 17;
/// IP protocol number for GRE (RFC 2784).
pub const PROTO_GRE: u8 = 47;

/// IP protocol number for SCTP (RFC 9260).
pub const PROTO_SCTP: u8 = 132;

/// Parsed IPv4 header.
///
/// Stored in host byte order; the raw on-wire format uses
Expand Down Expand Up @@ -642,6 +645,10 @@ pub struct NetworkStack {
gre_admitted: u64,
/// Count of GRE frames rejected by [`crate::gre::validate_gre_packet`].
gre_dropped: u64,
/// Count of SCTP packets admitted by the protocol-132 gate.
sctp_admitted: u64,
/// Count of SCTP packets rejected by the protocol-132 gate.
sctp_dropped: u64,
}

impl NetworkStack {
Expand All @@ -661,6 +668,8 @@ impl NetworkStack {
ipv6: Ipv6Stack::new(Ipv6Addr::link_local_from_mac(&mac), mac),
gre_admitted: 0,
gre_dropped: 0,
sctp_admitted: 0,
sctp_dropped: 0,
}
}

Expand All @@ -674,6 +683,16 @@ impl NetworkStack {
self.gre_dropped
}

/// Number of SCTP packets admitted by the protocol-132 gate.
pub const fn sctp_admitted(&self) -> u64 {
self.sctp_admitted
}

/// Number of SCTP packets dropped by the protocol-132 gate.
pub const fn sctp_dropped(&self) -> u64 {
self.sctp_dropped
}

/// Process an incoming Ethernet frame.
///
/// Parses the Ethernet header and dispatches to the appropriate
Expand Down Expand Up @@ -826,11 +845,11 @@ impl NetworkStack {
/// Dispatches by IP protocol after verifying the IPv4 header checksum and
/// that the datagram is addressed to us: ICMP echo requests are answered
/// (returning the reply frame length), UDP datagrams pass a checksum and
/// per-port framing admission gate, and GRE (protocol 47) is validated
/// structurally with C-bit checksum verification. Only ICMP produces a
/// reply; UDP and GRE are consumed and return `Ok(0)`, which also covers
/// every silently dropped malformed frame. Any other protocol returns
/// [`Error::NotImplemented`].
/// per-port framing admission gate, GRE (protocol 47) is validated with
/// C-bit checksum verification, and SCTP (protocol 132) passes a stateless
/// CRC32c and chunk-chain gate. Only ICMP produces a reply; UDP, GRE, and
/// SCTP are consumed and return `Ok(0)`, including silently dropped
/// malformed frames. Any other protocol returns [`Error::NotImplemented`].
fn handle_ipv4(
&mut self,
eth: &EtherHeader,
Expand Down Expand Up @@ -872,6 +891,13 @@ impl NetworkStack {
}
Ok(0)
}
PROTO_SCTP => {
match self.handle_sctp_packet(&ip_hdr, ip_payload) {
Ok(()) => self.sctp_admitted = self.sctp_admitted.saturating_add(1),
Err(_) => self.sctp_dropped = self.sctp_dropped.saturating_add(1),
}
Ok(0)
}
_ => Err(Error::NotImplemented),
}
}
Expand Down Expand Up @@ -933,6 +959,21 @@ impl NetworkStack {
Ok(())
}

/// Validate an SCTP packet before protocol-132 admission.
///
/// This stack has no IPv4 reassembly, so MF or a nonzero fragment offset
/// is rejected locally. DF is outside the mask and remains admissible.
///
/// # Errors
///
/// Returns [`Error::InvalidArgument`] for fragments or malformed SCTP.
fn handle_sctp_packet(&self, ip_hdr: &Ipv4Header, sctp_payload: &[u8]) -> Result<()> {
if ip_hdr.flags_frag & 0x3FFF != 0 {
return Err(Error::InvalidArgument);
}
crate::sctp_packet::validate_sctp_packet(sctp_payload)
}

/// Validate an incoming UDP datagram before delivery.
///
/// Re-parses the datagram so the on-wire UDP `length` field — not
Expand Down Expand Up @@ -1047,6 +1088,10 @@ impl NetworkStack {
}
}

#[cfg(test)]
#[path = "net_sctp_tests.rs"]
mod sctp_tests;

// =========================================================================
// Tests
// =========================================================================
Expand Down
170 changes: 170 additions & 0 deletions crates/kernel/src/net_sctp_tests.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,170 @@
// Copyright 2026 ONCRIX Contributors
// SPDX-License-Identifier: Apache-2.0

use super::*;

const LOCAL_MAC: [u8; 6] = [0xAA; 6];
const SOURCE_MAC: [u8; 6] = [0xBB; 6];
const LOCAL_IP: [u8; 4] = [10, 0, 0, 2];
const SOURCE_IP: [u8; 4] = [10, 0, 0, 1];
const SCTP_INIT_PACKET: [u8; 32] = [
0x13, 0x88, 0x00, 0x50, 0x00, 0x00, 0x00, 0x00, 0x12, 0xF6, 0xF6, 0xE9, 0x01, 0x00, 0x00, 0x14,
0xDE, 0xAD, 0xBE, 0xEF, 0x00, 0x01, 0x00, 0x00, 0x00, 0x02, 0x00, 0x02, 0x71, 0x5B, 0x5A, 0x94,
];

fn sctp_stack() -> NetworkStack {
NetworkStack::new(LOCAL_MAC, LOCAL_IP, SOURCE_IP, [255, 255, 255, 0])
}

fn ip_header(flags_frag: u16, protocol: u8) -> Ipv4Header {
Ipv4Header {
version_ihl: 0x45,
tos: 0,
total_len: (IPV4_HEADER_MIN_LEN + SCTP_INIT_PACKET.len()) as u16,
id: 0,
flags_frag,
ttl: 64,
protocol,
checksum: 0,
src_addr: SOURCE_IP,
dst_addr: LOCAL_IP,
}
}

fn ip_frame(protocol: u8, flags_frag: u16, payload: &[u8]) -> ([u8; 128], usize) {
let mut frame = [0u8; 128];
assert_eq!(
write_ether(&mut frame, &LOCAL_MAC, &SOURCE_MAC, ETHER_TYPE_IPV4),
Ok(ETHER_HEADER_LEN)
);
let mut ip = ip_header(flags_frag, protocol);
ip.total_len = (IPV4_HEADER_MIN_LEN + payload.len()) as u16;
assert_eq!(
write_ipv4(&mut frame[ETHER_HEADER_LEN..], &ip),
Ok(IPV4_HEADER_MIN_LEN)
);
let payload_offset = ETHER_HEADER_LEN + IPV4_HEADER_MIN_LEN;
frame[payload_offset..payload_offset + payload.len()].copy_from_slice(payload);
(frame, payload_offset + payload.len())
}

#[test]
fn sctp_direct_valid_packet_is_admitted() {
let stack = sctp_stack();
assert_eq!(
stack.handle_sctp_packet(&ip_header(0, PROTO_SCTP), &SCTP_INIT_PACKET),
Ok(())
);
}

#[test]
fn sctp_direct_corrupt_packet_is_rejected() {
let stack = sctp_stack();
let mut packet = SCTP_INIT_PACKET;
packet[8] ^= 1;
assert_eq!(
stack.handle_sctp_packet(&ip_header(0, PROTO_SCTP), &packet),
Err(Error::InvalidArgument)
);
}

#[test]
fn sctp_direct_more_fragments_and_offset_are_rejected() {
let stack = sctp_stack();
assert_eq!(
stack.handle_sctp_packet(&ip_header(0x2000, PROTO_SCTP), &SCTP_INIT_PACKET),
Err(Error::InvalidArgument)
);
assert_eq!(
stack.handle_sctp_packet(&ip_header(1, PROTO_SCTP), &SCTP_INIT_PACKET),
Err(Error::InvalidArgument)
);
}

#[test]
fn sctp_direct_do_not_fragment_is_admitted() {
let stack = sctp_stack();
assert_eq!(
stack.handle_sctp_packet(&ip_header(0x4000, PROTO_SCTP), &SCTP_INIT_PACKET),
Ok(())
);
}

#[test]
fn sctp_protocol_132_is_distinct_from_unsupported_protocol() {
let mut stack = sctp_stack();
let mut reply = [0u8; 128];
let (sctp_frame, sctp_len) = ip_frame(PROTO_SCTP, 0, &SCTP_INIT_PACKET);
assert_eq!(
stack.process_packet(&sctp_frame[..sctp_len], &mut reply),
Ok(0)
);
let (unknown_frame, unknown_len) = ip_frame(99, 0, &SCTP_INIT_PACKET);
assert_eq!(
stack.process_packet(&unknown_frame[..unknown_len], &mut reply),
Err(Error::NotImplemented)
);
}

#[test]
fn sctp_corrupt_boundary_packet_is_silently_dropped() {
let mut stack = sctp_stack();
let mut packet = SCTP_INIT_PACKET;
packet[8] ^= 1;
let (frame, len) = ip_frame(PROTO_SCTP, 0, &packet);
let mut reply = [0u8; 128];
assert_eq!(stack.process_packet(&frame[..len], &mut reply), Ok(0));
}

#[test]
fn sctp_valid_boundary_packet_increments_admitted_only() {
let mut stack = sctp_stack();
let (frame, len) = ip_frame(PROTO_SCTP, 0, &SCTP_INIT_PACKET);
let mut reply = [0u8; 128];
assert_eq!(stack.process_packet(&frame[..len], &mut reply), Ok(0));
assert_eq!(stack.sctp_admitted(), 1);
assert_eq!(stack.sctp_dropped(), 0);
}

#[test]
fn sctp_corrupt_boundary_packet_increments_dropped_only() {
let mut stack = sctp_stack();
let mut packet = SCTP_INIT_PACKET;
packet[8] ^= 1;
let (frame, len) = ip_frame(PROTO_SCTP, 0, &packet);
let mut reply = [0u8; 128];
assert_eq!(stack.process_packet(&frame[..len], &mut reply), Ok(0));
assert_eq!(stack.sctp_admitted(), 0);
assert_eq!(stack.sctp_dropped(), 1);
}

#[test]
fn sctp_fragment_boundary_packet_increments_dropped_only() {
let mut stack = sctp_stack();
let (frame, len) = ip_frame(PROTO_SCTP, 0x2000, &SCTP_INIT_PACKET);
let mut reply = [0u8; 128];
assert_eq!(stack.process_packet(&frame[..len], &mut reply), Ok(0));
assert_eq!(stack.sctp_admitted(), 0);
assert_eq!(stack.sctp_dropped(), 1);
}

#[test]
fn sctp_fragment_gate_does_not_change_gre_fragment_admission() {
let mut stack = sctp_stack();
let mut reply = [0u8; 128];
let (sctp_frame, sctp_len) = ip_frame(PROTO_SCTP, 0x2000, &SCTP_INIT_PACKET);
assert_eq!(
stack.process_packet(&sctp_frame[..sctp_len], &mut reply),
Ok(0)
);

let gre_packet = [0x00, 0x00, 0x08, 0x00];
let (gre_frame, gre_len) = ip_frame(PROTO_GRE, 0x2000, &gre_packet);
assert_eq!(
stack.process_packet(&gre_frame[..gre_len], &mut reply),
Ok(0)
);
assert_eq!(stack.sctp_dropped(), 1);
assert_eq!(stack.gre_admitted(), 1);
assert_eq!(stack.gre_dropped(), 0);
}
Loading