@@ -97,6 +97,36 @@ static __always_inline u8 is_dns(connection_info_t *conn) {
9797 return is_dns_port (conn -> s_port ) || is_dns_port (conn -> d_port );
9898}
9999
100+ static __always_inline void populate_dns_record (dns_req_t * req ,
101+ const pid_connection_info_t * p_conn ,
102+ const u16 orig_dport ,
103+ const u32 size ,
104+ const u8 qr ,
105+ const u16 id ,
106+ const conn_pid_t * conn_pid ) {
107+ __builtin_memcpy (& req -> conn , & p_conn -> conn , sizeof (connection_info_t ));
108+
109+ req -> flags = EVENT_DNS_REQUEST ;
110+ req -> len = size ;
111+ req -> dns_q = qr ;
112+ req -> id = bpf_ntohs (id );
113+ req -> tp .ts = bpf_ktime_get_ns ();
114+ req -> pid = conn_pid -> p_info ;
115+
116+ trace_key_t t_key = {0 };
117+ trace_key_from_pid_tid_with_p_key (& t_key , & conn_pid -> p_key , conn_pid -> id );
118+
119+ const u8 found = find_trace_for_client_request_with_t_key (
120+ p_conn , orig_dport , & t_key , conn_pid -> id , & req -> tp );
121+
122+ bpf_dbg_printk ("handle_dns: looking up client trace info, found %d" , found );
123+ if (found ) {
124+ urand_bytes (req -> tp .span_id , SPAN_ID_SIZE_BYTES );
125+ } else {
126+ init_new_trace (& req -> tp );
127+ }
128+ }
129+
100130static __always_inline u8 handle_dns (struct __sk_buff * skb ,
101131 connection_info_t * conn ,
102132 protocol_info_t * p_info ) {
@@ -158,30 +188,51 @@ static __always_inline u8 handle_dns(struct __sk_buff *skb,
158188 dns_req_t * req = bpf_ringbuf_reserve (& events , sizeof (dns_req_t ), 0 );
159189
160190 if (req ) {
161- __builtin_memcpy (& req -> conn , conn , sizeof (connection_info_t ));
162-
163- req -> flags = EVENT_DNS_REQUEST ;
164- req -> p_type = skb -> pkt_type ;
165- req -> len = skb -> len ;
166- req -> dns_q = qr ;
167- req -> id = bpf_ntohs (hdr .id );
168- req -> ts = bpf_ktime_get_ns ();
169- req -> tp .ts = bpf_ktime_get_ns ();
170- req -> pid = conn_pid -> p_info ;
171-
172- trace_key_t t_key = {0 };
173- trace_key_from_pid_tid_with_p_key (& t_key , & conn_pid -> p_key , conn_pid -> id );
174-
175- const u8 found = find_trace_for_client_request_with_t_key (
176- & p_conn , orig_dport , & t_key , conn_pid -> id , & req -> tp );
177-
178- bpf_dbg_printk ("handle_dns: looking up client trace info, found %d" , found );
179- if (found ) {
180- urand_bytes (req -> tp .span_id , SPAN_ID_SIZE_BYTES );
181- } else {
182- init_new_trace (& req -> tp );
183- }
184- read_skb_bytes (skb , dns_off , req -> buf , sizeof (req -> buf ));
191+ u32 len = skb -> len - dns_off ;
192+ bpf_clamp_umax (len , 512 );
193+ populate_dns_record (req , & p_conn , orig_dport , len , qr , hdr .id , conn_pid );
194+
195+ read_skb_bytes (skb , dns_off , req -> buf , len );
196+ bpf_d_printk ("sending dns trace" );
197+ bpf_ringbuf_submit (req , get_flags ());
198+ }
199+
200+ return 1 ;
201+ }
202+
203+ return 0 ;
204+ }
205+
206+ static __always_inline u8 handle_dns_buf (const unsigned char * buf ,
207+ const int size ,
208+ pid_connection_info_t * p_conn ,
209+ u16 orig_dport ) {
210+
211+ if (size < sizeof (struct dnshdr )) {
212+ bpf_d_printk ("dns packet too small" );
213+ return 0 ;
214+ }
215+
216+ struct dnshdr hdr ;
217+ bpf_probe_read_user (& hdr , sizeof (struct dnshdr ), buf );
218+
219+ const u16 flags = bpf_ntohs (hdr .flags );
220+ const u8 qr = dns_qr (flags );
221+
222+ bpf_d_printk ("QR type: %d" , qr );
223+
224+ if (qr == k_dns_qr_query || qr == k_dns_qr_resp ) {
225+ conn_pid_t * conn_pid = bpf_map_lookup_elem (& sock_pids , & p_conn -> conn );
226+ if (!conn_pid ) {
227+ bpf_d_printk ("can't find connection info for dns call" );
228+ return 0 ;
229+ }
230+
231+ dns_req_t * req = bpf_ringbuf_reserve (& events , sizeof (dns_req_t ), 0 );
232+ if (req ) {
233+ populate_dns_record (req , p_conn , orig_dport , size , qr , hdr .id , conn_pid );
234+
235+ bpf_probe_read (req -> buf , sizeof (req -> buf ), buf );
185236 bpf_d_printk ("sending dns trace" );
186237 bpf_ringbuf_submit (req , get_flags ());
187238 }
0 commit comments