Skip to content

Commit 9205f93

Browse files
committed
fix(cilium): keep the host mark on host traffic to a local pod
Since v1.20.0 the bpf_host SNAT conflict check fires for any packet carrying the host mark on every device bpf_host runs on, and the SNAT path replaces that mark with SNAT_DONE. With kube-ovn chaining a host connection to a pod on the same node leaves through ovn0, so the pod's to-container program sees SNAT_DONE and classifies the source as world instead of host. The implicit localhost allow then stops matching: kubelet probes fail under any default-deny ingress policy that does not admit world, and a world deny drops the API server calling a webhook on its own node. Carry a verbatim backport of cilium/cilium#49232, which skips the conflict check when the destination is a local endpoint: such traffic never leaves the node and cannot collide with pod egress SNAT. Keeping the hunks identical to upstream lets the patch be dropped as soon as a shipped cilium release contains the fix. Without chaining, host traffic to a local pod does not pass to-netdev and nothing changes. Assisted-by: LLM Signed-off-by: Aleksei Sviridkin <[email protected]>
1 parent c341152 commit 9205f93

3 files changed

Lines changed: 69 additions & 5 deletions

File tree

‎packages/system/cilium/Makefile‎

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,8 @@ update:
3737
{ echo 'ERROR: perl patch did not remove the upstream AppArmor block from cilium-agent/daemonset.yaml (upstream template format may have changed)' >&2; exit 1; }
3838
version=$$(awk '$$1 == "version:" {print $$2}' charts/cilium/Chart.yaml) && \
3939
$(SED_INPLACE) "s/ARG VERSION=.*/ARG VERSION=v$${version}/" images/cilium/Dockerfile
40-
# Verify the IPv6-passthrough BPF patch still applies to the new upstream tag,
41-
# so the bump author learns about drift immediately instead of at image build.
40+
# Verify the BPF patches still apply to the new upstream tag, so the bump
41+
# author learns about drift immediately instead of at image build.
4242
version=$$(awk '$$1 == "version:" {print $$2}' charts/cilium/Chart.yaml) && \
4343
tmpdir=$$(mktemp -d) && \
4444
{ curl --fail --silent --show-error --location \
@@ -48,6 +48,14 @@ update:
4848
< images/cilium/patches/pass-ipv6-to-stack-when-ipv6-datapath-disabled.diff || \
4949
{ echo "ERROR: images/cilium/patches/pass-ipv6-to-stack-when-ipv6-datapath-disabled.diff no longer applies to upstream bpf/bpf_host.c v$${version}; regenerate it for the new cilium version" >&2; \
5050
rm -rf "$$tmpdir"; exit 1; }; } && \
51+
mkdir -p "$$tmpdir/lib" && \
52+
{ curl --fail --silent --show-error --location \
53+
"https://raw.githubusercontent.com/cilium/cilium/v$${version}/bpf/lib/nodeport_egress.h" \
54+
-o "$$tmpdir/lib/nodeport_egress.h" || { rm -rf "$$tmpdir"; exit 1; }; } && \
55+
{ patch --dry-run --batch --forward --fuzz=0 -p1 -d "$$tmpdir" \
56+
< images/cilium/patches/skip-snat-conflict-for-local-destinations.diff || \
57+
{ echo "ERROR: images/cilium/patches/skip-snat-conflict-for-local-destinations.diff no longer applies to upstream bpf/lib/nodeport_egress.h v$${version}; regenerate it for the new cilium version" >&2; \
58+
rm -rf "$$tmpdir"; exit 1; }; } && \
5159
rm -rf "$$tmpdir"
5260

5361
image:

‎packages/system/cilium/images/cilium/Dockerfile‎

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,10 @@ ARG VERSION=v1.20.2@sha256:2939231d0d3e3ebddcd80fffa168b7ddcc78fdf0dc864d1c8c126
22

33
FROM quay.io/cilium/cilium:${VERSION} AS upstream
44

5-
# The upstream runtime image ships no `patch`; apply the BPF patch in a
6-
# throwaway alpine stage and copy the result back over the upstream image.
5+
# The upstream runtime image ships no `patch`; apply the BPF patches in a
6+
# throwaway alpine stage and copy the results back over the upstream image.
77
# The agent compiles BPF at startup from /var/lib/cilium/bpf, so replacing
8-
# the source file is sufficient.
8+
# the source files is sufficient.
99
FROM docker.io/library/alpine:3.22@sha256:5291449c3df73caf6ed85e649dec1b9e818b39a5d8c871e97afc13e9cd5e8fa8 AS patcher
1010
RUN apk add --no-cache patch
1111
WORKDIR /work
@@ -20,6 +20,16 @@ RUN patch --batch --forward --fuzz=0 -p1 < /patches/pass-ipv6-to-stack-when-ipv6
2020
RUN test "$(grep -c 'cozystack-ipv6-passthrough' bpf_host.c)" = 3 || { \
2121
echo 'ERROR: expected exactly 3 cozystack-ipv6-passthrough markers in bpf_host.c' >&2; \
2222
exit 1; }
23+
COPY --from=upstream /var/lib/cilium/bpf/lib/nodeport_egress.h ./lib/nodeport_egress.h
24+
COPY patches/skip-snat-conflict-for-local-destinations.diff /patches/
25+
RUN patch --batch --forward --fuzz=0 -p1 < /patches/skip-snat-conflict-for-local-destinations.diff || { \
26+
echo 'ERROR: the cilium/cilium#49232 backport no longer applies to bpf/lib/nodeport_egress.h.' >&2; \
27+
echo 'If the new tag already carries cilium/cilium#49232, drop the patch; otherwise regenerate it.' >&2; \
28+
exit 1; }
29+
RUN test "$(grep -c -e 'See comment in nodeport_has_nat_conflict_ipv4' -e 'Host traffic to a local endpoint never leaves the node' lib/nodeport_egress.h)" = 2 || { \
30+
echo 'ERROR: expected both cilium/cilium#49232 hunks in nodeport_egress.h' >&2; \
31+
exit 1; }
2332

2433
FROM upstream
2534
COPY --from=patcher /work/bpf_host.c /var/lib/cilium/bpf/bpf_host.c
35+
COPY --from=patcher /work/lib/nodeport_egress.h /var/lib/cilium/bpf/lib/nodeport_egress.h
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
Skip NodePort SNAT conflict handling for host traffic to a local endpoint.
2+
3+
The hunks of https://github.com/cilium/cilium/pull/49232 (commit
4+
e0462275b8164463ae9b473b32718a64645cda6d), verbatim, applied to cilium
5+
v1.20.2 bpf/lib/nodeport_egress.h. Upstream commit 990f79be17
6+
("bpf,nodeport: generalize SNAT conflict detection") runs the conflict path
7+
for any host-marked packet on every bpf_host device, and that path replaces
8+
MARK_MAGIC_HOST with MARK_MAGIC_SNAT_DONE. With kube-ovn chaining, host
9+
traffic to a local pod leaves through ovn0, so the pod sees it as world:
10+
kubelet probes fail under default-deny ingress policies and a world deny
11+
drops the API server calling a webhook on its own node. Upstream report:
12+
https://github.com/cilium/cilium/issues/49208, downstream tracking:
13+
https://github.com/cozystack/cozystack/issues/4781.
14+
15+
Drop this patch once a cilium release we ship carries cilium/cilium#49232.
16+
Until then `make update` dry-runs it against the new tag.
17+
18+
--- a/lib/nodeport_egress.h
19+
+++ b/lib/nodeport_egress.h
20+
@@ -46,6 +46,10 @@
21+
* already reserved.
22+
*/
23+
if (magic == MARK_MAGIC_HOST || (ep && (ep->flags & ENDPOINT_F_HOST))) {
24+
+ /* See comment in nodeport_has_nat_conflict_ipv4(). */
25+
+ if (__lookup_ip6_endpoint((union v6addr *)&ip6->daddr))
26+
+ return false;
27+
+
28+
ipv6_addr_copy(&target->addr, (union v6addr *)&ip6->saddr);
29+
target->needs_ct = true;
30+
return true;
31+
@@ -314,6 +318,15 @@
32+
* already reserved.
33+
*/
34+
if (magic == MARK_MAGIC_HOST || (ep && (ep->flags & ENDPOINT_F_HOST))) {
35+
+ /* Host traffic to a local endpoint never leaves the node, so it
36+
+ * cannot collide with egress SNAT and must keep MARK_MAGIC_HOST.
37+
+ * The SNAT path would replace that mark with MARK_MAGIC_SNAT_DONE,
38+
+ * and with CNI chaining the destination endpoint would then see
39+
+ * the source as world.
40+
+ */
41+
+ if (__lookup_ip4_endpoint(ip4->daddr))
42+
+ return false;
43+
+
44+
target->addr = ip4->saddr;
45+
target->needs_ct = true;
46+
return true;

0 commit comments

Comments
 (0)