Skip to content

Commit 8a8815d

Browse files
committed
Clarify rules; correct error in SRC_NAT
1 parent 3fc848f commit 8a8815d

File tree

1 file changed

+17
-16
lines changed

1 file changed

+17
-16
lines changed

probe/endpoint/nat.go

Lines changed: 17 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -44,40 +44,40 @@ Pod to pod via Kubernetes service
4444
picked up by ebpf as 10.32.0.16:47600->10.105.173.176:5432 and 10.32.0.6:5432 (??)
4545
NAT IPS_DST_NAT orig: 10.32.0.16:47600->10.105.173.176:5432, reply: 10.32.0.6:5432->10.32.0.16:47600
4646
We want: 10.32.0.16:47600->10.32.0.6:5432
47-
- replace the destination (== NAT orig dst) with the NAT reply source
47+
- replace the destination (== NAT orig dst) with the NAT reply source (A)
4848
4949
Incoming from outside the cluster to a NodePort:
5050
picked up by ebpf as 10.32.0.1:13488->10.32.0.7:80
5151
NAT: IPS_SRC_NAT IPS_DST_NAT orig: 37.157.33.76:13488->172.31.2.17:30081, reply: 10.32.0.7:80->10.32.0.1:13488
5252
We want: 37.157.33.76:13488->10.32.0.7:80
53-
- replace the source (== NAT reply dst) with the NAT original source
53+
- replace the source (== NAT reply dst) with the NAT original source (B)
5454
To match another probe with the other side of this connection, also want 37.157.33.76:13488->172.31.2.17:30081
55-
- add NAT original dst as a copy of nat reply dst
55+
- add NAT original dst as a copy of nat reply source (C)
5656
5757
Outgoing from a pod:
5858
picked up by ebpf as 10.32.0.7:36078->18.221.99.178:443
5959
NAT: IPS_SRC_NAT orig: 10.32.0.7:36078->18.221.99.178:443, reply: 18.221.99.178:443->172.31.2.17:36078
6060
We want: 10.32.0.7:36078->18.221.99.178:443
61-
- leave it alone.
61+
- leave it alone. (D)
6262
6363
Docker container exposing port to similar on different host
6464
host1:
6565
picked up by ebpf as ip-172-31-5-80;172.17.0.2:43042->172.31.2.17:8080
6666
NAT: IPS_SRC_NAT orig: 172.17.0.2:43042->172.31.2.17:8080, reply: 172.31.2.17:8080-> 172.31.5.80:43042
6767
We want: 172.31.5.80:43042->172.31.2.17:8080
68-
- can't have a blanket rule to replace NAT original source with NAT reply destination, because that breaks the "Outgoing from a pod" case
69-
we could add 172.31.5.80:43042 (nat reply destination) as a copy of ip-172-31-5-80;172.17.0.2:43042 (nat orig source)
68+
- can't have a blanket rule to replace NAT original source with NAT reply destination, because that breaks case D.
69+
we could add 172.31.5.80:43042 (nat reply destination) as a copy of ip-172-31-5-80;172.17.0.2:43042 (nat orig source) (E)
7070
host2:
7171
picked up by ebpf as 172.31.5.80:43042->ip-172-31-2-17;172.17.0.2:80
7272
NAT: IPS_DST_NAT orig: 172.31.5.80:43042->172.31.2.17:8080, reply: 172.17.0.2:80->172.31.5.80:43042
7373
Ideally we might want: ip-172-31-5-80;172.17.0.2:43042->ip-172-31-2-17;172.17.0.2:80
74-
we could add 172.31.2.17:8080 (nat original destination) as a copy of ip-172-31-2-17;172.17.0.2:80 (nat reply source)
74+
we could add 172.31.2.17:8080 (nat original destination) as a copy of ip-172-31-2-17;172.17.0.2:80 (nat reply source) (F)
7575
7676
All of the above can be satisfied by these rules:
77-
For SRC_NAT either add NAT orig source as a copy of NAT reply destination
78-
or add NAT reply destination as a copy of NAT original source
79-
For DST_NAT replace NAT original destination in adjacencies with the NAT reply source
80-
or add nat original destination as a copy of nat reply source
77+
For SRC_NAT either add NAT original destination as a copy of NAT reply source (C)
78+
or add NAT reply destination as a copy of NAT original source (E)
79+
For DST_NAT replace NAT original destination in adjacencies with the NAT reply source (A),(B)
80+
or add nat original destination as a copy of nat reply source (F)
8181
*/
8282

8383
// applyNAT modifies Nodes in the endpoint topology of a report, based on
@@ -89,14 +89,15 @@ func (n natMapper) applyNAT(rpt report.Report, scope string) {
8989
replyDstID := endpointNodeID(scope, f.Reply.Dst, f.Reply.DstPort)
9090
origSrcID := endpointNodeID(scope, f.Orig.Src, f.Orig.SrcPort)
9191
if replyDstID != origSrcID {
92-
// either add NAT orig source as a copy of NAT reply destination
92+
// either add NAT original destination as a copy of NAT reply destination (C)
9393
if replyDstNode, ok := rpt.Endpoint.Nodes[replyDstID]; ok {
94-
newNode := replyDstNode.WithID(origSrcID).WithLatests(map[string]string{
94+
origDstID := endpointNodeID(scope, f.Orig.Dst, f.Orig.DstPort)
95+
newNode := replyDstNode.WithID(origDstID).WithLatests(map[string]string{
9596
CopyOf: replyDstID,
9697
})
9798
rpt.Endpoint.AddNode(newNode)
9899
} else if origSrcNode, ok := rpt.Endpoint.Nodes[origSrcID]; ok {
99-
// or add NAT reply destination as a copy of NAT original source
100+
// or add NAT reply destination as a copy of NAT original source (E)
100101
newNode := origSrcNode.WithID(replyDstID).WithLatests(map[string]string{
101102
CopyOf: origSrcID,
102103
})
@@ -116,12 +117,12 @@ func (n natMapper) applyNAT(rpt report.Report, scope string) {
116117
}
117118

118119
if fromNode.Adjacency.Contains(origDstID) {
119-
// replace destination with reply source
120+
// replace destination with reply source (A),(B)
120121
fromNode.Adjacency = fromNode.Adjacency.Minus(origDstID)
121122
fromNode = fromNode.WithAdjacent(replySrcID)
122123
rpt.Endpoint.Nodes[fromID] = fromNode
123124
} else {
124-
// add nat original destination as a copy of nat reply source
125+
// add nat original destination as a copy of nat reply source (F)
125126
replySrcNode, ok := rpt.Endpoint.Nodes[replySrcID]
126127
if !ok {
127128
replySrcNode = report.MakeNode(replySrcID)

0 commit comments

Comments
 (0)