Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@slaise

slaise/tcp.c Secret

Created January 7, 2023 09:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save slaise/6022245cdcc09a13af34ac90190ab9cb to your computer and use it in GitHub Desktop.
Save slaise/6022245cdcc09a13af34ac90190ab9cb to your computer and use it in GitHub Desktop.
caretta notes
int handle_tcp_data_queue(struct pt_regs *ctx) {
// first argument to tcp_data_queue is a struct sock*
struct sock *sock = (struct sock *)PT_REGS_PARM1_CORE(ctx);
struct connection_identifier conn_id = {};
struct connection_throughput_stats throughput = {};
if (parse_sock_data(sock, &conn_id.tuple, &throughput) == BPF_ERROR) {
debug_print("error parsing sock");
return BPF_ERROR;
}
//..
// fill the conn_id extra details from sock_info map entry, or create one
struct sock_info *sock_info = bpf_map_lookup_elem(&sock_infos, &sock);
if (sock_info == NULL) {
// first time we encounter this sock
// check if server or client and insert to the maps
enum connection_role role = get_sock_role(sock);
struct sock_info info = {
.pid = 0, // can't associate to pid anyway
.role = role,
.is_active = true,
.id = get_unique_id(),
};
bpf_map_update_elem(&sock_infos, &sock, &info, BPF_ANY);
conn_id.pid = info.pid;
conn_id.id = info.id;
conn_id.role = info.role;
throughput.is_active = true;
bpf_map_update_elem(&connections, &conn_id, &throughput, BPF_ANY);
return BPF_SUCCESS;
}
// filling the existing sock item
conn_id.pid = sock_info->pid;
conn_id.id = sock_info->id;
conn_id.role = sock_info->role;
if (!sock_info->is_active) {
debug_print("inactive sock in tcp_data_queue");
return -1;
}
throughput.is_active = sock_info->is_active;
bpf_map_update_elem(&connections, &conn_id, &throughput, BPF_ANY);
return BPF_SUCCESS;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment