e0e362d700
SDK Tests / changes (push) Successful in 2m29s
Real E2E Tests / changes (push) Successful in 2m29s
Deploy Docs Pages / build (push) Has been cancelled
Deploy Docs Pages / deploy (push) Has been cancelled
Real E2E Tests / JavaScript E2E (docker bridge) (push) Has been cancelled
Real E2E Tests / Python E2E (docker bridge) (push) Has been cancelled
Real E2E Tests / Java E2E (docker bridge) (push) Has been cancelled
Real E2E Tests / C# E2E (docker bridge) (push) Has been cancelled
Real E2E Tests / Go E2E (docker bridge) (push) Has been cancelled
Real E2E Tests / Real E2E CI (push) Has been cancelled
SDK Tests / SDK CI (push) Has been cancelled
SDK Tests / CLI Tests (push) Has been cancelled
SDK Tests / Python SDK Quality (code-interpreter) (push) Has been cancelled
SDK Tests / Python SDK Quality (sandbox) (push) Has been cancelled
SDK Tests / Python SDK Tests (code-interpreter) (push) Has been cancelled
SDK Tests / JavaScript SDK Quality And Tests (code-interpreter) (push) Has been cancelled
SDK Tests / JavaScript SDK Quality And Tests (sandbox) (push) Has been cancelled
SDK Tests / Python SDK Tests (sandbox) (push) Has been cancelled
SDK Tests / CLI Quality (push) Has been cancelled
SDK Tests / Kotlin SDK Quality And Tests (sandbox) (push) Has been cancelled
SDK Tests / Kotlin SDK Quality And Tests (code-interpreter) (push) Has been cancelled
SDK Tests / C# SDK Quality And Tests (code-interpreter) (push) Has been cancelled
SDK Tests / C# SDK Quality And Tests (sandbox) (push) Has been cancelled
SDK Tests / Go SDK Quality And Tests (push) Has been cancelled
113 lines
2.9 KiB
Go
113 lines
2.9 KiB
Go
// Copyright 2026 Alibaba Group Holding Ltd.
|
|
//
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
// you may not use this file except in compliance with the License.
|
|
// You may obtain a copy of the License at
|
|
//
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
//
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
// See the License for the specific language governing permissions and
|
|
// limitations under the License.
|
|
|
|
package nftables
|
|
|
|
import (
|
|
"fmt"
|
|
"net/netip"
|
|
)
|
|
|
|
// normalizeNFTIntervalSet drops entries that are redundant for nftables sets with
|
|
// `flags interval`: a host or smaller CIDR that falls strictly inside another
|
|
// listed CIDR would make `nft add element` fail with "conflicting intervals specified".
|
|
// Used on every ApplyStatic (startup and /policy updates).
|
|
func normalizeNFTIntervalSet(elems []string) ([]string, error) {
|
|
if len(elems) == 0 {
|
|
return nil, nil
|
|
}
|
|
prefs := make([]netip.Prefix, 0, len(elems))
|
|
for _, s := range elems {
|
|
p, err := parseAsPrefix(s)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
prefs = append(prefs, p.Masked())
|
|
}
|
|
prefs = uniquePrefixes(prefs)
|
|
prefs = removeStrictSubnets(prefs)
|
|
out := make([]string, 0, len(prefs))
|
|
for _, p := range prefs {
|
|
out = append(out, formatPrefixForNFT(p))
|
|
}
|
|
return out, nil
|
|
}
|
|
|
|
func parseAsPrefix(s string) (netip.Prefix, error) {
|
|
if p, err := netip.ParsePrefix(s); err == nil {
|
|
return p, nil
|
|
}
|
|
if a, err := netip.ParseAddr(s); err == nil {
|
|
if a.Is4() {
|
|
return a.Prefix(32)
|
|
}
|
|
return a.Prefix(128)
|
|
}
|
|
return netip.Prefix{}, fmt.Errorf("nftables: invalid IP or CIDR %q", s)
|
|
}
|
|
|
|
func uniquePrefixes(prefs []netip.Prefix) []netip.Prefix {
|
|
seen := make(map[string]struct{}, len(prefs))
|
|
out := make([]netip.Prefix, 0, len(prefs))
|
|
for _, p := range prefs {
|
|
k := p.String()
|
|
if _, ok := seen[k]; ok {
|
|
continue
|
|
}
|
|
seen[k] = struct{}{}
|
|
out = append(out, p)
|
|
}
|
|
return out
|
|
}
|
|
|
|
// strictSupernet reports whether super covers all addresses of sub, with super
|
|
// strictly larger (smaller prefix length) than sub.
|
|
func strictSupernet(super, sub netip.Prefix) bool {
|
|
if super.Addr().Is4() != sub.Addr().Is4() {
|
|
return false
|
|
}
|
|
if super.Bits() >= sub.Bits() {
|
|
return false
|
|
}
|
|
return super.Contains(sub.Addr())
|
|
}
|
|
|
|
func removeStrictSubnets(prefs []netip.Prefix) []netip.Prefix {
|
|
out := make([]netip.Prefix, 0, len(prefs))
|
|
for _, p := range prefs {
|
|
redundant := false
|
|
for _, q := range prefs {
|
|
if strictSupernet(q, p) {
|
|
redundant = true
|
|
break
|
|
}
|
|
}
|
|
if !redundant {
|
|
out = append(out, p)
|
|
}
|
|
}
|
|
return out
|
|
}
|
|
|
|
func formatPrefixForNFT(p netip.Prefix) string {
|
|
p = p.Masked()
|
|
if p.Addr().Is4() && p.Bits() == 32 {
|
|
return p.Addr().String()
|
|
}
|
|
if p.Addr().Is6() && p.Bits() == 128 {
|
|
return p.Addr().String()
|
|
}
|
|
return p.String()
|
|
}
|