1 // SPDX-License-Identifier: GPL-2.0
2 /* Copyright (c) 2023 Isovalent */
3 #include <stdbool.h>
4 
5 #include <linux/bpf.h>
6 #include <linux/if_ether.h>
7 #include <linux/stddef.h>
8 #include <linux/if_packet.h>
9 #include <bpf/bpf_endian.h>
10 #include <bpf/bpf_helpers.h>
11 #include <bpf/bpf_core_read.h>
12 
13 char LICENSE[] SEC("license") = "GPL";
14 
15 bool seen_tc1;
16 bool seen_tc2;
17 bool seen_tc3;
18 bool seen_tc4;
19 bool seen_tc5;
20 bool seen_tc6;
21 bool seen_tc7;
22 bool seen_tc8;
23 
24 bool set_type;
25 
26 bool seen_eth;
27 bool seen_host;
28 bool seen_mcast;
29 
30 int mark, prio;
31 unsigned short headroom, tailroom;
32 
33 SEC("tc/ingress")
tc1(struct __sk_buff * skb)34 int tc1(struct __sk_buff *skb)
35 {
36 	struct ethhdr eth = {};
37 
38 	if (skb->protocol != __bpf_constant_htons(ETH_P_IP))
39 		goto out;
40 	if (bpf_skb_load_bytes(skb, 0, &eth, sizeof(eth)))
41 		goto out;
42 	seen_eth = eth.h_proto == bpf_htons(ETH_P_IP);
43 	seen_host = skb->pkt_type == PACKET_HOST;
44 	if (seen_host && set_type) {
45 		eth.h_dest[0] = 4;
46 		if (bpf_skb_store_bytes(skb, 0, &eth, sizeof(eth), 0))
47 			goto fail;
48 		bpf_skb_change_type(skb, PACKET_MULTICAST);
49 	}
50 out:
51 	seen_tc1 = true;
52 fail:
53 	return TCX_NEXT;
54 }
55 
56 SEC("tc/egress")
tc2(struct __sk_buff * skb)57 int tc2(struct __sk_buff *skb)
58 {
59 	seen_tc2 = true;
60 	return TCX_NEXT;
61 }
62 
63 SEC("tc/egress")
tc3(struct __sk_buff * skb)64 int tc3(struct __sk_buff *skb)
65 {
66 	seen_tc3 = true;
67 	return TCX_NEXT;
68 }
69 
70 SEC("tc/egress")
tc4(struct __sk_buff * skb)71 int tc4(struct __sk_buff *skb)
72 {
73 	seen_tc4 = true;
74 	return TCX_NEXT;
75 }
76 
77 SEC("tc/egress")
tc5(struct __sk_buff * skb)78 int tc5(struct __sk_buff *skb)
79 {
80 	seen_tc5 = true;
81 	return TCX_PASS;
82 }
83 
84 SEC("tc/egress")
tc6(struct __sk_buff * skb)85 int tc6(struct __sk_buff *skb)
86 {
87 	seen_tc6 = true;
88 	return TCX_PASS;
89 }
90 
91 SEC("tc/ingress")
tc7(struct __sk_buff * skb)92 int tc7(struct __sk_buff *skb)
93 {
94 	struct ethhdr eth = {};
95 
96 	if (skb->protocol != __bpf_constant_htons(ETH_P_IP))
97 		goto out;
98 	if (bpf_skb_load_bytes(skb, 0, &eth, sizeof(eth)))
99 		goto out;
100 	if (eth.h_dest[0] == 4 && set_type) {
101 		seen_mcast = skb->pkt_type == PACKET_MULTICAST;
102 		bpf_skb_change_type(skb, PACKET_HOST);
103 	}
104 out:
105 	seen_tc7 = true;
106 	return TCX_PASS;
107 }
108 
109 struct sk_buff {
110 	struct net_device *dev;
111 };
112 
113 struct net_device {
114 	unsigned short needed_headroom;
115 	unsigned short needed_tailroom;
116 };
117 
118 SEC("tc/egress")
tc8(struct __sk_buff * skb)119 int tc8(struct __sk_buff *skb)
120 {
121 	struct net_device *dev = BPF_CORE_READ((struct sk_buff *)skb, dev);
122 
123 	seen_tc8 = true;
124 	mark = skb->mark;
125 	prio = skb->priority;
126 	headroom = BPF_CORE_READ(dev, needed_headroom);
127 	tailroom = BPF_CORE_READ(dev, needed_tailroom);
128 	return TCX_PASS;
129 }
130