mirror of
https://github.com/safing/portmaster
synced 2025-09-02 02:29:12 +00:00
Merge pull request #262 from safing/feature/fast-network-table-lookup
Add fast network table lookup
This commit is contained in:
commit
ef6afea5c0
4 changed files with 32 additions and 17 deletions
|
@ -145,7 +145,7 @@ func checkSelfCommunication(ctx context.Context, conn *network.Connection, pkt p
|
||||||
SrcPort: pktInfo.SrcPort,
|
SrcPort: pktInfo.SrcPort,
|
||||||
Dst: pktInfo.Dst,
|
Dst: pktInfo.Dst,
|
||||||
DstPort: pktInfo.DstPort,
|
DstPort: pktInfo.DstPort,
|
||||||
})
|
}, true)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Tracer(ctx).Warningf("filter: failed to find local peer process PID: %s", err)
|
log.Tracer(ctx).Warningf("filter: failed to find local peer process PID: %s", err)
|
||||||
} else {
|
} else {
|
||||||
|
|
|
@ -78,7 +78,7 @@ func takeover(resolverIP net.IP, resolverPort uint16) (int, error) {
|
||||||
SrcPort: 0, // do not record direction
|
SrcPort: 0, // do not record direction
|
||||||
Dst: resolverIP,
|
Dst: resolverIP,
|
||||||
DstPort: resolverPort,
|
DstPort: resolverPort,
|
||||||
})
|
}, true)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
// there may be nothing listening on :53
|
// there may be nothing listening on :53
|
||||||
return 0, nil
|
return 0, nil
|
||||||
|
|
|
@ -23,7 +23,7 @@ import (
|
||||||
// - switch direction to outbound if outbound packet is seen?
|
// - switch direction to outbound if outbound packet is seen?
|
||||||
// - IP: Unidentified Process
|
// - IP: Unidentified Process
|
||||||
|
|
||||||
// Errors
|
// Errors.
|
||||||
var (
|
var (
|
||||||
ErrConnectionNotFound = errors.New("could not find connection in system state tables")
|
ErrConnectionNotFound = errors.New("could not find connection in system state tables")
|
||||||
ErrPIDNotFound = errors.New("could not find pid for socket inode")
|
ErrPIDNotFound = errors.New("could not find pid for socket inode")
|
||||||
|
@ -32,10 +32,11 @@ var (
|
||||||
var (
|
var (
|
||||||
baseWaitTime = 3 * time.Millisecond
|
baseWaitTime = 3 * time.Millisecond
|
||||||
lookupRetries = 7 * 2 // Every retry takes two full passes.
|
lookupRetries = 7 * 2 // Every retry takes two full passes.
|
||||||
|
fastLookupRetries = 2 * 2
|
||||||
)
|
)
|
||||||
|
|
||||||
// Lookup looks for the given connection in the system state tables and returns the PID of the associated process and whether the connection is inbound.
|
// Lookup looks for the given connection in the system state tables and returns the PID of the associated process and whether the connection is inbound.
|
||||||
func Lookup(pktInfo *packet.Info) (pid int, inbound bool, err error) {
|
func Lookup(pktInfo *packet.Info, fast bool) (pid int, inbound bool, err error) {
|
||||||
// auto-detect version
|
// auto-detect version
|
||||||
if pktInfo.Version == 0 {
|
if pktInfo.Version == 0 {
|
||||||
if ip := pktInfo.LocalIP().To4(); ip != nil {
|
if ip := pktInfo.LocalIP().To4(); ip != nil {
|
||||||
|
@ -47,31 +48,31 @@ func Lookup(pktInfo *packet.Info) (pid int, inbound bool, err error) {
|
||||||
|
|
||||||
switch {
|
switch {
|
||||||
case pktInfo.Version == packet.IPv4 && pktInfo.Protocol == packet.TCP:
|
case pktInfo.Version == packet.IPv4 && pktInfo.Protocol == packet.TCP:
|
||||||
return tcp4Table.lookup(pktInfo)
|
return tcp4Table.lookup(pktInfo, fast)
|
||||||
|
|
||||||
case pktInfo.Version == packet.IPv6 && pktInfo.Protocol == packet.TCP:
|
case pktInfo.Version == packet.IPv6 && pktInfo.Protocol == packet.TCP:
|
||||||
return tcp6Table.lookup(pktInfo)
|
return tcp6Table.lookup(pktInfo, fast)
|
||||||
|
|
||||||
case pktInfo.Version == packet.IPv4 && pktInfo.Protocol == packet.UDP:
|
case pktInfo.Version == packet.IPv4 && pktInfo.Protocol == packet.UDP:
|
||||||
return udp4Table.lookup(pktInfo)
|
return udp4Table.lookup(pktInfo, fast)
|
||||||
|
|
||||||
case pktInfo.Version == packet.IPv6 && pktInfo.Protocol == packet.UDP:
|
case pktInfo.Version == packet.IPv6 && pktInfo.Protocol == packet.UDP:
|
||||||
return udp6Table.lookup(pktInfo)
|
return udp6Table.lookup(pktInfo, fast)
|
||||||
|
|
||||||
default:
|
default:
|
||||||
return socket.UnidentifiedProcessID, false, errors.New("unsupported protocol for finding process")
|
return socket.UnidentifiedProcessID, false, errors.New("unsupported protocol for finding process")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (table *tcpTable) lookup(pktInfo *packet.Info) (
|
func (table *tcpTable) lookup(pktInfo *packet.Info, fast bool) (
|
||||||
pid int,
|
pid int,
|
||||||
inbound bool,
|
inbound bool,
|
||||||
err error,
|
err error,
|
||||||
) {
|
) {
|
||||||
// Search pattern: search, wait, search, refresh, search, wait, search, refresh, ...
|
// Search pattern: search, refresh, search, wait, search, refresh, search, wait, ...
|
||||||
|
|
||||||
// Search for the socket until found.
|
// Search for the socket until found.
|
||||||
for i := 0; i <= lookupRetries; i++ {
|
for i := 1; i <= lookupRetries; i++ {
|
||||||
// Check main table for socket.
|
// Check main table for socket.
|
||||||
socketInfo, inbound := table.findSocket(pktInfo)
|
socketInfo, inbound := table.findSocket(pktInfo)
|
||||||
if socketInfo == nil && table.dualStack != nil {
|
if socketInfo == nil && table.dualStack != nil {
|
||||||
|
@ -85,6 +86,11 @@ func (table *tcpTable) lookup(pktInfo *packet.Info) (
|
||||||
return checkPID(socketInfo, inbound)
|
return checkPID(socketInfo, inbound)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Search less if we want to be fast.
|
||||||
|
if fast && i < fastLookupRetries {
|
||||||
|
break
|
||||||
|
}
|
||||||
|
|
||||||
// every time, except for the last iteration
|
// every time, except for the last iteration
|
||||||
if i < lookupRetries {
|
if i < lookupRetries {
|
||||||
// Take turns in waiting and refreshing in order to satisfy the search pattern.
|
// Take turns in waiting and refreshing in order to satisfy the search pattern.
|
||||||
|
@ -134,12 +140,12 @@ func (table *tcpTable) findSocket(pktInfo *packet.Info) (
|
||||||
return nil, false
|
return nil, false
|
||||||
}
|
}
|
||||||
|
|
||||||
func (table *udpTable) lookup(pktInfo *packet.Info) (
|
func (table *udpTable) lookup(pktInfo *packet.Info, fast bool) (
|
||||||
pid int,
|
pid int,
|
||||||
inbound bool,
|
inbound bool,
|
||||||
err error,
|
err error,
|
||||||
) {
|
) {
|
||||||
// Search pattern: search, wait, search, refresh, search, wait, search, refresh, ...
|
// Search pattern: search, refresh, search, wait, search, refresh, search, wait, ...
|
||||||
|
|
||||||
// TODO: Currently broadcast/multicast scopes are not checked, so we might
|
// TODO: Currently broadcast/multicast scopes are not checked, so we might
|
||||||
// attribute an incoming broadcast/multicast packet to the wrong process if
|
// attribute an incoming broadcast/multicast packet to the wrong process if
|
||||||
|
@ -148,7 +154,7 @@ func (table *udpTable) lookup(pktInfo *packet.Info) (
|
||||||
isInboundMulticast := pktInfo.Inbound && netutils.ClassifyIP(pktInfo.LocalIP()) == netutils.LocalMulticast
|
isInboundMulticast := pktInfo.Inbound && netutils.ClassifyIP(pktInfo.LocalIP()) == netutils.LocalMulticast
|
||||||
|
|
||||||
// Search for the socket until found.
|
// Search for the socket until found.
|
||||||
for i := 0; i <= lookupRetries; i++ {
|
for i := 1; i <= lookupRetries; i++ {
|
||||||
// Check main table for socket.
|
// Check main table for socket.
|
||||||
socketInfo := table.findSocket(pktInfo, isInboundMulticast)
|
socketInfo := table.findSocket(pktInfo, isInboundMulticast)
|
||||||
if socketInfo == nil && table.dualStack != nil {
|
if socketInfo == nil && table.dualStack != nil {
|
||||||
|
@ -173,6 +179,11 @@ func (table *udpTable) lookup(pktInfo *packet.Info) (
|
||||||
return checkPID(socketInfo, connInbound)
|
return checkPID(socketInfo, connInbound)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Search less if we want to be fast.
|
||||||
|
if fast && i < fastLookupRetries {
|
||||||
|
break
|
||||||
|
}
|
||||||
|
|
||||||
// every time, except for the last iteration
|
// every time, except for the last iteration
|
||||||
if i < lookupRetries {
|
if i < lookupRetries {
|
||||||
// Take turns in waiting and refreshing in order to satisfy the search pattern.
|
// Take turns in waiting and refreshing in order to satisfy the search pattern.
|
||||||
|
|
|
@ -19,9 +19,13 @@ func GetProcessByConnection(ctx context.Context, pktInfo *packet.Info) (process
|
||||||
return GetUnidentifiedProcess(ctx), pktInfo.Inbound, nil
|
return GetUnidentifiedProcess(ctx), pktInfo.Inbound, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Use fast search for inbound packets, as the listening socket should
|
||||||
|
// already be there for a while now.
|
||||||
|
fastSearch := pktInfo.Inbound
|
||||||
|
|
||||||
log.Tracer(ctx).Tracef("process: getting pid from system network state")
|
log.Tracer(ctx).Tracef("process: getting pid from system network state")
|
||||||
var pid int
|
var pid int
|
||||||
pid, connInbound, err = state.Lookup(pktInfo)
|
pid, connInbound, err = state.Lookup(pktInfo, fastSearch)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Tracer(ctx).Debugf("process: failed to find PID of connection: %s", err)
|
log.Tracer(ctx).Debugf("process: failed to find PID of connection: %s", err)
|
||||||
return nil, pktInfo.Inbound, err
|
return nil, pktInfo.Inbound, err
|
||||||
|
|
Loading…
Add table
Reference in a new issue