Add new ICMP filter handler, fix cleaning of ICMP connections
This commit is contained in:
@@ -11,6 +11,14 @@ import (
|
||||
)
|
||||
|
||||
const (
|
||||
// EndConnsAfterInactiveFor defines the amount of time after not seen
|
||||
// connections of unsupported protocols are marked as ended.
|
||||
EndConnsAfterInactiveFor = 5 * time.Minute
|
||||
|
||||
// EndICMPConnsAfterInactiveFor defines the amount of time after not seen
|
||||
// ICMP "connections" are marked as ended.
|
||||
EndICMPConnsAfterInactiveFor = 1 * time.Minute
|
||||
|
||||
// DeleteConnsAfterEndedThreshold defines the amount of time after which
|
||||
// ended connections should be removed from the internal connection state.
|
||||
DeleteConnsAfterEndedThreshold = 10 * time.Minute
|
||||
@@ -48,7 +56,9 @@ func cleanConnections() (activePIDs map[int]struct{}) {
|
||||
_ = module.RunMicroTask("clean connections", 0, func(ctx context.Context) error {
|
||||
now := time.Now().UTC()
|
||||
nowUnix := now.Unix()
|
||||
ignoreNewer := nowUnix - 1
|
||||
ignoreNewer := nowUnix - 2
|
||||
endNotSeenSince := now.Add(-EndConnsAfterInactiveFor).Unix()
|
||||
endICMPNotSeenSince := now.Add(-EndICMPConnsAfterInactiveFor).Unix()
|
||||
deleteOlderThan := now.Add(-DeleteConnsAfterEndedThreshold).Unix()
|
||||
deleteIncompleteOlderThan := now.Add(-DeleteIncompleteConnsAfterStartedThreshold).Unix()
|
||||
|
||||
@@ -68,22 +78,37 @@ func cleanConnections() (activePIDs map[int]struct{}) {
|
||||
// Remove connection from state.
|
||||
conn.delete()
|
||||
}
|
||||
|
||||
case conn.Ended == 0:
|
||||
// Step 1: check if still active
|
||||
exists := state.Exists(&packet.Info{
|
||||
Inbound: false, // src == local
|
||||
Version: conn.IPVersion,
|
||||
Protocol: conn.IPProtocol,
|
||||
Src: conn.LocalIP,
|
||||
SrcPort: conn.LocalPort,
|
||||
Dst: conn.Entity.IP,
|
||||
DstPort: conn.Entity.Port,
|
||||
PID: process.UndefinedProcessID,
|
||||
SeenAt: time.Unix(conn.Started, 0), // State tables will be updated if older than this.
|
||||
}, now)
|
||||
var connActive bool
|
||||
switch conn.IPProtocol { //nolint:exhaustive
|
||||
case packet.TCP, packet.UDP:
|
||||
connActive = state.Exists(&packet.Info{
|
||||
Inbound: false, // src == local
|
||||
Version: conn.IPVersion,
|
||||
Protocol: conn.IPProtocol,
|
||||
Src: conn.LocalIP,
|
||||
SrcPort: conn.LocalPort,
|
||||
Dst: conn.Entity.IP,
|
||||
DstPort: conn.Entity.Port,
|
||||
PID: process.UndefinedProcessID,
|
||||
SeenAt: time.Unix(conn.Started, 0), // State tables will be updated if older than this.
|
||||
}, now)
|
||||
// Update last seen value for permanent verdict connections.
|
||||
if connActive && conn.VerdictPermanent {
|
||||
conn.lastSeen.Store(nowUnix)
|
||||
}
|
||||
|
||||
case packet.ICMP, packet.ICMPv6:
|
||||
connActive = conn.lastSeen.Load() > endICMPNotSeenSince
|
||||
|
||||
default:
|
||||
connActive = conn.lastSeen.Load() > endNotSeenSince
|
||||
}
|
||||
|
||||
// Step 2: mark as ended
|
||||
if !exists {
|
||||
if !connActive {
|
||||
conn.Ended = nowUnix
|
||||
|
||||
// Stop the firewall handler, in case one is running.
|
||||
@@ -97,6 +122,7 @@ func cleanConnections() (activePIDs map[int]struct{}) {
|
||||
if conn.process != nil {
|
||||
activePIDs[conn.process.Pid] = struct{}{}
|
||||
}
|
||||
|
||||
case conn.Ended < deleteOlderThan:
|
||||
// Step 3: delete
|
||||
// DEBUG:
|
||||
|
||||
@@ -6,6 +6,7 @@ import (
|
||||
"fmt"
|
||||
"net"
|
||||
"sync"
|
||||
"sync/atomic"
|
||||
"time"
|
||||
|
||||
"github.com/tevino/abool"
|
||||
@@ -180,6 +181,11 @@ type Connection struct { //nolint:maligned // TODO: fix alignment
|
||||
// BytesSent holds the observed sent bytes of the connection.
|
||||
BytesSent uint64
|
||||
|
||||
// lastSeen holds the timestamp when the connection was last seen.
|
||||
// If permanent verdicts are enabled and bandwidth reporting is not active,
|
||||
// this value will likely not be correct.
|
||||
lastSeen atomic.Int64
|
||||
|
||||
// prompt holds the active prompt for this connection, if there is one.
|
||||
prompt *notifications.Notification
|
||||
// promptLock locks the prompt separately from the connection.
|
||||
@@ -340,6 +346,7 @@ func NewConnectionFromDNSRequest(ctx context.Context, fqdn string, cnames []stri
|
||||
Ended: timestamp,
|
||||
dataComplete: abool.NewBool(true),
|
||||
}
|
||||
dnsConn.lastSeen.Store(timestamp)
|
||||
|
||||
// Inherit internal status of profile.
|
||||
if localProfile := proc.Profile().LocalProfile(); localProfile != nil {
|
||||
@@ -383,6 +390,7 @@ func NewConnectionFromExternalDNSRequest(ctx context.Context, fqdn string, cname
|
||||
Ended: timestamp,
|
||||
dataComplete: abool.NewBool(true),
|
||||
}
|
||||
dnsConn.lastSeen.Store(timestamp)
|
||||
|
||||
// Inherit internal status of profile.
|
||||
if localProfile := remoteHost.Profile().LocalProfile(); localProfile != nil {
|
||||
@@ -418,6 +426,7 @@ func NewIncompleteConnection(pkt packet.Packet) *Connection {
|
||||
PID: info.PID,
|
||||
dataComplete: abool.NewBool(false),
|
||||
}
|
||||
conn.lastSeen.Store(conn.Started)
|
||||
|
||||
// Bullshit check Started timestamp.
|
||||
if conn.Started < tooOldTimestamp {
|
||||
@@ -569,6 +578,7 @@ func (conn *Connection) GatherConnectionInfo(pkt packet.Packet) (err error) {
|
||||
conn.dataComplete.Set()
|
||||
}
|
||||
|
||||
conn.SaveWhenFinished()
|
||||
return nil
|
||||
}
|
||||
|
||||
@@ -859,6 +869,9 @@ func (conn *Connection) StopFirewallHandler() {
|
||||
|
||||
// HandlePacket queues packet of Link for handling.
|
||||
func (conn *Connection) HandlePacket(pkt packet.Packet) {
|
||||
// Update last seen timestamp.
|
||||
conn.lastSeen.Store(time.Now().Unix())
|
||||
|
||||
conn.pktQueueLock.Lock()
|
||||
defer conn.pktQueueLock.Unlock()
|
||||
|
||||
@@ -994,17 +1007,19 @@ func packetHandlerHandleConn(ctx context.Context, conn *Connection, pkt packet.P
|
||||
// Record metrics.
|
||||
packetHandlingHistogram.UpdateDuration(pkt.Info().SeenAt)
|
||||
|
||||
// Log result and submit trace.
|
||||
switch {
|
||||
case conn.DataIsComplete():
|
||||
tracer.Infof("filter: connection %s %s: %s", conn, conn.VerdictVerb(), conn.Reason.Msg)
|
||||
case conn.Verdict != VerdictUndecided:
|
||||
tracer.Debugf("filter: connection %s fast-tracked", pkt)
|
||||
default:
|
||||
tracer.Debugf("filter: gathered data on connection %s", conn)
|
||||
// Log result and submit trace, when there are any changes.
|
||||
if conn.saveWhenFinished {
|
||||
switch {
|
||||
case conn.DataIsComplete():
|
||||
tracer.Infof("filter: connection %s %s: %s", conn, conn.VerdictVerb(), conn.Reason.Msg)
|
||||
case conn.Verdict != VerdictUndecided:
|
||||
tracer.Debugf("filter: connection %s fast-tracked", pkt)
|
||||
default:
|
||||
tracer.Debugf("filter: gathered data on connection %s", conn)
|
||||
}
|
||||
// Submit trace logs.
|
||||
tracer.Submit()
|
||||
}
|
||||
// Submit trace logs.
|
||||
tracer.Submit()
|
||||
|
||||
// Push changes, if there are any.
|
||||
if conn.saveWhenFinished {
|
||||
|
||||
Reference in New Issue
Block a user