Procházet zdrojové kódy

bpf: Implement redirect for skbs on sockmap/sockhash

This implements redirect_skb and renames redirect to redirect_msg

Signed-off-by: Dave Tucker <dave@dtucker.co.uk>
Dave Tucker před 3 roky
rodič
revize
2a8ba55b7e

+ 12 - 3
bpf/aya-bpf/src/maps/sock_hash.rs

@@ -4,9 +4,9 @@ use aya_bpf_cty::c_void;
 
 use crate::{
     bindings::{bpf_map_def, bpf_map_type::BPF_MAP_TYPE_SOCKHASH, bpf_sock_ops},
-    helpers::{bpf_msg_redirect_hash, bpf_sock_hash_update},
+    helpers::{bpf_msg_redirect_hash, bpf_sk_redirect_hash, bpf_sock_hash_update},
     maps::PinningType,
-    programs::SkMsgContext,
+    programs::{SkMsgContext, SkSkbContext},
     BpfContext,
 };
 
@@ -66,7 +66,7 @@ impl<K> SockHash<K> {
         }
     }
 
-    pub unsafe fn redirect(&mut self, ctx: &SkMsgContext, key: &mut K, flags: u64) -> i64 {
+    pub unsafe fn redirect_msg(&mut self, ctx: &SkMsgContext, key: &mut K, flags: u64) -> i64 {
         bpf_msg_redirect_hash(
             ctx.as_ptr() as *mut _,
             &mut self.def as *mut _ as *mut _,
@@ -74,4 +74,13 @@ impl<K> SockHash<K> {
             flags,
         )
     }
+
+    pub unsafe fn redirect_skb(&mut self, ctx: &SkSkbContext, key: &mut K, flags: u64) -> i64 {
+        bpf_sk_redirect_hash(
+            ctx.as_ptr() as *mut _,
+            &mut self.def as *mut _ as *mut _,
+            key as *mut _ as *mut _,
+            flags,
+        )
+    }
 }

+ 12 - 3
bpf/aya-bpf/src/maps/sock_map.rs

@@ -4,9 +4,9 @@ use aya_bpf_cty::c_void;
 
 use crate::{
     bindings::{bpf_map_def, bpf_map_type::BPF_MAP_TYPE_SOCKMAP, bpf_sock_ops},
-    helpers::{bpf_msg_redirect_map, bpf_sock_map_update},
+    helpers::{bpf_msg_redirect_map, bpf_sk_redirect_map, bpf_sock_map_update},
     maps::PinningType,
-    programs::SkMsgContext,
+    programs::{SkMsgContext, SkSkbContext},
     BpfContext,
 };
 
@@ -63,7 +63,7 @@ impl SockMap {
         }
     }
 
-    pub unsafe fn redirect(&mut self, ctx: &SkMsgContext, index: u32, flags: u64) -> i64 {
+    pub unsafe fn redirect_msg(&mut self, ctx: &SkMsgContext, index: u32, flags: u64) -> i64 {
         bpf_msg_redirect_map(
             ctx.as_ptr() as *mut _,
             &mut self.def as *mut _ as *mut _,
@@ -71,4 +71,13 @@ impl SockMap {
             flags,
         )
     }
+
+    pub unsafe fn redirect_skb(&mut self, ctx: &SkSkbContext, index: u32, flags: u64) -> i64 {
+        bpf_sk_redirect_map(
+            ctx.as_ptr() as *mut _,
+            &mut self.def as *mut _ as *mut _,
+            index,
+            flags,
+        )
+    }
 }