Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(bpf): Distinguish TCP state #587

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 10 additions & 6 deletions control/kern/tproxy.c
Original file line number Diff line number Diff line change
Expand Up @@ -1022,6 +1022,7 @@ int tproxy_lan_ingress(struct __sk_buff *skb)
if (get_link_h_len(skb->ifindex, &link_h_len))
return TC_ACT_OK;
bool tcp_state_syn = false;
bool tcp_state_first_syn = false;
int ret = parse_transport(skb, link_h_len,
&ethh, &l3hdr, &l4hdr,
&ihl, &l3proto, &l4proto);
Expand Down Expand Up @@ -1074,8 +1075,9 @@ int tproxy_lan_ingress(struct __sk_buff *skb)
// TCP.
struct tcphdr *tcph = (struct tcphdr *)l4hdr;

tcp_state_syn = tcph->syn && !tcph->ack;
if (tcp_state_syn)
tcp_state_syn = tcph->syn;
tcp_state_first_syn = tcph->syn && !tcph->ack;
if (tcp_state_first_syn)
goto new_connection;

sk = bpf_skc_lookup_tcp(skb, &tuple, tuple_size,
Expand All @@ -1093,7 +1095,7 @@ int tproxy_lan_ingress(struct __sk_buff *skb)
new_connection:
__builtin_memset(flag, 0, sizeof(flag));
if (l4proto == IPPROTO_TCP) {
if (!tcp_state_syn) {
if (!tcp_state_first_syn) {
// Not a new TCP connection.
// Perhaps single-arm.
return TC_ACT_OK;
Expand Down Expand Up @@ -1357,6 +1359,7 @@ int tproxy_wan_egress(struct __sk_buff *skb)
if (get_link_h_len(skb->ifindex, &link_h_len))
return TC_ACT_OK;
bool tcp_state_syn = false;
bool tcp_state_first_syn = false;
int ret = parse_transport(skb, link_h_len,
&ethh, &l3hdr, &l4hdr,
&ihl, &l3proto, &l4proto);
Expand All @@ -1375,13 +1378,14 @@ int tproxy_wan_egress(struct __sk_buff *skb)
// Backup for further use.
struct tcphdr *tcph = (struct tcphdr *)l4hdr;

tcp_state_syn = tcph->syn && !tcph->ack;
tcp_state_syn = tcph->syn;
tcp_state_first_syn = tcph->syn && !tcph->ack;
__u8 outbound;
bool must;
__u32 mark;
struct pid_pname *pid_pname = NULL;

if (unlikely(tcp_state_syn)) {
if (unlikely(tcp_state_first_syn)) {
// New TCP connection.
// bpf_printk("[%X]New Connection", bpf_ntohl(tcph.seq));
__u32 flag[8] = { L4ProtoType_TCP }; // TCP
Expand Down Expand Up @@ -1476,7 +1480,7 @@ int tproxy_wan_egress(struct __sk_buff *skb)
return TC_ACT_SHOT;
}

if (unlikely(tcp_state_syn)) {
if (unlikely(tcp_state_first_syn)) {
struct routing_result routing_result = {};

routing_result.outbound = outbound;
Expand Down
Loading