Skip to content

Commit

Permalink
Code changes
Browse files Browse the repository at this point in the history
Signed-off-by: Dan Finneran <[email protected]>
  • Loading branch information
thebsdbox committed Sep 6, 2024
1 parent 962d2e0 commit 64ba76d
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 45 deletions.
68 changes: 30 additions & 38 deletions eBPF/redirect/ebpf/redirect.c
Original file line number Diff line number Diff line change
Expand Up @@ -49,9 +49,6 @@ static inline int redirect_tcp(struct __sk_buff *skb, bool ingress) {
return TC_ACT_OK;
}
__u32 ip_hlen = 0;
//__u32 poffset = 0;
//__u32 plength = 0;
// __u32 ip_total_length = bpf_ntohs(iph->tot_len);

ip_hlen = iph->ihl << 2;

Expand All @@ -72,15 +69,17 @@ static inline int redirect_tcp(struct __sk_buff *skb, bool ingress) {
bpf_printk("bpf_skb_load_bytes eth failed");
return TC_ACT_OK;
}
__u16 source = bpf_htons(tcp.source);
__u16 dest = bpf_htons(tcp.dest);

// something doesn't seem right here
__u16 source = tcp.source;
__u16 dest = tcp.dest;

bpf_printk("ingress:%s source: %d -> destination %d",
ingress ? "true" : "false", source, dest);

if (ingress) {
if (dest == 2001) {
tcp.dest = bpf_ntohs(2000);
tcp.dest = 2000;
long ret =
bpf_skb_store_bytes(skb, sizeof(struct ethhdr) + (ip.ihl << 2),
&tcp, sizeof(tcp), BPF_F_RECOMPUTE_CSUM);
Expand All @@ -89,41 +88,34 @@ static inline int redirect_tcp(struct __sk_buff *skb, bool ingress) {
}
}
} else {
if (source == 2000) {
tcp.source = bpf_ntohs(2001);
long ret =
bpf_skb_store_bytes(skb, sizeof(struct ethhdr) + (ip.ihl << 2),
&tcp, sizeof(tcp), BPF_F_RECOMPUTE_CSUM);
if (ret != 0) {
bpf_printk("Error writing bytes");
}
}
//
}
}
}

/*
15:27:27.195146 IP (tos 0x0, ttl 64, id 64035, offset 0, flags [DF], proto TCP
(6), length 60) 127.0.0.1.55590 > 127.0.0.1.2001: Flags [S], cksum 0xfe30
(incorrect -> 0xde8f), seq 1127217463, win 65495, options [mss 65495,sackOK,TS
val 1342243858 ecr 0,nop,wscale 7], length 0 15:27:27.195160 IP (tos 0x0, ttl
64, id 0, offset 0, flags [DF], proto TCP (6), length 40) 127.0.0.1.2001 >
127.0.0.1.55590: Flags [R.], cksum 0x946e (correct), seq 0, ack 1127217464,
win 0, length 0
15:27:19.192319 IP (tos 0x0, ttl 64, id 57861, offset 0, flags [DF], proto
TCP (6), length 60) 127.0.0.1.50720 > 127.0.0.1.2001: Flags [S], cksum 0xfe30
(incorrect -> 0xb3db), seq 1553087697, win 65495, options [mss 65495,sackOK,TS
val 1342235856 ecr 0,nop,wscale 7], length 0 15:27:19.192345 IP (tos 0x0, ttl
64, id 0, offset 0, flags [DF], proto TCP (6), length 60) 127.0.0.1.2000 >
127.0.0.1.50720: Flags [S.], cksum 0xfe30 (incorrect -> 0x7bf5), seq
2242477415, ack 1553087698, win 65483, options [mss 65495,sackOK,TS val
1342235856 ecr 1342235856,nop,wscale 7], length 0 15:27:19.192359 IP (tos 0x0,
ttl 64, id 0, offset 0, flags [DF], proto TCP (6), length 40) 127.0.0.1.50720
> 127.0.0.1.2001: Flags [R], cksum 0x4a89 (incorrect -> 0x4a88), seq
1553087698, win 0, length 0
*/

return TC_ACT_OK;
/*
15:27:27.195146 IP (tos 0x0, ttl 64, id 64035, offset 0, flags [DF], proto TCP
(6), length 60) 127.0.0.1.55590 > 127.0.0.1.2001: Flags [S], cksum 0xfe30
(incorrect -> 0xde8f), seq 1127217463, win 65495, options [mss 65495,sackOK,TS
val 1342243858 ecr 0,nop,wscale 7], length 0 15:27:27.195160 IP (tos 0x0, ttl
64, id 0, offset 0, flags [DF], proto TCP (6), length 40) 127.0.0.1.2001 >
127.0.0.1.55590: Flags [R.], cksum 0x946e (correct), seq 0, ack 1127217464,
win 0, length 0
15:27:19.192319 IP (tos 0x0, ttl 64, id 57861, offset 0, flags [DF], proto
TCP (6), length 60) 127.0.0.1.50720 > 127.0.0.1.2001: Flags [S], cksum 0xfe30
(incorrect -> 0xb3db), seq 1553087697, win 65495, options [mss 65495,sackOK,TS
val 1342235856 ecr 0,nop,wscale 7], length 0 15:27:19.192345 IP (tos 0x0, ttl
64, id 0, offset 0, flags [DF], proto TCP (6), length 60) 127.0.0.1.2000 >
127.0.0.1.50720: Flags [S.], cksum 0xfe30 (incorrect -> 0x7bf5), seq
2242477415, ack 1553087698, win 65483, options [mss 65495,sackOK,TS val
1342235856 ecr 1342235856,nop,wscale 7], length 0 15:27:19.192359 IP (tos 0x0,
ttl 64, id 0, offset 0, flags [DF], proto TCP (6), length 40) 127.0.0.1.50720
> 127.0.0.1.2001: Flags [R], cksum 0x4a89 (incorrect -> 0x4a88), seq
1553087698, win 0, length 0
*/

return TC_ACT_OK;
}

// eBPF hooks - This is where the magic happens!
Expand Down
16 changes: 9 additions & 7 deletions eBPF/response/ebpf/response.c
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,15 @@ static inline int swap_udp(struct __sk_buff *skb) {
data[3] = '\0'; // null terminate the string
bpf_printk("data: %s", data);

u32 data_offset = sizeof(*eth) + sizeof(*iph) + sizeof(*udph);

data[0] = '?';

ret = bpf_skb_store_bytes(skb, data_offset, &data, sizeof(data),
BPF_F_RECOMPUTE_CSUM);

// Something doesn't seem right

/* We'll store the mac addresses (L2) */
__u8 src_mac[ETH_ALEN];
__u8 dst_mac[ETH_ALEN];
Expand Down Expand Up @@ -133,13 +142,6 @@ static inline int swap_udp(struct __sk_buff *skb) {
offsetof(struct udphdr, dest),
&src_port, sizeof(src_port), 0);

u32 data_offset = sizeof(*eth) + sizeof(*iph) + sizeof(*udph);

data[0] = '?';

ret = bpf_skb_store_bytes(skb, data_offset, &data, sizeof(data),
BPF_F_RECOMPUTE_CSUM);

/* And then send it back from wherever it's come from */
ret = bpf_clone_redirect(skb, skb->ifindex, 0);
if (ret) {
Expand Down

0 comments on commit 64ba76d

Please sign in to comment.