Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0
0002 #include <stdint.h>
0003 #include <stdbool.h>
0004 
0005 #include <linux/bpf.h>
0006 #include <linux/stddef.h>
0007 #include <linux/pkt_cls.h>
0008 #include <linux/if_ether.h>
0009 #include <linux/ip.h>
0010 
0011 #include <bpf/bpf_helpers.h>
0012 
0013 volatile const __u32 IFINDEX_SRC;
0014 volatile const __u32 IFINDEX_DST;
0015 
0016 static const __u8 src_mac[] = {0x00, 0x11, 0x22, 0x33, 0x44, 0x55};
0017 static const __u8 dst_mac[] = {0x00, 0x22, 0x33, 0x44, 0x55, 0x66};
0018 
0019 SEC("tc")
0020 int tc_chk(struct __sk_buff *skb)
0021 {
0022     return TC_ACT_SHOT;
0023 }
0024 
0025 SEC("tc")
0026 int tc_dst(struct __sk_buff *skb)
0027 {
0028     return bpf_redirect_peer(IFINDEX_SRC, 0);
0029 }
0030 
0031 SEC("tc")
0032 int tc_src(struct __sk_buff *skb)
0033 {
0034     return bpf_redirect_peer(IFINDEX_DST, 0);
0035 }
0036 
0037 SEC("tc")
0038 int tc_dst_l3(struct __sk_buff *skb)
0039 {
0040     return bpf_redirect(IFINDEX_SRC, 0);
0041 }
0042 
0043 SEC("tc")
0044 int tc_src_l3(struct __sk_buff *skb)
0045 {
0046     __u16 proto = skb->protocol;
0047 
0048     if (bpf_skb_change_head(skb, ETH_HLEN, 0) != 0)
0049         return TC_ACT_SHOT;
0050 
0051     if (bpf_skb_store_bytes(skb, 0, &src_mac, ETH_ALEN, 0) != 0)
0052         return TC_ACT_SHOT;
0053 
0054     if (bpf_skb_store_bytes(skb, ETH_ALEN, &dst_mac, ETH_ALEN, 0) != 0)
0055         return TC_ACT_SHOT;
0056 
0057     if (bpf_skb_store_bytes(skb, ETH_ALEN + ETH_ALEN, &proto, sizeof(__u16), 0) != 0)
0058         return TC_ACT_SHOT;
0059 
0060     return bpf_redirect_peer(IFINDEX_DST, 0);
0061 }
0062 
0063 char __license[] SEC("license") = "GPL";