Skip to main content

SocketFilter

Struct SocketFilter 

Source
pub struct SocketFilter { /* private fields */ }
Expand description

A program used to inspect and filter incoming packets on a socket.

SocketFilter programs are attached on sockets and can be used to inspect and filter incoming packets.

Each socket has one filter slot. Attaching a new SocketFilter replaces the socket’s current filter, and detaching clears that current filter regardless of which program installed it. Aya therefore does not expose a link-style attachment handle for SocketFilter or automatically track socket filter attachments for cleanup. Dropping SocketFilter or crate::Ebpf does not detach the filter; call SocketFilter::detach explicitly when you want to remove it, or close the socket.

§Minimum kernel version

The minimum kernel version required to use this feature is 3.19. BPF_PROG_TYPE_SOCKET_FILTER and SO_ATTACH_BPF are present in Linux v3.19: https://github.com/torvalds/linux/blob/v3.19/include/uapi/linux/bpf.h#L118-L120 https://github.com/torvalds/linux/blob/v3.19/include/uapi/asm-generic/socket.h#L87-L88

§Examples

use std::net::TcpStream;
use aya::programs::SocketFilter;

let mut client = TcpStream::connect("127.0.0.1:1234")?;
let prog: &mut SocketFilter = bpf.program_mut("filter_packets").unwrap().try_into()?;
prog.load()?;
prog.attach(&client)?;

Implementations§

Source§

impl SocketFilter

Source

pub const PROGRAM_TYPE: ProgramType = ProgramType::SocketFilter

The type of the program according to the kernel.

Source

pub fn load(&mut self) -> Result<(), ProgramError>

Loads the program inside the kernel.

Source

pub fn attach<T: AsFd>(&self, socket: T) -> Result<(), ProgramError>

Attaches the filter on the given socket.

If the socket already has a filter attached, attaching again replaces the current filter instead of returning an already-attached error. This follows the kernel model: each socket has one filter slot and cannot run multiple socket filters together. The kernel detach API also clears the socket’s current filter slot; it cannot detach a specific program attachment. For that reason, Aya does not provide link-level RAII semantics for socket filters. Dropping SocketFilter or crate::Ebpf does not detach the filter. Call SocketFilter::detach explicitly when you want to remove it, or close the socket.

Source

pub fn detach<T: AsFd>(socket: T) -> Result<(), ProgramError>

Detaches the current filter from the given socket.

Detaching clears the socket’s current filter slot, regardless of which program was used to attach that filter. Unlike SocketFilter::attach, this operation does not require the program to remain loaded in this process. If another filter replaced this program on the same socket, detaching will remove that replacement filter.

Source

pub fn from_pin<P: AsRef<Path>>(path: P) -> Result<Self, ProgramError>

Creates a program from a pinned entry on a bpffs.

SocketFilter does not use link-style attachments, so this only restores access to the pinned program itself.

Dropping the returned value unloads the local program FD, but does not detach the filter from any socket. This will also not unload the program from the kernel while it remains pinned.

Source§

impl SocketFilter

Source

pub fn unload(&mut self) -> Result<(), ProgramError>

Unloads the program from the kernel.

Tracked links will be detached before unloading the program. Attachment mechanisms that do not create tracked links are not affected. Note that owned links obtained using take_link() will not be detached.

Source§

impl SocketFilter

Source

pub fn fd(&self) -> Result<&ProgramFd, ProgramError>

Returns the file descriptor of this Program.

Source§

impl SocketFilter

Source

pub fn pin<P: AsRef<Path>>(&mut self, path: P) -> Result<(), PinError>

Pins the program to a BPF filesystem.

When a BPF object is pinned to a BPF filesystem it will remain loaded after Aya has unloaded the program. To remove the program, the file on the BPF filesystem must be removed. Any directories in the the path provided should have been created by the caller.

Source

pub fn unpin(&mut self) -> Result<(), Error>

Removes the pinned link from the filesystem.

Source§

impl SocketFilter

Source

pub fn info(&self) -> Result<ProgramInfo, ProgramError>

Returns metadata information of this program.

Uses kernel v4.13 features.

Trait Implementations§

Source§

impl Debug for SocketFilter

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Drop for SocketFilter

Source§

fn drop(&mut self)

Executes the destructor for this type. Read more
Source§

fn pin_drop(self: Pin<&mut Self>)

🔬This is a nightly-only experimental API. (pin_ergonomics)
Execute the destructor for this type, but different to Drop::drop, it requires self to be pinned. Read more
Source§

impl TestRun for SocketFilter

Source§

type Opts<'a> = TestRunOptions<'a>

The options type used to configure a single test invocation. Read more
Source§

type Result = TestRunResult

The Result type for a single test invocation.
Source§

fn test_run(&self, opts: Self::Opts<'_>) -> Result<Self::Result, ProgramError>

Runs the program with test input data and returns the result. Read more
Source§

impl<'a> TryFrom<&'a Program> for &'a SocketFilter

Source§

type Error = ProgramError

The type returned in the event of a conversion error.
Source§

fn try_from(program: &'a Program) -> Result<&'a SocketFilter, ProgramError>

Performs the conversion.
Source§

impl<'a> TryFrom<&'a mut Program> for &'a mut SocketFilter

Source§

type Error = ProgramError

The type returned in the event of a conversion error.
Source§

fn try_from( program: &'a mut Program, ) -> Result<&'a mut SocketFilter, ProgramError>

Performs the conversion.

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.