Skip to content

Commit 71e40fe

Browse files
committed
feat: agent - eBPF Support unix domain sockets
1 parent e16f61a commit 71e40fe

File tree

3 files changed

+27
-10
lines changed

3 files changed

+27
-10
lines changed

agent/src/ebpf/kernel/include/protocol_inference.h

+6-6
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,9 @@ __protocol_port_check(enum traffic_protocol proto,
111111
return false;
112112
}
113113

114+
if (conn_info->sk_type == SOCK_UNIX)
115+
return true;
116+
114117
__u32 key = proto;
115118
ports_bitmap_t *ports = proto_ports_bitmap__lookup(&key);
116119
if (ports) {
@@ -426,7 +429,7 @@ static __inline enum message_type parse_http2_headers_frame(const char
426429
#if defined(LINUX_VER_KFUNC) || defined(LINUX_VER_5_2_PLUS)
427430
#define HTTPV2_LOOP_MAX 8
428431
#else
429-
#define HTTPV2_LOOP_MAX 6
432+
#define HTTPV2_LOOP_MAX 5
430433
#endif
431434
/*
432435
* HTTPV2_FRAME_READ_SZ取值考虑以下3部分:
@@ -3802,10 +3805,6 @@ infer_protocol_1(struct ctx_info_s *ctx,
38023805
if (conn_info->sk == NULL)
38033806
return inferred_message;
38043807

3805-
if (conn_info->tuple.dport == 0 || conn_info->tuple.num == 0) {
3806-
return inferred_message;
3807-
}
3808-
38093808
/*
38103809
* The socket that is indeed determined to be a protocol does not
38113810
* enter drop_msg_by_comm().
@@ -3884,7 +3883,8 @@ infer_protocol_1(struct ctx_info_s *ctx,
38843883
* If the data source comes from kernel system calls, it is discarded
38853884
* directly because some kernel probes do not handle TLS data.
38863885
*/
3887-
if (protocol_port_check_1(PROTO_TLS, conn_info) &&
3886+
if (conn_info->sk_type != SOCK_UNIX &&
3887+
protocol_port_check_1(PROTO_TLS, conn_info) &&
38883888
extra->source == DATA_SOURCE_SYSCALL) {
38893889
/*
38903890
* TLS first performs handshake protocol inference and discards the data

agent/src/ebpf/kernel/include/socket_trace.h

+3
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,8 @@
3434
#define INFER_CONTINUE 1
3535
#define INFER_TERMINATE 2
3636

37+
#define SOCK_UNIX 11 // Used to identify UNIX domain sockets.
38+
3739
#define MAX_PUSH_DELAY_TIME_NS 100000000ULL // 100ms
3840

3941
typedef long unsigned int __kernel_size_t;
@@ -85,6 +87,7 @@ struct mmsghdr {
8587
#define SOCK_CHECK_TYPE_ERROR 0
8688
#define SOCK_CHECK_TYPE_UDP 1
8789
#define SOCK_CHECK_TYPE_TCP_ES 2
90+
#define SOCK_CHECK_TYPE_UNIX 3
8891

8992
#include "socket_trace_common.h"
9093

agent/src/ebpf/kernel/socket_trace.bpf.c

+18-4
Original file line numberDiff line numberDiff line change
@@ -618,10 +618,11 @@ static __inline int is_tcp_udp_data(void *sk,
618618
bpf_probe_read_kernel(&conn_info->skc_family,
619619
sizeof(conn_info->skc_family),
620620
sk + offset->struct_sock_family_offset);
621-
/*
622-
* Without thinking about PF_UNIX.
623-
*/
621+
624622
switch (conn_info->skc_family) {
623+
case PF_UNIX:
624+
// Handle UNIX domain sockets, tracing local IPC
625+
return SOCK_CHECK_TYPE_UNIX;
625626
case PF_INET:
626627
break;
627628
case PF_INET6:
@@ -691,6 +692,11 @@ static __inline void init_conn_info(__u32 tgid, __u32 fd,
691692
static __inline bool get_socket_info(struct __socket_data *v, void *sk,
692693
struct conn_info_s *conn_info)
693694
{
695+
if (conn_info->sk_type == SOCK_UNIX) {
696+
v->tuple.addr_len = 4;
697+
return true;
698+
}
699+
694700
if (v == NULL || sk == NULL)
695701
return false;
696702

@@ -732,6 +738,11 @@ static __inline bool get_socket_info(struct __socket_data *v, void *sk,
732738
static __inline bool get_socket_info(struct __tuple_t *tuple, void *sk,
733739
struct conn_info_s *conn_info)
734740
{
741+
if (conn_info->sk_type == SOCK_UNIX) {
742+
tuple->addr_len = 4;
743+
return true;
744+
}
745+
735746
if (sk == NULL)
736747
return false;
737748

@@ -1704,7 +1715,7 @@ static __inline int process_data(struct pt_regs *ctx, __u64 id,
17041715
#endif
17051716
struct conn_info_s *conn_info, __conn_info = { 0 };
17061717
conn_info = &__conn_info;
1707-
__u8 sock_state;
1718+
__u8 sock_state = 0;
17081719
if (!(sk != NULL &&
17091720
((sock_state = is_tcp_udp_data(sk, offset, conn_info))
17101721
!= SOCK_CHECK_TYPE_ERROR))) {
@@ -1715,6 +1726,9 @@ static __inline int process_data(struct pt_regs *ctx, __u64 id,
17151726
#endif
17161727
}
17171728

1729+
if (sock_state == SOCK_CHECK_TYPE_UNIX)
1730+
conn_info->sk_type = SOCK_UNIX;
1731+
17181732
init_conn_info(id >> 32, args->fd, conn_info, sk, direction,
17191733
bytes_count, offset);
17201734
if (conn_info->tuple.l4_protocol == IPPROTO_UDP

0 commit comments

Comments
 (0)