Skip to content
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

Use correct byte ordering function for kernel struct member (skc_num) #2002

Merged
merged 1 commit into from
Sep 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 8 additions & 6 deletions src/stirling/source_connectors/tcp_stats/bcc_bpf/tcp_stats.c
Original file line number Diff line number Diff line change
Expand Up @@ -79,12 +79,12 @@ static int tcp_sendstat(struct pt_regs* ctx, uint32_t tgid, uint32_t id, int siz
event.upid.start_time_ticks = get_tgid_start_time();

if (family == AF_INET) {
event.local_addr.in4.sin_port = ntohs(lport);
event.local_addr.in4.sin_port = htons(lport);
event.remote_addr.in4.sin_port = rport;
BPF_PROBE_READ_KERNEL_VAR(event.local_addr.in4.sin_addr.s_addr, &sk_common->skc_rcv_saddr);
BPF_PROBE_READ_KERNEL_VAR(event.remote_addr.in4.sin_addr.s_addr, &sk_common->skc_daddr);
} else if (family == AF_INET6) {
event.local_addr.in6.sin6_port = ntohs(lport);
event.local_addr.in6.sin6_port = htons(lport);
event.remote_addr.in6.sin6_port = rport;
BPF_PROBE_READ_KERNEL_VAR(event.local_addr.in6.sin6_addr, &sk_common->skc_v6_rcv_saddr);
BPF_PROBE_READ_KERNEL_VAR(event.remote_addr.in6.sin6_addr, &sk_common->skc_v6_daddr);
Expand Down Expand Up @@ -158,12 +158,12 @@ int probe_entry_tcp_cleanup_rbuf(struct pt_regs* ctx, struct sock* sk, int copie
event.local_addr.sa.sa_family = family;

if (family == AF_INET) {
event.local_addr.in4.sin_port = ntohs(lport);
event.local_addr.in4.sin_port = htons(lport);
event.remote_addr.in4.sin_port = rport;
BPF_PROBE_READ_KERNEL_VAR(event.local_addr.in4.sin_addr.s_addr, &sk->__sk_common.skc_rcv_saddr);
BPF_PROBE_READ_KERNEL_VAR(event.remote_addr.in4.sin_addr.s_addr, &sk->__sk_common.skc_daddr);
} else if (family == AF_INET6) {
event.local_addr.in6.sin6_port = ntohs(lport);
event.local_addr.in6.sin6_port = htons(lport);
event.remote_addr.in6.sin6_port = rport;
BPF_PROBE_READ_KERNEL_VAR(event.local_addr.in6.sin6_addr, &sk->__sk_common.skc_v6_rcv_saddr);
BPF_PROBE_READ_KERNEL_VAR(event.remote_addr.in6.sin6_addr, &sk->__sk_common.skc_v6_daddr);
Expand Down Expand Up @@ -196,6 +196,8 @@ int probe_entry_tcp_retransmit_skb(struct pt_regs* ctx, struct sock* skp, struct

int lport = -1;
int rport = -1;
// skc_num is stored in host byte order. The rest of our user space processing
// assumes network byte order so convert it here.
BPF_PROBE_READ_KERNEL_VAR(lport, &skp->__sk_common.skc_num);
BPF_PROBE_READ_KERNEL_VAR(rport, &skp->__sk_common.skc_dport);

Expand All @@ -204,13 +206,13 @@ int probe_entry_tcp_retransmit_skb(struct pt_regs* ctx, struct sock* skp, struct
event.local_addr.sa.sa_family = family;

if (family == AF_INET) {
event.local_addr.in4.sin_port = ntohs(lport);
event.local_addr.in4.sin_port = htons(lport);
event.remote_addr.in4.sin_port = rport;
BPF_PROBE_READ_KERNEL_VAR(event.local_addr.in4.sin_addr.s_addr,
&skp->__sk_common.skc_rcv_saddr);
BPF_PROBE_READ_KERNEL_VAR(event.remote_addr.in4.sin_addr.s_addr, &skp->__sk_common.skc_daddr);
} else if (family == AF_INET6) {
event.local_addr.in6.sin6_port = ntohs(lport);
event.local_addr.in6.sin6_port = htons(lport);
event.remote_addr.in6.sin6_port = rport;
BPF_PROBE_READ_KERNEL_VAR(event.local_addr.in6.sin6_addr, &skp->__sk_common.skc_v6_rcv_saddr);
BPF_PROBE_READ_KERNEL_VAR(event.remote_addr.in6.sin6_addr, &skp->__sk_common.skc_v6_daddr);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,9 @@ TEST_F(TcpTraceTest, Capture) {
EXPECT_THAT(records, IsSupersetOf(expected));
// TODO(RagalahariP): Explore options for testing retransmissions in a unit test case,
// as retransmissions are blocking calls without known timeout value.

// TODO(ddelnano): Use a test case that verifies that local address of the socket is correct.
// The current implementation is correct, but other source connectors have had bugs here.
}

} // namespace stirling
Expand Down
Loading