nf 0.1.0

A port of the netfilter framework written entirely in rust.
Documentation
#![allow(non_snake_case, non_upper_case_globals, non_camel_case_types)]

/// CAN netlink functionality for interface configuration
pub mod netlink;

/* controller area network (CAN) kernel definitions */

/* special address description flags for the CAN_ID */
/// Extended frame format flag, set in the MSB of CAN ID
pub const CAN_EFF_FLAG: u32 = 0x80000000u32; /* EFF/SFF is set in the MSB */
/// Remote transmission request flag
pub const CAN_RTR_FLAG: u32 = 0x40000000u32; /* remote transmission request */
/// Error message frame flag
pub const CAN_ERR_FLAG: u32 = 0x20000000u32; /* error message frame */

/* valid bits in CAN ID for frame formats */
/// Standard frame format (SFF) ID mask (11 bits)
pub const CAN_SFF_MASK: u32 = 0x000007FFu32; /* standard frame format (SFF) */
/// Extended frame format (EFF) ID mask (29 bits)
pub const CAN_EFF_MASK: u32 = 0x1FFFFFFFu32; /* extended frame format (EFF) */
/// Error frame mask (omits EFF, RTR, ERR flags)
pub const CAN_ERR_MASK: u32 = 0x1FFFFFFFu32; /* omit EFF, RTR, ERR flags */
/// CAN XL priority mask (11 bits)
pub const CANXL_PRIO_MASK: u32 = CAN_SFF_MASK; /* 11 bit priority mask */

/*
 * Controller Area Network Identifier structure
 *
 * bit 0-28	: CAN identifier (11/29 bit)
 * bit 29	: error message frame flag (0 = data frame, 1 = error message)
 * bit 30	: remote transmission request flag (1 = rtr frame)
 * bit 31	: frame format flag (0 = standard 11 bit, 1 = extended 29 bit)
 */
/// CAN identifier type (32-bit value with embedded flags)
pub type canid_t = u32;

/// Number of bits in a standard frame format identifier
pub const CAN_SFF_ID_BITS: u32 = 11;
/// Number of bits in an extended frame format identifier
pub const CAN_EFF_ID_BITS: u32 = 29;
/// Number of bits in CAN XL priority field
pub const CANXL_PRIO_BITS: u32 = CAN_SFF_ID_BITS;

/*
 * Controller Area Network Error Message Frame Mask structure
 *
 * bit 0-28	: error class mask (see include/uapi/linux/can/error.h)
 * bit 29-31	: set to zero
 */
/// CAN error message frame mask type
pub type can_err_mask_t = u32;

/* CAN payload length and DLC definitions according to ISO 11898-1 */
/// Maximum data length code for Classical CAN
pub const CAN_MAX_DLC: u32 = 8;
/// Maximum raw DLC value
pub const CAN_MAX_RAW_DLC: u32 = 15;
/// Maximum data length for Classical CAN in bytes
pub const CAN_MAX_DLEN: u32 = 8;

/* CAN FD payload length and DLC definitions according to ISO 11898-7 */
/// Maximum data length code for CAN FD
pub const CANFD_MAX_DLC: u32 = 15;
/// Maximum data length for CAN FD in bytes
pub const CANFD_MAX_DLEN: u32 = 64;

/*
 * CAN XL payload length and DLC definitions according to ISO 11898-1
 * CAN XL DLC ranges from 0 .. 2047 => data length from 1 .. 2048 byte
 */
/// Minimum data length code for CAN XL
pub const CANXL_MIN_DLC: u32 = 0;
/// Maximum data length code for CAN XL
pub const CANXL_MAX_DLC: u32 = 2047;
/// Mask for CAN XL DLC field
pub const CANXL_MAX_DLC_MASK: u32 = 0x07FF;
/// Minimum data length for CAN XL in bytes
pub const CANXL_MIN_DLEN: u32 = 1;
/// Maximum data length for CAN XL in bytes
pub const CANXL_MAX_DLEN: u32 = 2048;

/*
 * defined bits for canfd_frame.flags
 *
 * The use of struct canfd_frame implies the FD Frame (FDF) bit to
 * be set in the CAN frame bitstream on the wire. The FDF bit switch turns
 * the CAN controllers bitstream processor into the CAN FD mode which creates
 * two new options within the CAN FD frame specification:
 *
 * Bit Rate Switch - to indicate a second bitrate is/was used for the payload
 * Error State Indicator - represents the error state of the transmitting node
 *
 * As the CANFD_ESI bit is internally generated by the transmitting CAN
 * controller only the CANFD_BRS bit is relevant for real CAN controllers when
 * building a CAN FD frame for transmission. Setting the CANFD_ESI bit can make
 * sense for virtual CAN interfaces to test applications with echoed frames.
 *
 * The struct can_frame and struct canfd_frame intentionally share the same
 * layout to be able to write CAN frame content into a CAN FD frame structure.
 * When this is done the former differentiation via CAN_MTU / CANFD_MTU gets
 * lost. CANFD_FDF allows programmers to mark CAN FD frames in the case of
 * using struct canfd_frame for mixed CAN / CAN FD content (dual use).
 * Since the introduction of CAN XL the CANFD_FDF flag is set in all CAN FD
 * frame structures provided by the CAN subsystem of the Linux kernel.
*/
/// Bit rate switch flag (second bitrate for payload data)
pub const CANFD_BRS: u32 = 0x01; /* bit rate switch (second bitrate for payload data) */
/// Error state indicator flag of the transmitting node
pub const CANFD_ESI: u32 = 0x02; /* error state indicator of the transmitting node */
/// Flag to mark CAN FD for dual use of struct canfd_frame
pub const CANFD_FDF: u32 = 0x04; /* mark CAN FD for dual use of struct canfd_frame */

/*
 * defined bits for canxl_frame.flags
 *
 * The canxl_frame.flags element contains two bits CANXL_XLF and CANXL_SEC
 * and shares the relative position of the struct can[fd]_frame.len element.
 * The CANXL_XLF bit ALWAYS needs to be set to indicate a valid CAN XL frame.
 * As a side effect setting this bit intentionally breaks the length checks
 * for Classical CAN and CAN FD frames.
 *
 * Undefined bits in canxl_frame.flags are reserved and shall be set to zero.
 */
/// Mandatory CAN XL frame flag (must always be set)
pub const CANXL_XLF: u32 = 0x80; /* mandatory CAN XL frame flag (must always be set!) */
/// Simple Extended Content flag (security/segmentation)
pub const CANXL_SEC: u32 = 0x01; /* Simple Extended Content (security/segmentation) */

/// MTU size for Classical CAN frames
pub const CAN_MTU: usize = size_of::<can_frame>();
/// MTU size for CAN FD frames
pub const CANFD_MTU: usize = size_of::<canfd_frame>();
/// MTU size for CAN XL frames
pub const CANXL_MTU: usize = size_of::<canxl_frame>();
/// Header size for CAN XL frames
pub const CANXL_HDR_SIZE: usize = std::mem::offset_of!(canxl_frame, data);
/// Minimum MTU size for CAN XL frames
pub const CANXL_MIN_MTU: usize = CANXL_HDR_SIZE + 64;
/// Maximum MTU size for CAN XL frames
pub const CANXL_MAX_MTU: usize = CANXL_MTU;

/// Raw CAN protocol
pub const CAN_RAW: u32 = 1;
/// Broadcast Manager CAN protocol
pub const CAN_BCM: u32 = 2;
/// Virtual Terminal Protocol
pub const CAN_TP16: u32 = 3;
/// ISO 15765-2 Transport Protocol
pub const CAN_TP20: u32 = 4;
/// Bosch MCNet protocol
pub const CAN_MCNET: u32 = 5;
/// ISO 15765-2 Transport Protocol
pub const CAN_ISOTP: u32 = 6;
/// SAE J1939 protocol
pub const CAN_J1939: u32 = 7;
/// Number of CAN protocols
pub const CAN_NPROTO: u32 = 8;

/// CAN frame structure for Classical CAN
#[derive(Copy, Clone)]
#[repr(C)]
pub struct can_frame {
    /// CAN ID with optional EFF/RTR/ERR flags
    pub can_id: canid_t,
    /// CAN frame payload length in byte (0..CAN_MAX_DLEN)
    pub len: u8,
    /// Padding
    pub __pad: u8,
    /// Reserved
    pub __res0: u8,
    /// Reserved
    pub __res1: u8,
    /// CAN frame payload data
    pub data: [u8; CAN_MAX_DLEN as usize],
}

/// CAN frame header union for compatibility
#[derive(Copy, Clone)]
#[repr(C)]
pub union can_word_hdr {
    /// CAN frame payload length in byte (0 .. CAN_MAX_DLEN)
    /// was previously named can_dlc so we need to carry that
    /// definition around for legacy support
    pub can_dlc: u8,
    /// CAN frame payload length in byte (0 .. CAN_MAX_DLEN)
    pub len: u8,
}

/// CAN FD frame structure
#[derive(Debug, Copy, Clone, Eq, PartialEq, Ord, PartialOrd, Hash)]
#[repr(C)]
pub struct canfd_frame {
    /// CAN ID with optional EFF/RTR/ERR flags
    pub can_id: canid_t,
    /// Frame payload length in byte (0..CANFD_MAX_DLEN)
    pub len: u8,
    /// Additional flags for CAN FD
    pub flags: u8,
    /// Reserved
    pub __res0: u8,
    /// Reserved
    pub __res1: u8,
    /// CAN FD frame payload data
    pub data: [u8; CANFD_MAX_DLEN as usize],
}

/// CAN XL frame structure
#[derive(Debug, Copy, Clone, Eq, PartialEq, Ord, PartialOrd, Hash)]
#[repr(C)]
pub struct canxl_frame {
    /// CAN ID / Priority
    pub prio: canid_t,
    /// Flags for CAN XL (CANXL_XLF must be set)
    pub flags: u8,
    /// Virtual CAN network (0 = physical network)
    pub vcid: u8,
    /// Data length in byte (1 .. CANXL_MAX_DLEN)
    pub len: u16,
    /// SDT (service data type)
    pub sdt: u32,
    /// CAN XL frame payload data
    pub data: [u8; CANXL_MAX_DLEN as usize],
}

/// Socket address structure for CAN sockets
#[derive(Copy, Clone)]
#[repr(C)]
pub struct sockaddr_can {
    /// Address family: AF_CAN
    pub can_family: u16,
    /// Interface index
    pub can_ifindex: i32,
    /// Protocol specific address information
    pub can_addr: can_addr,
}

/// J1939 address information structure
#[derive(Debug, Copy, Clone, Eq, PartialEq, Ord, PartialOrd, Hash)]
#[repr(C)]
pub struct can_j1939 {
    /// Name (8 byte)
    pub name: u64,
    /// PGN (parameter group number)
    pub pgn: u32,
    /// Address
    pub addr: u8,
}

/// Transport protocol address information structure
#[derive(Debug, Copy, Clone, Eq, PartialEq, Ord, PartialOrd, Hash)]
#[repr(C)]
pub struct can_tp {
    /// Receive CAN identifier
    pub rx_id: canid_t,
    /// Transmit CAN identifier
    pub tx_id: canid_t,
}

/// Union of different address types for CAN sockets
#[derive(Copy, Clone)]
#[repr(C)]
pub union can_addr {
    /// Transport protocol class address information (e.g. ISOTP)
    pub tp: can_tp,
    /// J1939 protocol address information
    pub j1939: can_j1939,
}

/// CAN filter structure
#[derive(Debug, Copy, Clone, Eq, PartialEq, Ord, PartialOrd, Hash)]
#[repr(C)]
pub struct can_filter {
    /// CAN ID to filter
    pub can_id: canid_t,
    /// CAN ID filter mask
    pub can_mask: canid_t,
}