1 // SPDX-License-Identifier: GPL-2.0
2 #include <stdint.h>
3 #include <stdbool.h>
4 
5 #include <linux/bpf.h>
6 #include <linux/stddef.h>
7 #include <linux/pkt_cls.h>
8 
9 #include <bpf/bpf_helpers.h>
10 
11 enum {
12 	dev_src,
13 	dev_dst,
14 };
15 
16 struct bpf_map_def SEC("maps") ifindex_map = {
17 	.type		= BPF_MAP_TYPE_ARRAY,
18 	.key_size	= sizeof(int),
19 	.value_size	= sizeof(int),
20 	.max_entries	= 2,
21 };
22 
23 static __always_inline int get_dev_ifindex(int which)
24 {
25 	int *ifindex = bpf_map_lookup_elem(&ifindex_map, &which);
26 
27 	return ifindex ? *ifindex : 0;
28 }
29 
30 SEC("chk_egress") int tc_chk(struct __sk_buff *skb)
31 {
32 	return TC_ACT_SHOT;
33 }
34 
35 SEC("dst_ingress") int tc_dst(struct __sk_buff *skb)
36 {
37 	return bpf_redirect_peer(get_dev_ifindex(dev_src), 0);
38 }
39 
40 SEC("src_ingress") int tc_src(struct __sk_buff *skb)
41 {
42 	return bpf_redirect_peer(get_dev_ifindex(dev_dst), 0);
43 }
44 
45 char __license[] SEC("license") = "GPL";
46