Skip to content

Commit

Permalink
working dns map write
Browse files Browse the repository at this point in the history
  • Loading branch information
r-caamano committed Dec 5, 2023
1 parent 4b62ecd commit f769850
Showing 1 changed file with 23 additions and 17 deletions.
40 changes: 23 additions & 17 deletions src/zfw_xdp_dns_filter.c
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,25 @@ struct dns_resource_record {
__uint8_t ipaddr[4];
};

static inline int set_dns(int index, void *dns_question){
struct dns_name_struct *dns_name = NULL;
dns_name = bpf_map_lookup_elem(&dns_map, &index);
long answer = 0;
if(dns_name){
answer = bpf_probe_read_kernel_str((void *)&dns_name->dns_name, sizeof(dns_name->dns_name), dns_question);
bpf_printk("answer=%ld",answer);
if (answer > 0) {
for(int x = 0; x < answer; x++){
bpf_printk(":%c", dns_name->dns_name[x]);
}
}
}else{
bpf_printk("no map entry found");
}
return answer;
}


SEC("xdp")
int xdp_filter(struct xdp_md *ctx) {

Expand Down Expand Up @@ -111,7 +130,6 @@ int xdp_filter(struct xdp_md *ctx) {

int max_question_count = 1;
__u8 dns_name_len = 0;
struct dns_name_struct *dns_name = NULL;
if ((dnsh->qdcount != 0) && (dnsh->ancount == 0) && (dnsh->nscount == 0) && (dnsh->arcount == 0)) {
for (int i = 0; i < max_question_count; i++) {
for (int j = 0; j < 255; j++) {
Expand All @@ -123,23 +141,11 @@ int xdp_filter(struct xdp_md *ctx) {
break;
}
}
bpf_printk("completed %d ", dns_name_len);
// for (int j = 0; j < dns_name_len; j++) {
// bpf_printk("dns name is %c ", *(dns_payload + j));
// }
// bpf_map_update_elem(&dns_map, &i, &dns_payload,0);
// memcpy(&dns_map,&dns_payload,dns_name_len);
// bpf_printk("completed2 %d ", dns_name_len);
dns_name = bpf_map_lookup_elem(&dns_map, &i);
if(dns_name){
long anwser = bpf_probe_read_kernel_str(&dns_name->dns_name, 2, dns_payload);
if (!anwser) {
return XDP_PASS;
}
long ret = set_dns(i, dns_payload);
if (ret < 0){
bpf_printk("wrote %d characters", ret);
}
return XDP_PASS;

bpf_printk("completed2 %d ", dns_name_len);
dns_payload = (dns_payload + dns_name_len + 4);
}
}
return XDP_PASS;
Expand Down

0 comments on commit f769850

Please sign in to comment.