Skip to content

Commit

Permalink
Fix the issue of local probe bypassing flows on Windows
Browse files Browse the repository at this point in the history
When proxyAll is enabled, kube-proxy can be replaced by AntreaProxy, then
Service traffic and non-Service traffic can be distinguished by ServiceCTMark
and NotServiceCTMark. Service traffic with ServiceCTMark should not bypass
Network Policies, and non-Service traffic generated by kubelet with
NotServiceCTMark should bypass Network Policies.

Signed-off-by: Hongliang Liu <lhongliang@vmware.com>
  • Loading branch information
hongliangl committed Mar 24, 2022
1 parent 2c7d486 commit 863b6ba
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 11 deletions.
2 changes: 1 addition & 1 deletion pkg/agent/openflow/client.go
Expand Up @@ -685,7 +685,7 @@ func (c *client) InstallGatewayFlows() error {
}

// Add flow to ensure the liveness check packet could be forwarded correctly.
flows = append(flows, c.featurePodConnectivity.localProbeFlow(c.ovsDatapathType)...)
flows = append(flows, c.featurePodConnectivity.localProbeFlows(c.ovsDatapathType, c.proxyAll)...)
flows = append(flows, c.featurePodConnectivity.l3FwdFlowToGateway()...)

if err := c.ofEntryOperations.AddAll(flows); err != nil {
Expand Down
3 changes: 3 additions & 0 deletions pkg/agent/openflow/fields.go
Expand Up @@ -163,6 +163,9 @@ var (
// This CT mark is used in CtZone / CtZoneV6 and SNATCtZone / SNATCtZoneV6.
ServiceCTMark = binding.NewOneBitCTMark(4)

// CTMark[4]: Mark to indicate the connection is not for Service.
NotServiceCTMark = binding.NewOneBitZeroCTMark(4)

// CTMark[5]: Mark to indicate SNAT should be performed on the connection for Service.
// This CT mark is only used in CtZone / CtZoneV6.
ConnSNATCTMark = binding.NewOneBitCTMark(5)
Expand Down
25 changes: 15 additions & 10 deletions pkg/agent/openflow/pipeline.go
Expand Up @@ -2060,26 +2060,31 @@ func (f *featureNetworkPolicy) dnsPacketInFlow(conjunctionID uint32) binding.Flo
Done()
}

// localProbeFlow generates the flow to forward locally generated packets to ConntrackCommitTable, bypassing ingress
// localProbeFlows generates the flows to forward locally generated packets to stageConntrack directly, bypassing ingress
// rules of Network Policies. The packets are sent by kubelet to probe the liveness/readiness of local Pods.
// On Linux and when OVS kernel datapath is used, it identifies locally generated packets by matching the
// HostLocalSourceMark, otherwise it matches the source IP. The difference is because:
// On Linux and when OVS kernel datapath is used, the probe packets are identified by matching the HostLocalSourceMark.
// On Windows or when OVS userspace (netdev) datapath is used, we need a different approach because:
// 1. On Windows, kube-proxy userspace mode is used, and currently there is no way to distinguish kubelet generated
// traffic from kube-proxy proxied traffic.
// 2. pkt_mark field is not properly supported for OVS userspace (netdev) datapath.
// Note that there is a defect in the latter way that NodePort Service access by external clients will be masqueraded as
// a local gateway IP to bypass Network Policies. See https://github.com/antrea-io/antrea/issues/280.
// TODO: Fix it after replacing kube-proxy with AntreaProxy.
func (f *featurePodConnectivity) localProbeFlow(ovsDatapathType ovsconfig.OVSDatapathType) []binding.Flow {
// when proxyAll is disabled, the probe packets are identified by matching the source IP is the Antrea gateway IP;
// otherwise, the packets are identified by matching both the Antrea gateway IP and NotServiceCTMark. Note that, when
// proxyAll is disabled, currently there is no way to distinguish kubelet generated traffic from kube-proxy proxied traffic
// only by matching the Antrea gateway IP. There is a defect that NodePort Service access by external clients will be
// masqueraded as the Antrea gateway IP to bypass Network Policies. See https://github.com/antrea-io/antrea/issues/280.
func (f *featurePodConnectivity) localProbeFlows(ovsDatapathType ovsconfig.OVSDatapathType, proxyAll bool) []binding.Flow {
cookieID := f.cookieAllocator.Request(f.category).Raw()
var flows []binding.Flow
if runtime.IsWindowsPlatform() || ovsDatapathType == ovsconfig.OVSDatapathNetdev {
for ipProtocol, gatewayIP := range f.gatewayIPs {
flows = append(flows, IngressSecurityClassifierTable.ofTable.BuildFlow(priorityHigh).
fb := IngressSecurityClassifierTable.ofTable.BuildFlow(priorityHigh).
Cookie(cookieID).
MatchProtocol(ipProtocol).
MatchSrcIP(gatewayIP).
Action().GotoStage(stageConntrack).
MatchSrcIP(gatewayIP)
if proxyAll {
fb = fb.MatchCTMark(NotServiceCTMark)
}
flows = append(flows, fb.Action().GotoStage(stageConntrack).
Done())
}
} else {
Expand Down

0 comments on commit 863b6ba

Please sign in to comment.