calico: eBPF not issuing TCP RST on unknown connections

Expected Behavior

When a node receives a packet for an unknown TCP connection on the host IP or to a closed port, it should by default reject that connection by issuing a packet with the TCP RST bit set. There may be situations where it should be silently dropped, in default it should behave transparently.

This behaviour should be identical between the eBPF mode and the iptables mode.

Current Behavior

When sending a TCP packet to a port on the hostIP of a node running calico on eBPF mode, for unknown connections, there is no reply sent, the packet is silently dropped and the client will resume retries until timeout. This happens both for unknown connections against closed ports and open ports. New connections against closed ports are properly rejected.

When sending such an unknown TCP packet to a node running calico on iptables mode, there is an immediate TCP RST packet and the client can immediately terminate (and potentially reestablish a connection).

Possible Solution

Even in eBPF mode, a packet with RST bit set should be sent for any connection not in conntrack.

Steps to Reproduce (for bugs)

  • Send a TCP packet without proper prior handshake to a nodeIP of a node which is eBPF enabled, regardless if the port is open or not.
  • Observe that you won’t get a reply.

Trivial python script to trigger this:

#!/usr/bin/env python3

from scapy.all import *
import argparse

def main():
    parser = argparse.ArgumentParser("rst-test")
    parser.add_argument("dest_host", help="the destination host to test against")
    parser.add_argument("dest_port", help="the destination port to test against", type=int)
    args = parser.parse_args()

    packet = IP(dst=args.dest_host) / TCP(sport=13337, dport=args.dest_port, seq=1337, ack=1336, flags="PA") / Raw(load="test")

    response = sr1(packet, timeout=10)

    if response:
        if response.haslayer(TCP) and response.getlayer(TCP).flags & 0x04:
            print("Received RST packet")
        else:
            print("Did not receive RST packet")
    else:
        print("No response received")

if __name__ == "__main__":
    main()

Context

We are running the apiservers on an anycast IP in the hostNetwork, announced via BGP. When one master node goes down and withdraws its announcement, ECMP routing will route the traffic to a remaining control plane node. With that, such a remaining node will see traffic for unknown TCP connections, right after the ECMP change.

In our case, on eBPF-enabled clusters, kubelets’ connections to the apiserver start to hang until a timeout is reached, partially causing nodes to be ejected as not-ready anymore. We identified that this happens because the kubelet don’t notice that their connection to the apiserver broke, they keep on resending packets, because they never see a TCP reset.

On the non-eBPF clusters, this scenario is not an issue at all. After the ECMP change, kubelet will see a TCP reset, and establish a new TCP connection immediately.

Remarks

This might be related to #8854 or at least affected by a fix for this. From the user perspective, it is a different thing.

Your Environment

  • Calico version 3.27.4/3.28.0
  • Orchestrator version (e.g. kubernetes, mesos, rkt): kubernetes v1.28.9
  • Operating System and version: FlatCar beta 3874.1.0 with kernel 6.6.21-flatcar

About this issue

  • Original URL
  • State: closed
  • Created 2 years ago
  • Comments: 15 (9 by maintainers)

Commits related to this issue

Most upvoted comments

This is a newer image thruby/node:3-28-tcp-reject-2 that came up well for me. Hard to say with the log excerpt anything beyond that there is a loop timeout indeed.

calico-node didn’t come up ready on 3 of 8 nodes with internal dataplane main loop timeout:

│ calico-node 2024-06-25 13:47:17.147 [INFO][53] felix/health.go 336: Overall health status changed: live=false ready=false                                                                                                                                                                                                │
│ calico-node +---------------------------+---------+----------------+-----------------+--------+                                                                                                                                                                                                                          │
│ calico-node |         COMPONENT         | TIMEOUT |    LIVENESS    |    READINESS    | DETAIL |                                                                                                                                                                                                                          │
│ calico-node +---------------------------+---------+----------------+-----------------+--------+                                                                                                                                                                                                                          │
│ calico-node | BPFEndpointManager        | -       | -              | reporting ready |        |                                                                                                                                                                                                                          │
│ calico-node | CalculationGraph          | 30s     | reporting live | reporting ready |        |                                                                                                                                                                                                                          │
│ calico-node | FelixStartup              | -       | reporting live | reporting ready |        |                                                                                                                                                                                                                          │
│ calico-node | InternalDataplaneMainLoop | 1m30s   | timed out      | timed out       |        |                                                                                                                                                                                                                          │
│ calico-node +---------------------------+---------+----------------+-----------------+--------+                                                                                                                                                                                                                          │
│ calico-node 2024-06-25 13:47:17.149 [WARNING][53] felix/health.go 286: Reporter is not live: timed out. name="InternalDataplaneMainLoop"                                                                                                                                                                                 │
│ calico-node 2024-06-25 13:47:17.149 [INFO][53] felix/health.go 294: Reporter is not ready: timed out. name="InternalDataplaneMainLoop"                                                                                                                                                                                   │
│ calico-node 2024-06-25 13:47:27.122 [WARNING][53] felix/health.go 286: Reporter is not live: timed out. name="InternalDataplaneMainLoop"                                                                                                                                                                                 │
│ calico-node 2024-06-25 13:47:27.122 [INFO][53] felix/health.go 294: Reporter is not ready: timed out. name="InternalDataplaneMainLoop"                                                                                                                                                                                   │
│ calico-node 2024-06-25 13:47:27.128 [WARNING][53] felix/health.go 286: Reporter is not live: timed out. name="InternalDataplaneMainLoop"                                                                                                                                                                                 │

With loglevel set to info, nothing special visible in the logs AFAICS.

Initially, it is reporting live, but not ready. Later, it is reporting timeouts for both liveness and readiness (as in the excerpt above). I could not identify a pattern which hosts would come up and which not.

But regarding the original issue: All 3 masters came up fine with the new calico-node and I properly received my tcp-rst when sending out-of-line packets. So it seems this might be solved.

@tomastigera that does fit, because in my scenario we do have such a mid-flow packet, because it was rerouted due to ECMP when 1 potential (and original) destination was removed. I understand the concern about spoofing, but I’d say there only is a risk if such a packet would break an existing connection state. In this case, it would only be about “revealing” that no matching connections exists. And that response would be sent to the “original” IP, not the spoofer. And not sure inhowfar this would require sequence numbers to match sufficiently. And, at least in my case, now sure how generic this is: If someone would be able to inject an invalid packet into the hostNetwork at this level, we’d have a problem anyway. A spoofed packet from outside should have been rejected with invalid source IP already before being routed into that network.

All ebpf drop counters stay 0. And I didn’t see anything special in conntrack. But I digged a bit into the iptables rules.

In a cluster without eBPF, as far as I understand we pass INPUT->cali-INPUT->cali-from-host-endpoint->cali-fh-any-interface-at-all->cali-failsafe-in and at least for this, we have a failsafe rule which causes the accept (end then ending in a reset because there is no connection).

In the eBPF case, we see directly the following in the INPUT chain: 373 49106 DROP all – * * 0.0.0.0/0 0.0.0.0/0 /* cali:XQL0mC-L6wldZdgN / / Drop packets from unknown flows. */ mark match 0x5000000/0x5000000

Observing the counter, this triggers for my unrelated packet and causes the drop. So this at least should be the reason for the differing behaviour. Anything we can do about this?

An additional factor here is that I wouldn’t even expect eBPF to interfere here, since we are plainly working with hostIP, not pods or calico interfaces involved at all. It is pure host2host communication, with IP addresses not managed via calico or K8s. And, as mentioned, in non-ebpf mode, it behaves as expected.