143a7c3efSYan Zhai // SPDX-License-Identifier: GPL-2.0
243a7c3efSYan Zhai #include <linux/bpf.h>
343a7c3efSYan Zhai #include <bpf/bpf_endian.h>
443a7c3efSYan Zhai #include <bpf/bpf_helpers.h>
5*0a55264cSYonghong Song #include <linux/ip.h>
643a7c3efSYan Zhai #include "bpf_tracing_net.h"
743a7c3efSYan Zhai 
843a7c3efSYan Zhai /* We don't care about whether the packet can be received by network stack.
943a7c3efSYan Zhai  * Just care if the packet is sent to the correct device at correct direction
1043a7c3efSYan Zhai  * and not panic the kernel.
1143a7c3efSYan Zhai  */
prepend_dummy_mac(struct __sk_buff * skb)1243a7c3efSYan Zhai static int prepend_dummy_mac(struct __sk_buff *skb)
1343a7c3efSYan Zhai {
1443a7c3efSYan Zhai 	char mac[] = {0x1, 0x2, 0x3, 0x4, 0x5, 0x6, 0xf,
1543a7c3efSYan Zhai 		      0xe, 0xd, 0xc, 0xb, 0xa, 0x08, 0x00};
1643a7c3efSYan Zhai 
1743a7c3efSYan Zhai 	if (bpf_skb_change_head(skb, ETH_HLEN, 0))
1843a7c3efSYan Zhai 		return -1;
1943a7c3efSYan Zhai 
2043a7c3efSYan Zhai 	if (bpf_skb_store_bytes(skb, 0, mac, sizeof(mac), 0))
2143a7c3efSYan Zhai 		return -1;
2243a7c3efSYan Zhai 
2343a7c3efSYan Zhai 	return 0;
2443a7c3efSYan Zhai }
2543a7c3efSYan Zhai 
2643a7c3efSYan Zhai /* Use the last byte of IP address to redirect the packet */
get_redirect_target(struct __sk_buff * skb)2743a7c3efSYan Zhai static int get_redirect_target(struct __sk_buff *skb)
2843a7c3efSYan Zhai {
2943a7c3efSYan Zhai 	struct iphdr *iph = NULL;
3043a7c3efSYan Zhai 	void *start = (void *)(long)skb->data;
3143a7c3efSYan Zhai 	void *end = (void *)(long)skb->data_end;
3243a7c3efSYan Zhai 
3343a7c3efSYan Zhai 	if (start + sizeof(*iph) > end)
3443a7c3efSYan Zhai 		return -1;
3543a7c3efSYan Zhai 
3643a7c3efSYan Zhai 	iph = (struct iphdr *)start;
3743a7c3efSYan Zhai 	return bpf_ntohl(iph->daddr) & 0xff;
3843a7c3efSYan Zhai }
3943a7c3efSYan Zhai 
4043a7c3efSYan Zhai SEC("redir_ingress")
test_lwt_redirect_in(struct __sk_buff * skb)4143a7c3efSYan Zhai int test_lwt_redirect_in(struct __sk_buff *skb)
4243a7c3efSYan Zhai {
4343a7c3efSYan Zhai 	int target = get_redirect_target(skb);
4443a7c3efSYan Zhai 
4543a7c3efSYan Zhai 	if (target < 0)
4643a7c3efSYan Zhai 		return BPF_OK;
4743a7c3efSYan Zhai 
4843a7c3efSYan Zhai 	if (prepend_dummy_mac(skb))
4943a7c3efSYan Zhai 		return BPF_DROP;
5043a7c3efSYan Zhai 
5143a7c3efSYan Zhai 	return bpf_redirect(target, BPF_F_INGRESS);
5243a7c3efSYan Zhai }
5343a7c3efSYan Zhai 
5443a7c3efSYan Zhai SEC("redir_egress")
test_lwt_redirect_out(struct __sk_buff * skb)5543a7c3efSYan Zhai int test_lwt_redirect_out(struct __sk_buff *skb)
5643a7c3efSYan Zhai {
5743a7c3efSYan Zhai 	int target = get_redirect_target(skb);
5843a7c3efSYan Zhai 
5943a7c3efSYan Zhai 	if (target < 0)
6043a7c3efSYan Zhai 		return BPF_OK;
6143a7c3efSYan Zhai 
6243a7c3efSYan Zhai 	if (prepend_dummy_mac(skb))
6343a7c3efSYan Zhai 		return BPF_DROP;
6443a7c3efSYan Zhai 
6543a7c3efSYan Zhai 	return bpf_redirect(target, 0);
6643a7c3efSYan Zhai }
6743a7c3efSYan Zhai 
6843a7c3efSYan Zhai SEC("redir_egress_nomac")
test_lwt_redirect_out_nomac(struct __sk_buff * skb)6943a7c3efSYan Zhai int test_lwt_redirect_out_nomac(struct __sk_buff *skb)
7043a7c3efSYan Zhai {
7143a7c3efSYan Zhai 	int target = get_redirect_target(skb);
7243a7c3efSYan Zhai 
7343a7c3efSYan Zhai 	if (target < 0)
7443a7c3efSYan Zhai 		return BPF_OK;
7543a7c3efSYan Zhai 
7643a7c3efSYan Zhai 	return bpf_redirect(target, 0);
7743a7c3efSYan Zhai }
7843a7c3efSYan Zhai 
7943a7c3efSYan Zhai SEC("redir_ingress_nomac")
test_lwt_redirect_in_nomac(struct __sk_buff * skb)8043a7c3efSYan Zhai int test_lwt_redirect_in_nomac(struct __sk_buff *skb)
8143a7c3efSYan Zhai {
8243a7c3efSYan Zhai 	int target = get_redirect_target(skb);
8343a7c3efSYan Zhai 
8443a7c3efSYan Zhai 	if (target < 0)
8543a7c3efSYan Zhai 		return BPF_OK;
8643a7c3efSYan Zhai 
8743a7c3efSYan Zhai 	return bpf_redirect(target, BPF_F_INGRESS);
8843a7c3efSYan Zhai }
8943a7c3efSYan Zhai 
9043a7c3efSYan Zhai char _license[] SEC("license") = "GPL";
91