xref: /linux/tools/testing/selftests/bpf/progs/test_tc_peer.c (revision 03ab8e6297acd1bc0eedaa050e2a1635c576fd11)
19f4c53caSDaniel Borkmann // SPDX-License-Identifier: GPL-2.0
29f4c53caSDaniel Borkmann #include <stdint.h>
39f4c53caSDaniel Borkmann #include <stdbool.h>
49f4c53caSDaniel Borkmann 
59f4c53caSDaniel Borkmann #include <linux/bpf.h>
69f4c53caSDaniel Borkmann #include <linux/stddef.h>
79f4c53caSDaniel Borkmann #include <linux/pkt_cls.h>
86fd5fb63SJussi Maki #include <linux/if_ether.h>
96fd5fb63SJussi Maki #include <linux/ip.h>
109f4c53caSDaniel Borkmann 
119f4c53caSDaniel Borkmann #include <bpf/bpf_helpers.h>
129f4c53caSDaniel Borkmann 
138f1634b8SStanislav Fomichev volatile const __u32 IFINDEX_SRC;
148f1634b8SStanislav Fomichev volatile const __u32 IFINDEX_DST;
159f4c53caSDaniel Borkmann 
166fd5fb63SJussi Maki static const __u8 src_mac[] = {0x00, 0x11, 0x22, 0x33, 0x44, 0x55};
176fd5fb63SJussi Maki static const __u8 dst_mac[] = {0x00, 0x22, 0x33, 0x44, 0x55, 0x66};
186fd5fb63SJussi Maki 
19*c22bdd28SAndrii Nakryiko SEC("tc")
tc_chk(struct __sk_buff * skb)20096eccdeSJussi Maki int tc_chk(struct __sk_buff *skb)
219f4c53caSDaniel Borkmann {
229f4c53caSDaniel Borkmann 	return TC_ACT_SHOT;
239f4c53caSDaniel Borkmann }
249f4c53caSDaniel Borkmann 
25*c22bdd28SAndrii Nakryiko SEC("tc")
tc_dst(struct __sk_buff * skb)26096eccdeSJussi Maki int tc_dst(struct __sk_buff *skb)
279f4c53caSDaniel Borkmann {
28096eccdeSJussi Maki 	return bpf_redirect_peer(IFINDEX_SRC, 0);
299f4c53caSDaniel Borkmann }
309f4c53caSDaniel Borkmann 
31*c22bdd28SAndrii Nakryiko SEC("tc")
tc_src(struct __sk_buff * skb)32096eccdeSJussi Maki int tc_src(struct __sk_buff *skb)
339f4c53caSDaniel Borkmann {
34096eccdeSJussi Maki 	return bpf_redirect_peer(IFINDEX_DST, 0);
359f4c53caSDaniel Borkmann }
369f4c53caSDaniel Borkmann 
37*c22bdd28SAndrii Nakryiko SEC("tc")
tc_dst_l3(struct __sk_buff * skb)386fd5fb63SJussi Maki int tc_dst_l3(struct __sk_buff *skb)
396fd5fb63SJussi Maki {
406fd5fb63SJussi Maki 	return bpf_redirect(IFINDEX_SRC, 0);
416fd5fb63SJussi Maki }
426fd5fb63SJussi Maki 
43*c22bdd28SAndrii Nakryiko SEC("tc")
tc_src_l3(struct __sk_buff * skb)446fd5fb63SJussi Maki int tc_src_l3(struct __sk_buff *skb)
456fd5fb63SJussi Maki {
466fd5fb63SJussi Maki 	__u16 proto = skb->protocol;
476fd5fb63SJussi Maki 
486fd5fb63SJussi Maki 	if (bpf_skb_change_head(skb, ETH_HLEN, 0) != 0)
496fd5fb63SJussi Maki 		return TC_ACT_SHOT;
506fd5fb63SJussi Maki 
516fd5fb63SJussi Maki 	if (bpf_skb_store_bytes(skb, 0, &src_mac, ETH_ALEN, 0) != 0)
526fd5fb63SJussi Maki 		return TC_ACT_SHOT;
536fd5fb63SJussi Maki 
546fd5fb63SJussi Maki 	if (bpf_skb_store_bytes(skb, ETH_ALEN, &dst_mac, ETH_ALEN, 0) != 0)
556fd5fb63SJussi Maki 		return TC_ACT_SHOT;
566fd5fb63SJussi Maki 
576fd5fb63SJussi Maki 	if (bpf_skb_store_bytes(skb, ETH_ALEN + ETH_ALEN, &proto, sizeof(__u16), 0) != 0)
586fd5fb63SJussi Maki 		return TC_ACT_SHOT;
596fd5fb63SJussi Maki 
606fd5fb63SJussi Maki 	return bpf_redirect_peer(IFINDEX_DST, 0);
616fd5fb63SJussi Maki }
626fd5fb63SJussi Maki 
639f4c53caSDaniel Borkmann char __license[] SEC("license") = "GPL";
64