|
1 | 1 | #include <vmlinux.h> |
2 | 2 | #include <bpf/bpf_tracing.h> |
| 3 | +#include <bpf/bpf_endian.h> |
3 | 4 | #include "maps.bpf.h" |
4 | 5 |
|
| 6 | +#define ETH_P_IPV6 0x86DD |
| 7 | +#define ETH_P_IP 0x0800 |
| 8 | + |
| 9 | +struct kfree_skb_key_t { |
| 10 | + u16 eth_proto; |
| 11 | + u16 ip_proto; |
| 12 | + u16 port; |
| 13 | + u16 reason; |
| 14 | +}; |
| 15 | + |
5 | 16 | struct { |
6 | 17 | __uint(type, BPF_MAP_TYPE_HASH); |
7 | | - __uint(max_entries, 1024); |
8 | | - __type(key, u16); |
| 18 | + __uint(max_entries, 10240); |
| 19 | + __type(key, struct kfree_skb_key_t); |
9 | 20 | __type(value, u64); |
10 | 21 | } kfree_skb_total SEC(".maps"); |
11 | 22 |
|
12 | | -SEC("tp_btf/kfree_skb") |
13 | | -int BPF_PROG(kfree_skb, struct sk_buff *skb, void *location, enum skb_drop_reason reason) |
| 23 | +SEC("tp_btf/kfree_skb") int BPF_PROG(kfree_skb, struct sk_buff *skb, void *location, enum skb_drop_reason reason) |
14 | 24 | { |
15 | | - u16 key = reason; |
| 25 | + struct kfree_skb_key_t key; |
| 26 | + struct ethhdr eth_hdr; |
| 27 | + struct iphdr ip_hdr; |
| 28 | + struct ipv6hdr ipv6_hdr; |
| 29 | + struct tcphdr tcp_hdr; |
| 30 | + struct udphdr udp_hdr; |
| 31 | + u16 ip_proto = 0; |
| 32 | + |
| 33 | + // Same as skb_mac_header_was_set: |
| 34 | + // * https://elixir.bootlin.com/linux/v6.5-rc1/source/include/linux/skbuff.h#L2899 |
| 35 | + if (skb->mac_header == (typeof(skb->mac_header)) ~0U) { |
| 36 | + return 0; |
| 37 | + } |
| 38 | + |
| 39 | + if (bpf_probe_read_kernel(ð_hdr, sizeof(eth_hdr), skb->head + skb->mac_header)) { |
| 40 | + return 0; |
| 41 | + } |
| 42 | + |
| 43 | + key.eth_proto = bpf_ntohs(eth_hdr.h_proto); |
| 44 | + |
| 45 | + if (!key.eth_proto && !bpf_ntohs(skb->protocol)) { |
| 46 | + return 0; |
| 47 | + } |
| 48 | + |
| 49 | + switch (key.eth_proto) { |
| 50 | + case ETH_P_IP: |
| 51 | + if (bpf_probe_read_kernel(&ip_hdr, sizeof(ip_hdr), skb->head + skb->network_header) < 0) { |
| 52 | + return 0; |
| 53 | + } |
| 54 | + ip_proto = ip_hdr.protocol; |
| 55 | + break; |
| 56 | + case ETH_P_IPV6: |
| 57 | + if (bpf_probe_read_kernel(&ipv6_hdr, sizeof(ipv6_hdr), skb->head + skb->network_header) < 0) { |
| 58 | + return 0; |
| 59 | + } |
| 60 | + ip_proto = ipv6_hdr.nexthdr; |
| 61 | + break; |
| 62 | + } |
| 63 | + |
| 64 | + key.ip_proto = ip_proto; |
| 65 | + |
| 66 | + // Same as skb_transport_header_was_set: |
| 67 | + // * https://elixir.bootlin.com/linux/v6.5-rc1/source/include/linux/skbuff.h#L2860 |
| 68 | + if (skb->transport_header == (typeof(skb->transport_header)) ~0U) { |
| 69 | + return 0; |
| 70 | + } |
| 71 | + |
| 72 | + // Using key.ip_proto directly is not allowed for some reason: |
| 73 | + // |
| 74 | + // ; switch (key.ip_proto) { |
| 75 | + // 48: (54) w1 &= 65535 |
| 76 | + // R1 32-bit pointer arithmetic prohibited |
| 77 | + switch (ip_proto) { |
| 78 | + case IPPROTO_TCP: |
| 79 | + if (bpf_probe_read_kernel(&tcp_hdr, sizeof(tcp_hdr), skb->head + skb->transport_header) < 0) { |
| 80 | + return 0; |
| 81 | + } |
| 82 | + key.port = bpf_ntohs(tcp_hdr.dest); |
| 83 | + break; |
| 84 | + case IPPROTO_UDP: |
| 85 | + if (bpf_probe_read_kernel(&udp_hdr, sizeof(udp_hdr), skb->head + skb->transport_header) < 0) { |
| 86 | + return 0; |
| 87 | + } |
| 88 | + key.port = bpf_ntohs(udp_hdr.dest); |
| 89 | + break; |
| 90 | + } |
| 91 | + |
| 92 | + key.reason = reason; |
| 93 | + |
16 | 94 | increment_map(&kfree_skb_total, &key, 1); |
| 95 | + |
17 | 96 | return 0; |
18 | 97 | } |
19 | 98 |
|
|
0 commit comments