pub struct SkMsg { /* private fields */ }Expand description
A program used to intercept messages sent with sendmsg()/sendfile().
SkMsg programs are attached to socket maps, and can be used inspect,
filter and redirect messages sent on sockets. See also SockMap and
SockHash.
§Minimum kernel version
The minimum kernel version required to use this feature is 4.17.
§Examples
use std::io::Write;
use std::net::TcpStream;
use std::os::fd::AsRawFd;
use aya::maps::SockHash;
use aya::programs::SkMsg;
let intercept_egress: SockHash<_, u32> = bpf.map("INTERCEPT_EGRESS").unwrap().try_into()?;
let map_fd = intercept_egress.fd().try_clone()?;
let prog: &mut SkMsg = bpf.program_mut("intercept_egress_packet").unwrap().try_into()?;
prog.load()?;
prog.attach(&map_fd)?;
let mut client = TcpStream::connect("127.0.0.1:1234")?;
let mut intercept_egress: SockHash<_, u32> = bpf.map_mut("INTERCEPT_EGRESS").unwrap().try_into()?;
intercept_egress.insert(1234, client.as_raw_fd(), 0)?;
// the write will be intercepted
client.write_all(b"foo")?;Implementations§
Source§impl SkMsg
impl SkMsg
Sourcepub const PROGRAM_TYPE: ProgramType = ProgramType::SkMsg
pub const PROGRAM_TYPE: ProgramType = ProgramType::SkMsg
The type of the program according to the kernel.
Sourcepub fn load(&mut self) -> Result<(), ProgramError>
pub fn load(&mut self) -> Result<(), ProgramError>
Loads the program inside the kernel.
Sourcepub fn attach(&mut self, map: &SockMapFd) -> Result<SkMsgLinkId, ProgramError>
pub fn attach(&mut self, map: &SockMapFd) -> Result<SkMsgLinkId, ProgramError>
Attaches the program to the given sockmap.
The returned value can be used to detach, see SkMsg::detach.
Source§impl SkMsg
impl SkMsg
Sourcepub fn detach(&mut self, link_id: SkMsgLinkId) -> Result<(), ProgramError>
pub fn detach(&mut self, link_id: SkMsgLinkId) -> Result<(), ProgramError>
Detaches the program.
See Self::attach.
Sourcepub fn take_link(
&mut self,
link_id: SkMsgLinkId,
) -> Result<SkMsgLink, ProgramError>
pub fn take_link( &mut self, link_id: SkMsgLinkId, ) -> Result<SkMsgLink, ProgramError>
Takes ownership of the link referenced by the provided link_id.
The caller takes the responsibility of managing the lifetime of the link. When the
returned
SkMsgLink
is dropped, the link will be detached.
Source§impl SkMsg
impl SkMsg
Sourcepub fn unload(&mut self) -> Result<(), ProgramError>
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 SkMsg
impl SkMsg
Sourcepub fn fd(&self) -> Result<&ProgramFd, ProgramError>
pub fn fd(&self) -> Result<&ProgramFd, ProgramError>
Returns the file descriptor of this Program.
Source§impl SkMsg
impl SkMsg
Sourcepub fn pin<P: AsRef<Path>>(&mut self, path: P) -> Result<(), PinError>
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§impl SkMsg
impl SkMsg
Sourcepub fn from_pin<P: AsRef<Path>>(path: P) -> Result<Self, ProgramError>
pub fn from_pin<P: AsRef<Path>>(path: P) -> Result<Self, ProgramError>
Creates a program from a pinned entry on a bpffs.
Existing links will not be populated. To work with existing links you should use crate::programs::links::PinnedLink.
On drop, any managed links are detached and the program is unloaded. This will not result in the program being unloaded from the kernel if it is still pinned.
Source§impl SkMsg
impl SkMsg
Sourcepub fn from_program_info(
info: ProgramInfo,
name: Cow<'static, str>,
) -> Result<Self, ProgramError>
pub fn from_program_info( info: ProgramInfo, name: Cow<'static, str>, ) -> Result<Self, ProgramError>
Constructs an instance of a Self from a ProgramInfo.
This allows the caller to get a handle to an already loaded program from the kernel without having to load it again.
§Errors
- If the program type reported by the kernel does not match
Self::PROGRAM_TYPE. - If the file descriptor of the program cannot be cloned.
Source§impl SkMsg
impl SkMsg
Sourcepub fn info(&self) -> Result<ProgramInfo, ProgramError>
pub fn info(&self) -> Result<ProgramInfo, ProgramError>
Returns metadata information of this program.
Uses kernel v4.13 features.