2023-04-17 11:15:37 +02:00
|
|
|
//go:build !android
|
|
|
|
|
2022-09-05 09:06:35 +02:00
|
|
|
package routemanager
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"fmt"
|
2023-04-17 11:15:37 +02:00
|
|
|
|
2022-09-05 09:06:35 +02:00
|
|
|
log "github.com/sirupsen/logrus"
|
2023-09-05 21:07:32 +02:00
|
|
|
|
|
|
|
"github.com/netbirdio/netbird/client/internal/checkfw"
|
2022-09-05 09:06:35 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
2022-09-30 11:39:15 +02:00
|
|
|
ipv6Forwarding = "netbird-rt-ipv6-forwarding"
|
|
|
|
ipv4Forwarding = "netbird-rt-ipv4-forwarding"
|
|
|
|
ipv6Nat = "netbird-rt-ipv6-nat"
|
|
|
|
ipv4Nat = "netbird-rt-ipv4-nat"
|
|
|
|
natFormat = "netbird-nat-%s"
|
|
|
|
forwardingFormat = "netbird-fwd-%s"
|
|
|
|
inNatFormat = "netbird-nat-in-%s"
|
|
|
|
inForwardingFormat = "netbird-fwd-in-%s"
|
|
|
|
ipv6 = "ipv6"
|
|
|
|
ipv4 = "ipv4"
|
2022-09-05 09:06:35 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
func genKey(format string, input string) string {
|
|
|
|
return fmt.Sprintf(format, input)
|
|
|
|
}
|
|
|
|
|
2023-09-05 21:07:32 +02:00
|
|
|
// newFirewall if supported, returns an iptables manager, otherwise returns a nftables manager
|
|
|
|
func newFirewall(parentCTX context.Context) (firewallManager, error) {
|
|
|
|
checkResult := checkfw.Check()
|
|
|
|
switch checkResult {
|
|
|
|
case checkfw.IPTABLES, checkfw.IPTABLESWITHV6:
|
|
|
|
log.Debug("creating an iptables firewall manager for route rules")
|
|
|
|
ipv6Supported := checkResult == checkfw.IPTABLESWITHV6
|
|
|
|
return newIptablesManager(parentCTX, ipv6Supported)
|
|
|
|
case checkfw.NFTABLES:
|
|
|
|
log.Info("creating an nftables firewall manager for route rules")
|
|
|
|
return newNFTablesManager(parentCTX), nil
|
2023-08-12 11:42:36 +02:00
|
|
|
}
|
2023-09-05 21:07:32 +02:00
|
|
|
|
|
|
|
return nil, fmt.Errorf("couldn't initialize nftables or iptables clients. Using a dummy firewall manager for route rules")
|
2023-07-14 20:44:35 +02:00
|
|
|
}
|
|
|
|
|
2022-09-30 11:39:15 +02:00
|
|
|
func getInPair(pair routerPair) routerPair {
|
|
|
|
return routerPair{
|
|
|
|
ID: pair.ID,
|
|
|
|
// invert source/destination
|
|
|
|
source: pair.destination,
|
|
|
|
destination: pair.source,
|
|
|
|
masquerade: pair.masquerade,
|
|
|
|
}
|
|
|
|
}
|