-
Notifications
You must be signed in to change notification settings - Fork 49
NETOBSERV-1704 Add kTLS tracker #814
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Draft
jpinsonneau
wants to merge
3
commits into
netobserv:main
Choose a base branch
from
jpinsonneau:ktls_poc
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,169 @@ | ||
| /* | ||
| kTLS tracker | ||
| */ | ||
| #include "utils.h" | ||
|
|
||
| #ifndef __KTLS_TRACKER_H__ | ||
| #define __KTLS_TRACKER_H__ | ||
|
|
||
| #define MAX_SOCK_OPS_MAP_ENTRIES 65535 | ||
| struct sock_key { | ||
| u8 remote_ip[IP_MAX_LEN]; | ||
| u8 local_ip[IP_MAX_LEN]; | ||
| u32 remote_port; | ||
| u32 local_port; | ||
| u32 family; | ||
| }; | ||
|
|
||
| struct { | ||
| __uint(type, BPF_MAP_TYPE_SOCKHASH); | ||
| __uint(max_entries, MAX_SOCK_OPS_MAP_ENTRIES); | ||
| __type(key, struct sock_key); | ||
| __type(value, u64); | ||
| } sock_hash SEC(".maps"); | ||
|
|
||
| static __always_inline void bpf_sock_ops_ip(struct bpf_sock_ops *skops) { | ||
| int ret; | ||
|
|
||
| struct sock_key skk = { | ||
| .local_port = skops->local_port, | ||
| .remote_port = bpf_ntohl(skops->remote_port), | ||
| .family = skops->family, | ||
| }; | ||
|
|
||
| switch (skops->family) { | ||
| case AF_INET: | ||
| __builtin_memcpy(skk.remote_ip, ip4in6, sizeof(ip4in6)); | ||
| __builtin_memcpy(skk.local_ip, ip4in6, sizeof(ip4in6)); | ||
| __builtin_memcpy(skk.remote_ip + sizeof(ip4in6), &skops->remote_ip4, | ||
| sizeof(skops->remote_ip4)); | ||
| __builtin_memcpy(skk.local_ip + sizeof(ip4in6), &skops->local_ip4, | ||
| sizeof(skops->local_ip4)); | ||
| break; | ||
| case AF_INET6: | ||
| return; | ||
| } | ||
|
|
||
| ret = bpf_sock_hash_update(skops, &sock_hash, &skk, BPF_NOEXIST); | ||
| if (ret) { | ||
| bpf_printk("failed to update sock hash op: %d, port %d --> %d\n", skops->op, skk.local_port, | ||
| skk.remote_port); | ||
| return; | ||
| } | ||
| } | ||
|
|
||
| SEC("sockops") | ||
| int bpf_sockops(struct bpf_sock_ops *skops) { | ||
| u32 op = skops->op; | ||
|
|
||
| switch (op) { | ||
| case BPF_SOCK_OPS_PASSIVE_ESTABLISHED_CB: | ||
| case BPF_SOCK_OPS_ACTIVE_ESTABLISHED_CB: | ||
| bpf_sock_ops_ip(skops); | ||
| break; | ||
| default: | ||
| break; | ||
| } | ||
|
|
||
| return 0; | ||
| } | ||
|
|
||
| static __always_inline int find_update_flow(struct sk_msg_md *msg, int verdict) { | ||
| int ret = 0; | ||
| flow_id id; | ||
|
|
||
| __builtin_memset(&id, 0, sizeof(id)); | ||
|
|
||
| id.src_port = msg->sk->src_port; | ||
| id.dst_port = bpf_ntohs(msg->sk->dst_port); | ||
| id.transport_protocol = msg->sk->protocol; | ||
| __builtin_memcpy(id.src_ip, ip4in6, sizeof(ip4in6)); | ||
| __builtin_memcpy(id.dst_ip, ip4in6, sizeof(ip4in6)); | ||
| __builtin_memcpy(id.src_ip + sizeof(ip4in6), &msg->sk->src_ip4, sizeof(msg->sk->src_ip4)); | ||
| __builtin_memcpy(id.dst_ip + sizeof(ip4in6), &msg->sk->dst_ip4, sizeof(msg->sk->dst_ip4)); | ||
|
|
||
| u64 current_ts = bpf_ktime_get_ns(); | ||
| additional_metrics *aggregate_flow = | ||
| (additional_metrics *)bpf_map_lookup_elem(&additional_flow_metrics, &id); | ||
| if (aggregate_flow != NULL) { | ||
| aggregate_flow->end_mono_time_ts = current_ts; | ||
| if (aggregate_flow->start_mono_time_ts == 0) { | ||
| aggregate_flow->start_mono_time_ts = current_ts; | ||
| } | ||
| aggregate_flow->verdict = verdict; | ||
| aggregate_flow->tls_msg.family = (u8)msg->family; | ||
| aggregate_flow->tls_msg.size = msg->size; | ||
| aggregate_flow->tls_msg.local_port = (u16)msg->local_port; | ||
| aggregate_flow->tls_msg.remote_port = (u16)bpf_ntohl(msg->remote_port); | ||
|
|
||
| unsigned char *p = (unsigned char *)(long)msg->data; | ||
| unsigned char *end = (unsigned char *)(long)msg->data_end; | ||
| if (p + 5 <= end) { | ||
| aggregate_flow->tls_msg.tls_content_type = p[0]; | ||
| unsigned char *q = p + 5; | ||
| if (aggregate_flow->tls_msg.tls_content_type == 22 && q + 1 <= end) { | ||
| aggregate_flow->tls_msg.tls_handshake_type = q[0]; | ||
| } else if (aggregate_flow->tls_msg.tls_content_type == 21 && q + 2 <= end) { | ||
| aggregate_flow->tls_msg.tls_alert_level = q[0]; | ||
| aggregate_flow->tls_msg.tls_alert_desc = q[1]; | ||
| } | ||
| } | ||
| ret = bpf_map_update_elem(&additional_flow_metrics, &id, aggregate_flow, BPF_ANY); | ||
| } else { | ||
| additional_metrics new_flow = { | ||
| .start_mono_time_ts = current_ts, | ||
| .end_mono_time_ts = current_ts, | ||
| .verdict = verdict, | ||
| .tls_msg = { | ||
| .family = (u8)msg->family, | ||
| .size = msg->size, | ||
| .local_port = (u16)msg->local_port, | ||
| .remote_port = (u16)bpf_ntohl(msg->remote_port), | ||
| }, | ||
| }; | ||
| unsigned char *p2 = (unsigned char *)(long)msg->data; | ||
| unsigned char *end2 = (unsigned char *)(long)msg->data_end; | ||
| if (p2 + 5 <= end2) { | ||
| new_flow.tls_msg.tls_content_type = p2[0]; | ||
| unsigned char *q2 = p2 + 5; | ||
| if (new_flow.tls_msg.tls_content_type == 22 && q2 + 1 <= end2) { | ||
| new_flow.tls_msg.tls_handshake_type = q2[0]; | ||
| } else if (new_flow.tls_msg.tls_content_type == 21 && q2 + 2 <= end2) { | ||
| new_flow.tls_msg.tls_alert_level = q2[0]; | ||
| new_flow.tls_msg.tls_alert_desc = q2[1]; | ||
| } | ||
| } | ||
| ret = bpf_map_update_elem(&additional_flow_metrics, &id, &new_flow, BPF_ANY); | ||
| } | ||
| return ret; | ||
| } | ||
|
|
||
| SEC("sk_msg") | ||
| int bpf_ktls_redir(struct sk_msg_md *msg) { | ||
| struct sock_key skk = { | ||
| .local_port = bpf_ntohl(msg->remote_port), | ||
| .remote_port = msg->local_port, | ||
| .family = msg->family, | ||
| }; | ||
|
|
||
| switch (msg->family) { | ||
| case AF_INET: | ||
| __builtin_memcpy(skk.remote_ip, ip4in6, sizeof(ip4in6)); | ||
| __builtin_memcpy(skk.local_ip, ip4in6, sizeof(ip4in6)); | ||
| __builtin_memcpy(skk.remote_ip + sizeof(ip4in6), &msg->remote_ip4, sizeof(msg->remote_ip4)); | ||
| __builtin_memcpy(skk.local_ip + sizeof(ip4in6), &msg->local_ip4, sizeof(msg->local_ip4)); | ||
| break; | ||
| case AF_INET6: | ||
| return SK_PASS; | ||
| } | ||
|
|
||
| int verdict = bpf_msg_redirect_hash(msg, &sock_hash, &skk, BPF_F_INGRESS); | ||
| int ret = find_update_flow(msg, verdict); | ||
| if (ret != 0 && trace_messages) { | ||
| bpf_printk("error updating flow %d\n", ret); | ||
| } | ||
|
|
||
| return SK_PASS; | ||
| } | ||
|
|
||
| #endif // __KTLS_TRACKER_H__ | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@msherif1234 was the goal of your initial PoC to get the content of
msg->datahere ?I'm trying to see how we could read some parts of the packet payload, especially when the traffic is encrypted 👼
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The idea behind this PoC was triggered by this talk in KubeCon https://youtu.be/-2FykHaqvlg?si=ArqarppDqd33YH15
this approach avoid using userspace probes
https://elixir.bootlin.com/linux/v6.17.1/source/include/uapi/linux/bpf.h#L6553 sk_msg_md has the same layout like tc or tcp hook look like u can collect all L2/L3/L4 headers info from there
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
the key thing here is this whole approach relies on using kTLS and more advanced openssl libraries if that is a GO I would like to help with project if there is interest, pls lmk
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
if we are looking for user-space approach for SSL/TLS we can do something like https://github.com/gojue/ecapture/tree/master which I can help PoC it in separate PR if there is interest
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for your feedback @msherif1234
Let's discuss this on the upstream slack 😸