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

With catchALL honor "except" #58

Merged
merged 8 commits into from
Sep 21, 2023
Merged
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
63 changes: 32 additions & 31 deletions pkg/ebpf/bpf_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -739,43 +739,44 @@ func (l *bpfClient) computeMapEntriesFromEndpointRules(firewallRules []EbpfFirew
firewallRule.IPCidr += v1alpha1.NetworkAddress(l.hostMask)
}

if utils.IsCatchAllIPEntry(string(firewallRule.IPCidr)) ||
utils.IsNodeIP(l.nodeIP, string(firewallRule.IPCidr)) {
if utils.IsNodeIP(l.nodeIP, string(firewallRule.IPCidr)) {
continue
}

if len(firewallRule.L4Info) == 0 {
l.logger.Info("No L4 specified. Add Catch all entry: ", "CIDR: ", firewallRule.IPCidr)
l.addCatchAllL4Entry(&firewallRule)
l.logger.Info("Total L4 entries ", "count: ", len(firewallRule.L4Info))
}
if utils.IsNonHostCIDR(string(firewallRule.IPCidr)) {
if existingL4Info, ok := nonHostCIDRs[string(firewallRule.IPCidr)]; ok {
firewallRule.L4Info = append(firewallRule.L4Info, existingL4Info...)
}
nonHostCIDRs[string(firewallRule.IPCidr)] = firewallRule.L4Info
} else {
if existingL4Info, ok := ipCIDRs[string(firewallRule.IPCidr)]; ok {
firewallRule.L4Info = append(firewallRule.L4Info, existingL4Info...)
if !utils.IsCatchAllIPEntry(string(firewallRule.IPCidr)) {
if len(firewallRule.L4Info) == 0 {
l.logger.Info("No L4 specified. Add Catch all entry: ", "CIDR: ", firewallRule.IPCidr)
l.addCatchAllL4Entry(&firewallRule)
l.logger.Info("Total L4 entries ", "count: ", len(firewallRule.L4Info))
}
// Check if the /32 entry is part of any non host CIDRs that we've encountered so far
// If found, we need to include the port and protocol combination against the current entry as well since
// we use LPM TRIE map and the /32 will always win out.
cidrL4Info = l.checkAndDeriveL4InfoFromAnyMatchingCIDRs(string(firewallRule.IPCidr), nonHostCIDRs)
if len(cidrL4Info) > 0 {
firewallRule.L4Info = append(firewallRule.L4Info, cidrL4Info...)
if utils.IsNonHostCIDR(string(firewallRule.IPCidr)) {
if existingL4Info, ok := nonHostCIDRs[string(firewallRule.IPCidr)]; ok {
firewallRule.L4Info = append(firewallRule.L4Info, existingL4Info...)
}
nonHostCIDRs[string(firewallRule.IPCidr)] = firewallRule.L4Info
} else {
if existingL4Info, ok := ipCIDRs[string(firewallRule.IPCidr)]; ok {
firewallRule.L4Info = append(firewallRule.L4Info, existingL4Info...)
}
// Check if the /32 entry is part of any non host CIDRs that we've encountered so far
// If found, we need to include the port and protocol combination against the current entry as well since
// we use LPM TRIE map and the /32 will always win out.
cidrL4Info = l.checkAndDeriveL4InfoFromAnyMatchingCIDRs(string(firewallRule.IPCidr), nonHostCIDRs)
if len(cidrL4Info) > 0 {
firewallRule.L4Info = append(firewallRule.L4Info, cidrL4Info...)
}
ipCIDRs[string(firewallRule.IPCidr)] = firewallRule.L4Info
}
ipCIDRs[string(firewallRule.IPCidr)] = firewallRule.L4Info
}
//Include port and protocol combination paired with catch all entries
firewallRule.L4Info = append(firewallRule.L4Info, catchAllIPPorts...)
//Include port and protocol combination paired with catch all entries
firewallRule.L4Info = append(firewallRule.L4Info, catchAllIPPorts...)

l.logger.Info("Updating Map with ", "IP Key:", firewallRule.IPCidr)
_, mapKey, _ = net.ParseCIDR(string(firewallRule.IPCidr))
// Key format: Prefix length (4 bytes) followed by 4/16byte IP address
key = utils.ComputeTrieKey(*mapKey, l.enableIPv6)
value = utils.ComputeTrieValue(firewallRule.L4Info, l.logger, allowAll, false)
mapEntries[string(key)] = uintptr(unsafe.Pointer(&value[0]))
l.logger.Info("Updating Map with ", "IP Key:", firewallRule.IPCidr)
_, mapKey, _ = net.ParseCIDR(string(firewallRule.IPCidr))
// Key format: Prefix length (4 bytes) followed by 4/16byte IP address
key = utils.ComputeTrieKey(*mapKey, l.enableIPv6)
value = utils.ComputeTrieValue(firewallRule.L4Info, l.logger, allowAll, false)
mapEntries[string(key)] = uintptr(unsafe.Pointer(&value[0]))
}
if firewallRule.Except != nil {
for _, exceptCIDR := range firewallRule.Except {
_, mapKey, _ = net.ParseCIDR(string(exceptCIDR))
Expand Down
Loading