Exempt IPv6 LAN (broadcast domain only) from SPN

This commit is contained in:
Daniel 2022-03-02 14:33:25 +01:00
parent 014ac058ce
commit d970b163a5
2 changed files with 100 additions and 53 deletions

View file

@ -4,6 +4,7 @@ import (
"context" "context"
"github.com/safing/portbase/log" "github.com/safing/portbase/log"
"github.com/safing/portmaster/netenv"
"github.com/safing/portmaster/network" "github.com/safing/portmaster/network"
"github.com/safing/portmaster/network/packet" "github.com/safing/portmaster/network/packet"
"github.com/safing/portmaster/profile" "github.com/safing/portmaster/profile"
@ -40,6 +41,21 @@ func checkTunneling(ctx context.Context, conn *network.Connection, pkt packet.Pa
} }
} }
// Check more extensively for Local/LAN connections.
myNet, err := netenv.IsMyNet(conn.Entity.IP)
if err != nil {
log.Warningf("firewall: failed to check if %s is in my net: %s", conn.Entity.IP, err)
} else if myNet {
// With IPv6, just checking the IP scope is not enough, as the host very
// likely has a public IPv6 address.
// Don't tunnel LAN connections.
// TODO: We currently don't check the full LAN scope, but only the
// broadcast domain of the host - ie. the networks that the host is
// directly attached to.
return
}
// Get profile. // Get profile.
layeredProfile := conn.Process().Profile() layeredProfile := conn.Process().Profile()
if layeredProfile == nil { if layeredProfile == nil {
@ -128,7 +144,7 @@ func checkTunneling(ctx context.Context, conn *network.Connection, pkt packet.Pa
} }
// Queue request in sluice. // Queue request in sluice.
err := sluice.AwaitRequest(conn, crew.HandleSluiceRequest) err = sluice.AwaitRequest(conn, crew.HandleSluiceRequest)
if err != nil { if err != nil {
log.Tracer(pkt.Ctx()).Warningf("failed to request tunneling: %s", err) log.Tracer(pkt.Ctx()).Warningf("failed to request tunneling: %s", err)
conn.Failed("failed to request tunneling", "") conn.Failed("failed to request tunneling", "")

View file

@ -56,52 +56,30 @@ var (
myNetworksLock sync.Mutex myNetworksLock sync.Mutex
myNetworksNetworkChangedFlag = GetNetworkChangedFlag() myNetworksNetworkChangedFlag = GetNetworkChangedFlag()
myNetworksRefreshError error //nolint:errname // Not what the linter thinks this is for. myNetworksRefreshError error //nolint:errname // Not what the linter thinks this is for.
myNetworksRefreshFailingUntil time.Time myNetworksDontRefreshUntil time.Time
) )
// IsMyIP returns whether the given unicast IP is currently configured on the local host. // refreshMyNetworks refreshes the networks held in refreshMyNetworks.
// Broadcast or multicast addresses will never match, even if valid in in use. // The caller must hold myNetworksLock.
func IsMyIP(ip net.IP) (yes bool, err error) { func refreshMyNetworks() error {
// Check for IPs that don't need extra checks. // Check if we already refreshed recently.
switch netutils.GetIPScope(ip) { //nolint:exhaustive // Only looking for specific values. if time.Now().Before(myNetworksDontRefreshUntil) {
case netutils.HostLocal: // Return previous error, if available.
return true, nil if myNetworksRefreshError != nil {
case netutils.LocalMulticast, netutils.GlobalMulticast: return fmt.Errorf("failed to previously refresh interface addresses: %w", myNetworksRefreshError)
return false, nil
} }
return nil
myNetworksLock.Lock()
defer myNetworksLock.Unlock()
// Check if current data matches IP.
// Matching on somewhat older data is not a problem, as these IPs would not
// just randomly pop up somewhere else that fast.
mine, myNet := checkIfMyIP(ip)
switch {
case mine:
// IP matched.
return true, nil
case myNetworksNetworkChangedFlag.IsSet():
// The network changed, so we need to refresh the data.
case myNet:
// IP is one of the networks and nothing changed, so this is not our IP.
return false, nil
}
// Check if there was a recent error on the previous refresh.
if myNetworksRefreshError != nil && time.Now().Before(myNetworksRefreshFailingUntil) {
return false, fmt.Errorf("failed to previously refresh interface addresses: %w", myNetworksRefreshError)
} }
myNetworksRefreshError = nil
myNetworksDontRefreshUntil = time.Now().Add(1 * time.Second)
// Refresh assigned networks. // Refresh assigned networks.
interfaceNetworks, err := net.InterfaceAddrs() interfaceNetworks, err := net.InterfaceAddrs()
if err != nil { if err != nil {
// Save error for one second.
// In some cases the system blocks on this call, which piles up to // In some cases the system blocks on this call, which piles up to
// literally over thousand goroutines wanting to try this again. // literally over thousand goroutines wanting to try this again.
myNetworksRefreshError = err myNetworksRefreshError = err
myNetworksRefreshFailingUntil = time.Now().Add(1 * time.Second) return fmt.Errorf("failed to refresh interface addresses: %w", err)
return false, fmt.Errorf("failed to refresh interface addresses: %w", err)
} }
myNetworks = make([]*net.IPNet, 0, len(interfaceNetworks)) myNetworks = make([]*net.IPNet, 0, len(interfaceNetworks))
for _, ifNet := range interfaceNetworks { for _, ifNet := range interfaceNetworks {
@ -114,24 +92,39 @@ func IsMyIP(ip net.IP) (yes bool, err error) {
myNetworks = append(myNetworks, ipNet) myNetworks = append(myNetworks, ipNet)
} }
// Reset error.
myNetworksRefreshError = nil
// Reset changed flag. // Reset changed flag.
myNetworksNetworkChangedFlag.Refresh() myNetworksNetworkChangedFlag.Refresh()
// Check for match again. return nil
if mine, matched := checkIfMyIP(ip); matched {
return mine, nil
}
return false, nil
} }
func checkIfMyIP(ip net.IP) (mine bool, myNet bool) { // IsMyIP returns whether the given unicast IP is currently configured on the local host.
// Broadcast or multicast addresses will never match, even if valid and in use.
// Function is optimized with the assumption that is likely that the IP is mine.
func IsMyIP(ip net.IP) (yes bool, err error) {
// Check for IPs that don't need extra checks.
switch netutils.GetIPScope(ip) { //nolint:exhaustive // Only looking for specific values.
case netutils.HostLocal:
return true, nil
case netutils.LocalMulticast, netutils.GlobalMulticast:
return false, nil
}
myNetworksLock.Lock()
defer myNetworksLock.Unlock()
// Check if the network changed.
if myNetworksNetworkChangedFlag.IsSet() {
err := refreshMyNetworks()
if err != nil {
return false, err
}
}
// Check against assigned IPs. // Check against assigned IPs.
for _, myNet := range myNetworks { for _, myNet := range myNetworks {
if ip.Equal(myNet.IP) { if ip.Equal(myNet.IP) {
return true, true return true, nil
} }
} }
@ -140,9 +133,47 @@ func checkIfMyIP(ip net.IP) (mine bool, myNet bool) {
// most cases and network matching is more expensive. // most cases and network matching is more expensive.
for _, myNet := range myNetworks { for _, myNet := range myNetworks {
if myNet.Contains(ip) { if myNet.Contains(ip) {
return false, true return false, nil
} }
} }
return false, false // Could not find IP anywhere. Refresh network to be sure.
err = refreshMyNetworks()
if err != nil {
return false, err
}
// Check against assigned IPs again.
for _, myNet := range myNetworks {
if ip.Equal(myNet.IP) {
return true, nil
}
}
return false, nil
}
// IsMyNet returns whether the given IP is currently in the host's broadcast
// domain - ie. the networks that the host is directly attached to.
// Function is optimized with the assumption that is unlikely that the IP is
// in the broadcast domain.
func IsMyNet(ip net.IP) (yes bool, err error) {
myNetworksLock.Lock()
defer myNetworksLock.Unlock()
// Check if the network changed.
if myNetworksNetworkChangedFlag.IsSet() {
err := refreshMyNetworks()
if err != nil {
return false, err
}
}
// Check if the IP address is in my networks.
for _, myNet := range myNetworks {
if myNet.Contains(ip) {
return true, nil
}
}
return false, nil
} }