|
| 1 | +// SPDX-License-Identifier: GPL-2.0 |
| 2 | + |
| 3 | +#include "vmlinux.h" |
| 4 | + |
| 5 | +#include <bpf/bpf_helpers.h> |
| 6 | +#include <bpf/bpf_tracing.h> |
| 7 | +#include <bpf/bpf_endian.h> |
| 8 | + |
| 9 | +#define IP_MF 0x2000 |
| 10 | +#define IP_OFFSET 0x1FFF |
| 11 | +#define ETH_HLEN 14 |
| 12 | +#define ETH_P_IP 0x0800 |
| 13 | + |
| 14 | +#define TCP_OPTION_CODE 253 |
| 15 | +#define TCP_OPTION_MAGIC 0xEB9F |
| 16 | + |
| 17 | +char _license[] SEC("license") = "GPL"; |
| 18 | + |
| 19 | +const volatile __u32 targ_ip = 0; |
| 20 | +const volatile __u32 data_such_as_trace_id = 0; |
| 21 | + |
| 22 | +struct __attribute__((packed)) tcp_option |
| 23 | +{ |
| 24 | + u8 kind; |
| 25 | + u8 length; |
| 26 | + u16 magic; |
| 27 | + u32 data; |
| 28 | +}; |
| 29 | + |
| 30 | +static void reserve_space_for_tcp_option(struct bpf_sock_ops *skops) |
| 31 | +{ |
| 32 | + u32 need_space = skops->skb_len + sizeof(struct tcp_option); |
| 33 | + if (need_space > skops->mss_cache) |
| 34 | + return; |
| 35 | + |
| 36 | + bpf_printk("Sufficient space available to store a TCP option, total space: %u, required space: %u", skops->mss_cache, need_space); |
| 37 | + bpf_reserve_hdr_opt(skops, sizeof(struct tcp_option), 0); |
| 38 | +} |
| 39 | + |
| 40 | +static inline void store_tcp_option_header(struct bpf_sock_ops *skops) |
| 41 | +{ |
| 42 | + struct tcp_option tcp_option; |
| 43 | + struct tcphdr *th = skops->skb_data; |
| 44 | + |
| 45 | + if (skops->skb_len + sizeof(struct tcp_option) > skops->mss_cache) |
| 46 | + return; |
| 47 | + |
| 48 | + tcp_option.kind = TCP_OPTION_CODE; |
| 49 | + tcp_option.length = sizeof(struct tcp_option); |
| 50 | + tcp_option.magic = bpf_htons(TCP_OPTION_MAGIC); |
| 51 | + tcp_option.data = data_such_as_trace_id; |
| 52 | + bpf_store_hdr_opt(skops, &tcp_option, sizeof(tcp_option), 0); |
| 53 | + bpf_printk("Stored a TCP option in TCP Flag: %u", skops->skb_tcp_flags); |
| 54 | +} |
| 55 | + |
| 56 | +SEC("sockops") |
| 57 | +int sockops_write_tcp_options(struct bpf_sock_ops *skops) |
| 58 | +{ |
| 59 | + u32 l_ip, r_ip; |
| 60 | + l_ip = skops->local_ip4; |
| 61 | + r_ip = skops->remote_ip4; |
| 62 | + |
| 63 | + // Check if the IP addresses match the target IP |
| 64 | + if (r_ip != targ_ip && l_ip != targ_ip) { |
| 65 | + return 1; |
| 66 | + } |
| 67 | + switch (skops->op) |
| 68 | + { |
| 69 | + // When creating a connection to another host |
| 70 | + case BPF_SOCK_OPS_TCP_CONNECT_CB: |
| 71 | + bpf_sock_ops_cb_flags_set(skops, skops->bpf_sock_ops_cb_flags | BPF_SOCK_OPS_WRITE_HDR_OPT_CB_FLAG); |
| 72 | + break; |
| 73 | + // When accepting a connection from another host |
| 74 | + case BPF_SOCK_OPS_ACTIVE_ESTABLISHED_CB: |
| 75 | + bpf_sock_ops_cb_flags_set(skops, skops->bpf_sock_ops_cb_flags | BPF_SOCK_OPS_WRITE_HDR_OPT_CB_FLAG); |
| 76 | + break; |
| 77 | + // When the socket is established |
| 78 | + case BPF_SOCK_OPS_PASSIVE_ESTABLISHED_CB: |
| 79 | + bpf_sock_ops_cb_flags_set(skops, skops->bpf_sock_ops_cb_flags | BPF_SOCK_OPS_WRITE_HDR_OPT_CB_FLAG); |
| 80 | + break; |
| 81 | + // When reserving space for TCP options header |
| 82 | + case BPF_SOCK_OPS_HDR_OPT_LEN_CB: |
| 83 | + reserve_space_for_tcp_option(skops); |
| 84 | + break; |
| 85 | + // When writing TCP options header |
| 86 | + case BPF_SOCK_OPS_WRITE_HDR_OPT_CB: |
| 87 | + store_tcp_option_header(skops); |
| 88 | + break; |
| 89 | + } |
| 90 | + return 1; |
| 91 | +} |
| 92 | + |
| 93 | +struct __tcphdr |
| 94 | +{ |
| 95 | + __be16 source; |
| 96 | + __be16 dest; |
| 97 | + __be32 seq; |
| 98 | + __be32 ack_seq; |
| 99 | + __u16 res1 : 4, doff : 4, fin : 1, syn : 1, rst : 1, psh : 1, ack : 1, urg : 1, ece : 1, cwr : 1; |
| 100 | + __be16 window; |
| 101 | + __sum16 check; |
| 102 | + __be16 urg_ptr; |
| 103 | +}; |
| 104 | + |
| 105 | +static inline int ip_is_fragment(struct __sk_buff *skb, __u32 nhoff) |
| 106 | +{ |
| 107 | + __u16 frag_off; |
| 108 | + |
| 109 | + bpf_skb_load_bytes(skb, nhoff + offsetof(struct iphdr, frag_off), &frag_off, 2); |
| 110 | + frag_off = __bpf_ntohs(frag_off); |
| 111 | + return frag_off & (IP_MF | IP_OFFSET); |
| 112 | +} |
| 113 | + |
| 114 | +SEC("socket") |
| 115 | +int socket_handler(struct __sk_buff *skb) { |
| 116 | + u16 proto; |
| 117 | + u32 nhoff = ETH_HLEN; |
| 118 | + u8 hdr_len; |
| 119 | + u32 tcp_hdr_start = 0; |
| 120 | + u32 ip_proto = 0; |
| 121 | + u32 l_ip, r_ip; |
| 122 | + bpf_skb_load_bytes(skb, 12, &proto, 2); |
| 123 | + proto = __bpf_ntohs(proto); |
| 124 | + if (proto != ETH_P_IP) |
| 125 | + return 0; |
| 126 | + |
| 127 | + if (ip_is_fragment(skb, nhoff)) |
| 128 | + return 0; |
| 129 | + |
| 130 | + bpf_skb_load_bytes(skb, ETH_HLEN, &hdr_len, sizeof(hdr_len)); |
| 131 | + hdr_len &= 0x0f; |
| 132 | + hdr_len *= 4; |
| 133 | + |
| 134 | + |
| 135 | + bpf_skb_load_bytes(skb, nhoff + offsetof(struct iphdr, protocol), &ip_proto, 1); |
| 136 | + |
| 137 | + if (ip_proto != IPPROTO_TCP) |
| 138 | + { |
| 139 | + return 0; |
| 140 | + } |
| 141 | + |
| 142 | + bpf_skb_load_bytes(skb, nhoff + offsetof(struct iphdr, saddr), &l_ip, 4); |
| 143 | + bpf_skb_load_bytes(skb, nhoff + offsetof(struct iphdr, daddr), &r_ip, 4); |
| 144 | + |
| 145 | + if (r_ip == targ_ip || l_ip == targ_ip) { |
| 146 | + |
| 147 | + tcp_hdr_start = nhoff + hdr_len; |
| 148 | + u8 tcp_flag; |
| 149 | + bpf_skb_load_bytes(skb, tcp_hdr_start + offsetof(struct __tcphdr, ack_seq) + 5, &tcp_flag, sizeof(tcp_flag)); |
| 150 | + |
| 151 | + u16 tcp_data_offset; |
| 152 | + bpf_skb_load_bytes(skb, tcp_hdr_start + offsetof(struct __tcphdr, ack_seq) + 4, &tcp_data_offset, sizeof(tcp_data_offset)); |
| 153 | + |
| 154 | + tcp_data_offset = __bpf_ntohs(tcp_data_offset) >> 12; |
| 155 | + tcp_data_offset *= 4; |
| 156 | + |
| 157 | + u32 option_start = tcp_hdr_start + 20; |
| 158 | + u32 option_end = tcp_hdr_start + tcp_data_offset; |
| 159 | + int i = 0; |
| 160 | + for (i = 0; i < 10; i++) { |
| 161 | + u16 option_hdr; |
| 162 | + bpf_skb_load_bytes(skb, option_start, &option_hdr, sizeof(option_hdr)); |
| 163 | + u8 length = option_hdr>>8; |
| 164 | + u8 kind = option_hdr & 0xff; |
| 165 | + |
| 166 | + if (kind == 1) { |
| 167 | + option_start += 1; |
| 168 | + goto END; |
| 169 | + } |
| 170 | + |
| 171 | + if (kind == TCP_OPTION_CODE) { |
| 172 | + u16 magic; |
| 173 | + u32 data; |
| 174 | + |
| 175 | + // Load magic number from TCP option header |
| 176 | + bpf_skb_load_bytes(skb, option_start + 2, &magic, sizeof(magic)); |
| 177 | + magic = __bpf_ntohs(magic); |
| 178 | + bpf_printk("####=> Socket TCP option magic: 0x%x", magic); |
| 179 | + |
| 180 | + if (magic == TCP_OPTION_MAGIC) { |
| 181 | + // Load data from TCP option header |
| 182 | + bpf_skb_load_bytes(skb, option_start + 4, &data, sizeof(data)); |
| 183 | + bpf_printk("####=> Socket TCP option data: %u", data); |
| 184 | + } |
| 185 | + } |
| 186 | + |
| 187 | + option_start += length; |
| 188 | + END: |
| 189 | + if (option_start >= option_end) { |
| 190 | + break; |
| 191 | + } |
| 192 | + } |
| 193 | + |
| 194 | + } |
| 195 | + return skb->len; |
| 196 | +} |
0 commit comments