2023-07-19 16:08:57 +02:00
|
|
|
// SPDX-License-Identifier: GPL-2.0
|
|
|
|
/* Copyright (c) 2023 Isovalent */
|
|
|
|
#include <stdbool.h>
|
2023-10-24 23:49:04 +02:00
|
|
|
|
2023-07-19 16:08:57 +02:00
|
|
|
#include <linux/bpf.h>
|
2023-10-24 23:49:04 +02:00
|
|
|
#include <linux/if_ether.h>
|
2024-05-24 18:36:19 +02:00
|
|
|
#include <linux/stddef.h>
|
|
|
|
#include <linux/if_packet.h>
|
2023-10-24 23:49:04 +02:00
|
|
|
#include <bpf/bpf_endian.h>
|
2023-07-19 16:08:57 +02:00
|
|
|
#include <bpf/bpf_helpers.h>
|
2024-12-21 00:46:58 +01:00
|
|
|
#include <bpf/bpf_core_read.h>
|
2023-07-19 16:08:57 +02:00
|
|
|
|
|
|
|
char LICENSE[] SEC("license") = "GPL";
|
|
|
|
|
|
|
|
bool seen_tc1;
|
|
|
|
bool seen_tc2;
|
|
|
|
bool seen_tc3;
|
|
|
|
bool seen_tc4;
|
2023-08-14 18:14:10 +02:00
|
|
|
bool seen_tc5;
|
|
|
|
bool seen_tc6;
|
2024-05-24 18:36:19 +02:00
|
|
|
bool seen_tc7;
|
2024-10-04 12:13:35 +02:00
|
|
|
bool seen_tc8;
|
2024-05-24 18:36:19 +02:00
|
|
|
|
|
|
|
bool set_type;
|
|
|
|
|
2023-10-24 23:49:04 +02:00
|
|
|
bool seen_eth;
|
2024-05-24 18:36:19 +02:00
|
|
|
bool seen_host;
|
|
|
|
bool seen_mcast;
|
2023-07-19 16:08:57 +02:00
|
|
|
|
2024-10-04 12:13:35 +02:00
|
|
|
int mark, prio;
|
2024-12-21 00:46:58 +01:00
|
|
|
unsigned short headroom, tailroom;
|
2024-10-04 12:13:35 +02:00
|
|
|
|
2023-07-19 16:08:57 +02:00
|
|
|
SEC("tc/ingress")
|
|
|
|
int tc1(struct __sk_buff *skb)
|
|
|
|
{
|
2023-10-24 23:49:04 +02:00
|
|
|
struct ethhdr eth = {};
|
|
|
|
|
|
|
|
if (skb->protocol != __bpf_constant_htons(ETH_P_IP))
|
|
|
|
goto out;
|
|
|
|
if (bpf_skb_load_bytes(skb, 0, ð, sizeof(eth)))
|
|
|
|
goto out;
|
|
|
|
seen_eth = eth.h_proto == bpf_htons(ETH_P_IP);
|
2024-05-24 18:36:19 +02:00
|
|
|
seen_host = skb->pkt_type == PACKET_HOST;
|
|
|
|
if (seen_host && set_type) {
|
|
|
|
eth.h_dest[0] = 4;
|
|
|
|
if (bpf_skb_store_bytes(skb, 0, ð, sizeof(eth), 0))
|
|
|
|
goto fail;
|
|
|
|
bpf_skb_change_type(skb, PACKET_MULTICAST);
|
|
|
|
}
|
2023-10-24 23:49:04 +02:00
|
|
|
out:
|
2023-07-19 16:08:57 +02:00
|
|
|
seen_tc1 = true;
|
2024-05-24 18:36:19 +02:00
|
|
|
fail:
|
2023-07-19 16:08:57 +02:00
|
|
|
return TCX_NEXT;
|
|
|
|
}
|
|
|
|
|
|
|
|
SEC("tc/egress")
|
|
|
|
int tc2(struct __sk_buff *skb)
|
|
|
|
{
|
|
|
|
seen_tc2 = true;
|
|
|
|
return TCX_NEXT;
|
|
|
|
}
|
|
|
|
|
|
|
|
SEC("tc/egress")
|
|
|
|
int tc3(struct __sk_buff *skb)
|
|
|
|
{
|
|
|
|
seen_tc3 = true;
|
|
|
|
return TCX_NEXT;
|
|
|
|
}
|
|
|
|
|
|
|
|
SEC("tc/egress")
|
|
|
|
int tc4(struct __sk_buff *skb)
|
|
|
|
{
|
|
|
|
seen_tc4 = true;
|
|
|
|
return TCX_NEXT;
|
|
|
|
}
|
2023-08-14 18:14:10 +02:00
|
|
|
|
|
|
|
SEC("tc/egress")
|
|
|
|
int tc5(struct __sk_buff *skb)
|
|
|
|
{
|
|
|
|
seen_tc5 = true;
|
|
|
|
return TCX_PASS;
|
|
|
|
}
|
|
|
|
|
|
|
|
SEC("tc/egress")
|
|
|
|
int tc6(struct __sk_buff *skb)
|
|
|
|
{
|
|
|
|
seen_tc6 = true;
|
|
|
|
return TCX_PASS;
|
|
|
|
}
|
2024-05-24 18:36:19 +02:00
|
|
|
|
|
|
|
SEC("tc/ingress")
|
|
|
|
int tc7(struct __sk_buff *skb)
|
|
|
|
{
|
|
|
|
struct ethhdr eth = {};
|
|
|
|
|
|
|
|
if (skb->protocol != __bpf_constant_htons(ETH_P_IP))
|
|
|
|
goto out;
|
|
|
|
if (bpf_skb_load_bytes(skb, 0, ð, sizeof(eth)))
|
|
|
|
goto out;
|
|
|
|
if (eth.h_dest[0] == 4 && set_type) {
|
|
|
|
seen_mcast = skb->pkt_type == PACKET_MULTICAST;
|
|
|
|
bpf_skb_change_type(skb, PACKET_HOST);
|
|
|
|
}
|
|
|
|
out:
|
|
|
|
seen_tc7 = true;
|
|
|
|
return TCX_PASS;
|
|
|
|
}
|
2024-10-04 12:13:35 +02:00
|
|
|
|
2024-12-21 00:46:58 +01:00
|
|
|
struct sk_buff {
|
|
|
|
struct net_device *dev;
|
|
|
|
};
|
|
|
|
|
|
|
|
struct net_device {
|
|
|
|
unsigned short needed_headroom;
|
|
|
|
unsigned short needed_tailroom;
|
|
|
|
};
|
|
|
|
|
2024-10-04 12:13:35 +02:00
|
|
|
SEC("tc/egress")
|
|
|
|
int tc8(struct __sk_buff *skb)
|
|
|
|
{
|
2024-12-21 00:46:58 +01:00
|
|
|
struct net_device *dev = BPF_CORE_READ((struct sk_buff *)skb, dev);
|
|
|
|
|
2024-10-04 12:13:35 +02:00
|
|
|
seen_tc8 = true;
|
|
|
|
mark = skb->mark;
|
|
|
|
prio = skb->priority;
|
2024-12-21 00:46:58 +01:00
|
|
|
headroom = BPF_CORE_READ(dev, needed_headroom);
|
|
|
|
tailroom = BPF_CORE_READ(dev, needed_tailroom);
|
2024-10-04 12:13:35 +02:00
|
|
|
return TCX_PASS;
|
|
|
|
}
|