Skip to content

Commit c783846

Browse files
committed
fix(network-monitor): Include the packet payload regarless of L4 protocol
* User-space part of pulsar is expecting the buffer to be initialized regardless of L4 protocol (TCP or UDP). * DNS responses, if long enough, might go through TCP. Not very common, but possible. * Stop returning an error when packet payload length is 0. It's totally normal for SYN, SYN-ACK and ACK packets. Logging errors for them is an unnecessary noise. Fixes #297
1 parent b957fe0 commit c783846

File tree

3 files changed

+18
-11
lines changed

3 files changed

+18
-11
lines changed

crates/bpf-builder/include/buffer.bpf.h

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -132,10 +132,16 @@ static __always_inline int buffer_append_skb_bytes(struct buffer *buffer,
132132
return -1;
133133
}
134134

135+
// * The `< 0` case is impossible to reproduce (otherwise it would mean
136+
// `skb->len` is somehow lower than the size of packet headers we already
137+
// consumed from that SKB).
138+
// It's here only to appease the eBPF verifier (using `u32` and checking
139+
// just for `0` doesn't work).
140+
// * The `= 0` case usually applies to SYN, SYN-ACK, ACK or any other packets
141+
// which don't contain any payload after L4 headers.
142+
// Therefore, we don't return any error here.
135143
if (len <= 0) {
136-
LOG_ERROR("Invalid offset (%zu) exceeding the packet length (%zu).",
137-
offset, skb->len);
138-
return -1;
144+
return 0;
139145
}
140146

141147
int r = bpf_skb_load_bytes(skb, offset, &((char *)buffer->buffer)[pos],

crates/modules/network-monitor/probes.bpf.c

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -520,6 +520,8 @@ __always_inline int process_skb(struct __sk_buff *skb,
520520
void *data_end = (void *)(long)skb->data_end;
521521
void *data = (void *)(long)skb->data;
522522

523+
buffer_index_init(&network_event->buffer, &msg_event->data);
524+
523525
// Parse L3 header (IPv4 / IPv6).
524526
switch (l3_proto) {
525527
case ETH_P_IPV4: {
@@ -653,19 +655,18 @@ __always_inline int process_skb(struct __sk_buff *skb,
653655
}
654656
break;
655657
}
656-
657-
buffer_index_init(&network_event->buffer, &msg_event->data);
658-
if (buffer_append_skb_bytes(&network_event->buffer, &msg_event->data, skb,
659-
headers_len) < 0) {
660-
LOG_ERROR("Failed to retrieve the packet payload. The event is going to miss the `data` part.");
661-
}
662658
break;
663659
}
664660
default:
665661
LOG_DEBUG("ignored unsupported L4 protocol %d", l4_proto);
666662
goto send_event;
667663
}
668664

665+
if (buffer_append_skb_bytes(&network_event->buffer, &msg_event->data, skb,
666+
headers_len) < 0) {
667+
LOG_ERROR("Failed to retrieve the packet payload. The event is going to miss the `data` part.");
668+
}
669+
669670
msg_event->data_len = skb->len - headers_len;
670671

671672
send_event:

crates/modules/network-monitor/src/lib.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -303,11 +303,11 @@ pub mod pulsar {
303303
let data = data
304304
.bytes(&event.buffer)
305305
.map_err(|err| {
306-
log::error!("[dns] Error getting message: {}", err);
306+
log::error!("Error getting network packet payload: {err}");
307307
})
308308
.ok()?;
309309

310-
// any valid dns data?
310+
// Check wheter the payload contains any DNS data.
311311
let dns = dns_parser::Packet::parse(data).ok()?;
312312
let with_q = !dns.questions.is_empty();
313313
let with_a = !dns.answers.is_empty();

0 commit comments

Comments
 (0)