network-types

Rust structs representing network protocol headers (on Layer 2, 3 and 4).

The crate is no_std, which makes it a great fit for eBPF programs written with Aya.

Examples

An example of an XDP program logging information about addresses and ports for incoming packets:

```rust use core::mem;

use ayabpf::{bindings::xdpaction, macros::xdp, programs::XdpContext}; use ayalogebpf::info;

use network_types::{ eth::{EthHdr, EtherType}, ip::{Ipv4Hdr, IpProto}, tcp::TcpHdr, udp::UdpHdr, };

[xdp]

pub fn xdpfirewall(ctx: XdpContext) -> u32 { match tryxdpfirewall(ctx) { Ok(ret) => ret, Err() => xdpaction::XDPPASS, } }

[inline(always)]

unsafe fn ptrat(ctx: &XdpContext, offset: usize) -> Result<*const T, ()> { let start = ctx.data(); let end = ctx.dataend(); let len = mem::size_of::();

if start + offset + len > end {
    return Err(());
}

Ok((start + offset) as *const T)

}

fn tryxdpfirewall(ctx: XdpContext) -> Resultat(&ctx, 0)? }; match unsafe { *ethhdr }.ethertype { EtherType::Ipv4 => {} _ => return Ok(xdpaction::XDPPASS), }

let ipv4hdr: *const Ipv4Hdr = unsafe { ptr_at(&ctx, EthHdr::LEN)? };
let source_addr = u32::from_be(unsafe { *ipv4hdr }.src_addr);

let source_port = match unsafe { *ipv4hdr }.proto {
    IpProto::Tcp => {
        let tcphdr: *const TcpHdr =
            unsafe { ptr_at(&ctx, EthHdr::LEN + Ipv4Hdr::LEN) }?;
        u16::from_be(unsafe { *tcphdr }.source)
    }
    IpProto::Udp => {
        let udphdr: *const UdpHdr =
            unsafe { ptr_at(&ctx, EthHdr::LEN + Ipv4Hdr::LEN) }?;
        u16::from_be(unsafe { *udphdr }.source)
    }
    _ => return Err(()),
};

info!(&ctx, "SRC IP: {}, SRC PORT: {}", source_addr, source_port);

Ok(xdp_action::XDP_PASS)

} ```

Naming conventions

When naming stucts and fields, we are trying to stick to the following principles:

License: MIT