-
Notifications
You must be signed in to change notification settings - Fork 60
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #79 from coroot/dns_tracking
Add DNS protocol tracing
- Loading branch information
Showing
11 changed files
with
276 additions
and
38 deletions.
There are no files selected for viewing
This file contains 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 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 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
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains 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,58 @@ | ||
#define DNS_QR_RESPONSE 0b10000000 | ||
#define DNS_OPCODE 0b01111000 | ||
#define DNS_Z 0b11110000 | ||
#define DNS_RCODE 0b00001111 | ||
|
||
struct dns_header { | ||
__s16 id; | ||
__u8 bits0; | ||
__u8 bits1; | ||
__s16 qdcount; | ||
}; | ||
|
||
static __always_inline | ||
int is_dns_request(char *buf, __u64 buf_size, __s16 *stream_id) { | ||
struct dns_header h = {}; | ||
if (buf_size < sizeof(h)) { | ||
return 0; | ||
} | ||
bpf_read(buf, h); | ||
if (h.bits0 & DNS_QR_RESPONSE) { | ||
return 0; | ||
} | ||
if (h.bits0 & DNS_OPCODE) { | ||
return 0; | ||
} | ||
h.qdcount = bpf_ntohs(h.qdcount); | ||
|
||
if (h.qdcount != 1) { | ||
return 0; | ||
} | ||
*stream_id = h.id; | ||
return 1; | ||
} | ||
|
||
static __always_inline | ||
int is_dns_response(char *buf, __u64 buf_size, __s16 *stream_id, __u32 *status) { | ||
struct dns_header h = {}; | ||
if (buf_size < sizeof(h)) { | ||
return 0; | ||
} | ||
bpf_read(buf, h); | ||
if (!(h.bits0 & DNS_QR_RESPONSE)) { | ||
return 0; | ||
} | ||
if (h.bits0 & DNS_OPCODE) { | ||
return 0; | ||
} | ||
if (!(h.bits1 & DNS_Z)) { | ||
return 0; | ||
} | ||
h.qdcount = bpf_ntohs(h.qdcount); | ||
if (h.qdcount != 1) { | ||
return 0; | ||
} | ||
*status = h.bits1 & DNS_RCODE; | ||
*stream_id = h.id; | ||
return 1; | ||
} |
This file contains 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.