You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
330 lines
9.2 KiB
Go
330 lines
9.2 KiB
Go
package networkobserve
|
|
|
|
import (
|
|
"errors"
|
|
"net/netip"
|
|
)
|
|
|
|
const (
|
|
HardMaxFlowsPerWindow = 1_000_000
|
|
HardMaxUniqueRemoteIPs = 65_536
|
|
HardMaxUniqueRemotePorts = 65_536
|
|
HardMaxUniqueLocalPorts = 65_536
|
|
HardMaxRelations = 131_072
|
|
)
|
|
|
|
type Limits struct {
|
|
MaxFlows int
|
|
MaxUniqueRemoteIPs int
|
|
MaxUniqueRemotePorts int
|
|
MaxUniqueLocalPorts int
|
|
MaxRelations int
|
|
}
|
|
|
|
func DefaultLimits() Limits {
|
|
return Limits{
|
|
MaxFlows: 250_000, MaxUniqueRemoteIPs: 32_768,
|
|
MaxUniqueRemotePorts: 65_536, MaxUniqueLocalPorts: 65_536,
|
|
MaxRelations: 65_536,
|
|
}
|
|
}
|
|
|
|
func (limits Limits) Validate() error {
|
|
if limits.MaxFlows <= 0 || limits.MaxFlows > HardMaxFlowsPerWindow ||
|
|
limits.MaxUniqueRemoteIPs <= 0 || limits.MaxUniqueRemoteIPs > HardMaxUniqueRemoteIPs ||
|
|
limits.MaxUniqueRemotePorts <= 0 || limits.MaxUniqueRemotePorts > HardMaxUniqueRemotePorts ||
|
|
limits.MaxUniqueLocalPorts <= 0 || limits.MaxUniqueLocalPorts > HardMaxUniqueLocalPorts ||
|
|
limits.MaxRelations <= 0 || limits.MaxRelations > HardMaxRelations {
|
|
return errors.New("network observation limits are outside hard bounds")
|
|
}
|
|
return nil
|
|
}
|
|
|
|
type Direction uint8
|
|
|
|
const (
|
|
DirectionInbound Direction = iota + 1
|
|
DirectionOutbound
|
|
)
|
|
|
|
type Protocol uint8
|
|
|
|
const (
|
|
ProtocolTCP Protocol = iota + 1
|
|
ProtocolUDP
|
|
ProtocolOther
|
|
)
|
|
|
|
type Flow struct {
|
|
IPv6 bool
|
|
Direction Direction
|
|
Protocol Protocol
|
|
RemoteIP netip.Addr
|
|
// LocalPort is the VM-side port. For inbound traffic this is the target
|
|
// service port; RemotePort is the peer's source port. For outbound traffic
|
|
// RemotePort is the target service port and LocalPort is the VM source port.
|
|
LocalPort uint16
|
|
RemotePort uint16
|
|
New bool
|
|
SYN bool
|
|
Unreplied bool
|
|
}
|
|
|
|
type aggregateKey struct {
|
|
ipv6 bool
|
|
direction Direction
|
|
}
|
|
|
|
type flowAggregate struct {
|
|
summary FlowSummary
|
|
remoteIPs map[netip.Addr]struct{}
|
|
remotePorts map[uint16]struct{}
|
|
localPorts map[uint16]struct{}
|
|
remotePortsByIP map[netip.Addr]map[uint16]struct{}
|
|
remoteIPsByRemotePort map[uint16]map[netip.Addr]struct{}
|
|
localPortsByRemoteIP map[netip.Addr]map[uint16]struct{}
|
|
remoteIPsByLocalPort map[uint16]map[netip.Addr]struct{}
|
|
remoteRelations int
|
|
localRelations int
|
|
quality map[QualityFlag]struct{}
|
|
}
|
|
|
|
type Aggregator struct {
|
|
limits Limits
|
|
flows int
|
|
byScope map[aggregateKey]*flowAggregate
|
|
quality map[QualityFlag]struct{}
|
|
}
|
|
|
|
func NewAggregator(limits Limits) (*Aggregator, error) {
|
|
if err := limits.Validate(); err != nil {
|
|
return nil, err
|
|
}
|
|
return &Aggregator{
|
|
limits: limits, byScope: make(map[aggregateKey]*flowAggregate),
|
|
quality: make(map[QualityFlag]struct{}),
|
|
}, nil
|
|
}
|
|
|
|
func (a *Aggregator) Add(flow Flow) {
|
|
if a.flows >= a.limits.MaxFlows {
|
|
a.quality[QualityFlowLimitReached] = struct{}{}
|
|
a.quality[QualityIncomplete] = struct{}{}
|
|
return
|
|
}
|
|
if flow.Direction != DirectionInbound && flow.Direction != DirectionOutbound {
|
|
a.quality[QualityIncomplete] = struct{}{}
|
|
return
|
|
}
|
|
if !flow.RemoteIP.IsValid() {
|
|
a.quality[QualityIncomplete] = struct{}{}
|
|
return
|
|
}
|
|
flow.RemoteIP = flow.RemoteIP.Unmap()
|
|
if flow.RemoteIP.Is6() != flow.IPv6 {
|
|
a.quality[QualityIncomplete] = struct{}{}
|
|
return
|
|
}
|
|
if flow.Protocol != ProtocolTCP && flow.Protocol != ProtocolUDP && flow.Protocol != ProtocolOther {
|
|
flow.Protocol = ProtocolOther
|
|
}
|
|
a.flows++
|
|
key := aggregateKey{ipv6: flow.IPv6, direction: flow.Direction}
|
|
scope := a.byScope[key]
|
|
if scope == nil {
|
|
scope = &flowAggregate{
|
|
remoteIPs: make(map[netip.Addr]struct{}), remotePorts: make(map[uint16]struct{}),
|
|
localPorts: make(map[uint16]struct{}),
|
|
remotePortsByIP: make(map[netip.Addr]map[uint16]struct{}),
|
|
remoteIPsByRemotePort: make(map[uint16]map[netip.Addr]struct{}),
|
|
localPortsByRemoteIP: make(map[netip.Addr]map[uint16]struct{}),
|
|
remoteIPsByLocalPort: make(map[uint16]map[netip.Addr]struct{}),
|
|
quality: make(map[QualityFlag]struct{}),
|
|
}
|
|
a.byScope[key] = scope
|
|
}
|
|
switch flow.Protocol {
|
|
case ProtocolTCP:
|
|
scope.summary.TCPActiveFlows++
|
|
if flow.New {
|
|
scope.summary.TCPNewFlows++
|
|
}
|
|
if flow.SYN {
|
|
scope.summary.TCPSYNFlows++
|
|
}
|
|
if flow.Unreplied {
|
|
scope.summary.TCPUnrepliedFlows++
|
|
}
|
|
case ProtocolUDP:
|
|
scope.summary.UDPActiveFlows++
|
|
if flow.New {
|
|
scope.summary.UDPNewFlows++
|
|
}
|
|
default:
|
|
scope.summary.OtherActiveFlows++
|
|
}
|
|
scope.addCardinality(flow.RemoteIP, flow.LocalPort, flow.RemotePort, a.limits)
|
|
}
|
|
|
|
func (scope *flowAggregate) addCardinality(remoteIP netip.Addr, localPort, remotePort uint16, limits Limits) {
|
|
ipRecorded := !remoteIP.IsValid()
|
|
if remoteIP.IsValid() {
|
|
if _, exists := scope.remoteIPs[remoteIP]; exists {
|
|
ipRecorded = true
|
|
} else {
|
|
if len(scope.remoteIPs) < limits.MaxUniqueRemoteIPs {
|
|
scope.remoteIPs[remoteIP] = struct{}{}
|
|
ipRecorded = true
|
|
} else {
|
|
scope.quality[QualityRemoteIPCardinality] = struct{}{}
|
|
scope.quality[QualityIncomplete] = struct{}{}
|
|
}
|
|
}
|
|
}
|
|
portRecorded := remotePort == 0
|
|
if remotePort != 0 {
|
|
if _, exists := scope.remotePorts[remotePort]; exists {
|
|
portRecorded = true
|
|
} else {
|
|
if len(scope.remotePorts) < limits.MaxUniqueRemotePorts {
|
|
scope.remotePorts[remotePort] = struct{}{}
|
|
portRecorded = true
|
|
} else {
|
|
scope.quality[QualityRemotePortCardinality] = struct{}{}
|
|
scope.quality[QualityIncomplete] = struct{}{}
|
|
}
|
|
}
|
|
}
|
|
localPortRecorded := localPort == 0
|
|
if localPort != 0 {
|
|
if _, exists := scope.localPorts[localPort]; exists {
|
|
localPortRecorded = true
|
|
} else if len(scope.localPorts) < limits.MaxUniqueLocalPorts {
|
|
scope.localPorts[localPort] = struct{}{}
|
|
localPortRecorded = true
|
|
} else {
|
|
scope.quality[QualityLocalPortCardinality] = struct{}{}
|
|
scope.quality[QualityIncomplete] = struct{}{}
|
|
}
|
|
}
|
|
if ipRecorded && portRecorded && remotePort != 0 {
|
|
ports := scope.remotePortsByIP[remoteIP]
|
|
if ports == nil || !containsPort(ports, remotePort) {
|
|
if scope.remoteRelations >= limits.MaxRelations {
|
|
scope.markRelationSaturated()
|
|
} else {
|
|
if ports == nil {
|
|
ports = make(map[uint16]struct{})
|
|
scope.remotePortsByIP[remoteIP] = ports
|
|
}
|
|
ports[remotePort] = struct{}{}
|
|
ips := scope.remoteIPsByRemotePort[remotePort]
|
|
if ips == nil {
|
|
ips = make(map[netip.Addr]struct{})
|
|
scope.remoteIPsByRemotePort[remotePort] = ips
|
|
}
|
|
ips[remoteIP] = struct{}{}
|
|
scope.remoteRelations++
|
|
}
|
|
}
|
|
}
|
|
if ipRecorded && localPortRecorded && localPort != 0 {
|
|
ports := scope.localPortsByRemoteIP[remoteIP]
|
|
if ports == nil || !containsPort(ports, localPort) {
|
|
if scope.localRelations >= limits.MaxRelations {
|
|
scope.markRelationSaturated()
|
|
} else {
|
|
if ports == nil {
|
|
ports = make(map[uint16]struct{})
|
|
scope.localPortsByRemoteIP[remoteIP] = ports
|
|
}
|
|
ports[localPort] = struct{}{}
|
|
ips := scope.remoteIPsByLocalPort[localPort]
|
|
if ips == nil {
|
|
ips = make(map[netip.Addr]struct{})
|
|
scope.remoteIPsByLocalPort[localPort] = ips
|
|
}
|
|
ips[remoteIP] = struct{}{}
|
|
scope.localRelations++
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
func containsPort(ports map[uint16]struct{}, port uint16) bool {
|
|
_, ok := ports[port]
|
|
return ok
|
|
}
|
|
|
|
func (scope *flowAggregate) markRelationSaturated() {
|
|
scope.quality[QualityRelationCardinality] = struct{}{}
|
|
scope.quality[QualityIncomplete] = struct{}{}
|
|
}
|
|
|
|
func (a *Aggregator) Result() (*FamilyObservation, *FamilyObservation, []QualityFlag) {
|
|
ipv4 := a.family(false)
|
|
ipv6 := a.family(true)
|
|
flagSet := make(map[QualityFlag]struct{}, len(a.quality)+4)
|
|
for flag := range a.quality {
|
|
flagSet[flag] = struct{}{}
|
|
}
|
|
for _, scope := range a.byScope {
|
|
for flag := range scope.quality {
|
|
flagSet[flag] = struct{}{}
|
|
}
|
|
}
|
|
flags := make([]QualityFlag, 0, len(flagSet))
|
|
for flag := range flagSet {
|
|
flags = append(flags, flag)
|
|
}
|
|
flags, _ = CanonicalQualityFlags(flags)
|
|
return ipv4, ipv6, flags
|
|
}
|
|
|
|
func (a *Aggregator) family(ipv6 bool) *FamilyObservation {
|
|
inbound := a.summary(aggregateKey{ipv6: ipv6, direction: DirectionInbound})
|
|
outbound := a.summary(aggregateKey{ipv6: ipv6, direction: DirectionOutbound})
|
|
if inbound == nil && outbound == nil {
|
|
return nil
|
|
}
|
|
if inbound == nil {
|
|
inbound = &FlowSummary{}
|
|
}
|
|
if outbound == nil {
|
|
outbound = &FlowSummary{}
|
|
}
|
|
return &FamilyObservation{Inbound: inbound, Outbound: outbound}
|
|
}
|
|
|
|
func (a *Aggregator) summary(key aggregateKey) *FlowSummary {
|
|
scope := a.byScope[key]
|
|
if scope == nil {
|
|
return nil
|
|
}
|
|
result := scope.summary
|
|
result.UniqueRemoteIPs = uint64(len(scope.remoteIPs))
|
|
result.UniqueRemotePorts = uint64(len(scope.remotePorts))
|
|
result.UniqueLocalPorts = uint64(len(scope.localPorts))
|
|
for _, ports := range scope.remotePortsByIP {
|
|
if uint64(len(ports)) > result.MaxRemotePortsPerRemoteIP {
|
|
result.MaxRemotePortsPerRemoteIP = uint64(len(ports))
|
|
}
|
|
}
|
|
for _, ips := range scope.remoteIPsByRemotePort {
|
|
if uint64(len(ips)) > result.MaxRemoteIPsPerRemotePort {
|
|
result.MaxRemoteIPsPerRemotePort = uint64(len(ips))
|
|
}
|
|
}
|
|
for _, ports := range scope.localPortsByRemoteIP {
|
|
if uint64(len(ports)) > result.MaxLocalPortsPerRemoteIP {
|
|
result.MaxLocalPortsPerRemoteIP = uint64(len(ports))
|
|
}
|
|
}
|
|
for _, ips := range scope.remoteIPsByLocalPort {
|
|
if uint64(len(ips)) > result.MaxRemoteIPsPerLocalPort {
|
|
result.MaxRemoteIPsPerLocalPort = uint64(len(ips))
|
|
}
|
|
}
|
|
return &result
|
|
}
|