2021-12-08 01:46:55 +08:00
package logic
import (
"encoding/json"
"errors"
2022-08-08 10:22:54 +08:00
"fmt"
2021-12-08 01:46:55 +08:00
"strings"
"time"
"github.com/gravitl/netmaker/database"
"github.com/gravitl/netmaker/logger"
"github.com/gravitl/netmaker/models"
2022-09-29 02:59:21 +08:00
"github.com/gravitl/netmaker/servercfg"
2021-12-08 01:46:55 +08:00
)
// CreateEgressGateway - creates an egress gateway
2022-12-20 04:15:35 +08:00
func CreateEgressGateway ( gateway models . EgressGatewayRequest ) ( models . LegacyNode , error ) {
2022-09-03 20:55:49 +08:00
for i , cidr := range gateway . Ranges {
normalized , err := NormalizeCIDR ( cidr )
if err != nil {
2022-12-20 04:15:35 +08:00
return models . LegacyNode { } , err
2022-09-03 20:55:49 +08:00
}
gateway . Ranges [ i ] = normalized
}
2022-01-11 06:52:21 +08:00
node , err := GetNodeByID ( gateway . NodeID )
2021-12-08 01:46:55 +08:00
if err != nil {
2022-12-20 04:15:35 +08:00
return models . LegacyNode { } , err
2021-12-08 01:46:55 +08:00
}
2022-05-09 01:37:22 +08:00
if node . OS != "linux" && node . OS != "freebsd" { // add in darwin later
2022-12-20 04:15:35 +08:00
return models . LegacyNode { } , errors . New ( node . OS + " is unsupported for egress gateways" )
2022-01-26 22:04:30 +08:00
}
2022-08-20 03:32:25 +08:00
if node . OS == "linux" && node . FirewallInUse == models . FIREWALL_NONE {
2022-12-20 04:15:35 +08:00
return models . LegacyNode { } , errors . New ( "firewall is not supported for egress gateways" )
2022-08-20 03:32:25 +08:00
}
2022-07-20 19:01:28 +08:00
if gateway . NatEnabled == "" {
gateway . NatEnabled = "yes"
}
2021-12-08 01:46:55 +08:00
err = ValidateEgressGateway ( gateway )
if err != nil {
2022-12-20 04:15:35 +08:00
return models . LegacyNode { } , err
2021-12-08 01:46:55 +08:00
}
node . IsEgressGateway = "yes"
node . EgressGatewayRanges = gateway . Ranges
2022-07-19 19:20:31 +08:00
node . EgressGatewayNatEnabled = gateway . NatEnabled
2022-08-08 10:22:54 +08:00
node . EgressGatewayRequest = gateway // store entire request for use when preserving the egress gateway
2022-05-09 01:37:22 +08:00
postUpCmd := ""
postDownCmd := ""
2022-09-01 20:10:49 +08:00
ipv4 , ipv6 := getNetworkProtocols ( gateway . Ranges )
2022-10-06 01:49:15 +08:00
//no support for ipv6 and ip6tables in netmaker container
if node . IsServer == "yes" {
ipv6 = false
}
2022-08-07 06:00:20 +08:00
logger . Log ( 3 , "creating egress gateway firewall in use is '" , node . FirewallInUse , "'" )
2022-05-09 01:37:22 +08:00
if node . OS == "linux" {
2022-08-04 03:18:04 +08:00
switch node . FirewallInUse {
case models . FIREWALL_NFTABLES :
// nftables only supported on Linux
2022-09-02 23:01:19 +08:00
// assumes chains eg FORWARD and postrouting already exist
2022-08-07 06:00:20 +08:00
logger . Log ( 3 , "creating egress gateway nftables is present" )
2022-08-05 20:07:00 +08:00
// down commands don't remove as removal of the rules leaves an empty chain while
// removing the chain with rules in it would remove all rules in that section (not safe
// if there are remaining rules on the host that need to stay). In practice the chain is removed
// when non-empty even though the removal of a non-empty chain should not be possible per nftables wiki.
2022-09-02 23:01:19 +08:00
postUpCmd , postDownCmd = firewallNFTCommandsCreateEgress ( node . Interface , gateway . Interface , gateway . Ranges , node . EgressGatewayNatEnabled , ipv4 , ipv6 )
2022-08-08 10:22:54 +08:00
2022-08-04 03:18:04 +08:00
default : // iptables assumed
2022-08-07 06:00:20 +08:00
logger . Log ( 3 , "creating egress gateway nftables is not present" )
2022-09-01 20:10:49 +08:00
postUpCmd , postDownCmd = firewallIPTablesCommandsCreateEgress ( node . Interface , gateway . Interface , node . EgressGatewayNatEnabled , ipv4 , ipv6 )
2022-07-19 19:20:31 +08:00
}
2022-05-09 01:37:22 +08:00
}
if node . OS == "freebsd" {
2022-08-25 18:59:12 +08:00
// spacing around ; is important for later parsing of postup/postdown in wireguard/common.go
2022-05-09 01:37:22 +08:00
postUpCmd = "kldload ipfw ipfw_nat ; "
postUpCmd += "ipfw disable one_pass ; "
postUpCmd += "ipfw nat 1 config if " + gateway . Interface + " same_ports unreg_only reset ; "
postUpCmd += "ipfw add 64000 reass all from any to any in ; "
postUpCmd += "ipfw add 64000 nat 1 ip from any to any in via " + gateway . Interface + " ; "
postUpCmd += "ipfw add 64000 check-state ; "
postUpCmd += "ipfw add 64000 nat 1 ip from any to any out via " + gateway . Interface + " ; "
postUpCmd += "ipfw add 65534 allow ip from any to any ; "
postDownCmd = "ipfw delete 64000 ; "
postDownCmd += "ipfw delete 65534 ; "
postDownCmd += "kldunload ipfw_nat ipfw"
}
2021-12-08 01:46:55 +08:00
if gateway . PostUp != "" {
postUpCmd = gateway . PostUp
}
if gateway . PostDown != "" {
postDownCmd = gateway . PostDown
}
if node . PostUp != "" {
if ! strings . Contains ( node . PostUp , postUpCmd ) {
2022-09-27 01:55:08 +08:00
postUpCmd = node . PostUp + postUpCmd
2021-12-08 01:46:55 +08:00
}
}
if node . PostDown != "" {
if ! strings . Contains ( node . PostDown , postDownCmd ) {
2022-09-27 01:55:08 +08:00
postDownCmd = node . PostDown + postDownCmd
2021-12-08 01:46:55 +08:00
}
}
2022-01-11 23:58:20 +08:00
2021-12-08 01:46:55 +08:00
node . PostUp = postUpCmd
node . PostDown = postDownCmd
node . SetLastModified ( )
nodeData , err := json . Marshal ( & node )
if err != nil {
return node , err
}
2022-01-11 23:58:20 +08:00
if err = database . Insert ( node . ID , string ( nodeData ) , database . NODES_TABLE_NAME ) ; err != nil {
2022-12-20 04:15:35 +08:00
return models . LegacyNode { } , err
2021-12-08 01:46:55 +08:00
}
return node , nil
}
2022-01-11 23:58:20 +08:00
// ValidateEgressGateway - validates the egress gateway model
2021-12-08 01:46:55 +08:00
func ValidateEgressGateway ( gateway models . EgressGatewayRequest ) error {
var err error
empty := len ( gateway . Ranges ) == 0
if empty {
err = errors . New ( "IP Ranges Cannot Be Empty" )
}
empty = gateway . Interface == ""
if empty {
2022-02-09 00:01:44 +08:00
err = errors . New ( "interface cannot be empty" )
2021-12-08 01:46:55 +08:00
}
return err
}
// DeleteEgressGateway - deletes egress from node
2022-12-20 04:15:35 +08:00
func DeleteEgressGateway ( network , nodeid string ) ( models . LegacyNode , error ) {
2021-12-08 01:46:55 +08:00
2022-01-11 06:52:21 +08:00
node , err := GetNodeByID ( nodeid )
2021-12-08 01:46:55 +08:00
if err != nil {
2022-12-20 04:15:35 +08:00
return models . LegacyNode { } , err
2021-12-08 01:46:55 +08:00
}
node . IsEgressGateway = "no"
node . EgressGatewayRanges = [ ] string { }
2022-08-08 10:22:54 +08:00
node . EgressGatewayRequest = models . EgressGatewayRequest { } // remove preserved request as the egress gateway is gone
2022-08-08 11:28:10 +08:00
// needed in case we don't preserve a gateway (i.e., no ingress to preserve)
node . PostUp = ""
node . PostDown = ""
2022-09-02 23:01:19 +08:00
cidrs := [ ] string { }
cidrs = append ( cidrs , node . IngressGatewayRange )
cidrs = append ( cidrs , node . IngressGatewayRange6 )
ipv4 , ipv6 := getNetworkProtocols ( cidrs )
2022-08-07 06:00:20 +08:00
logger . Log ( 3 , "deleting egress gateway firewall in use is '" , node . FirewallInUse , "'" )
2021-12-08 01:46:55 +08:00
if node . IsIngressGateway == "yes" { // check if node is still an ingress gateway before completely deleting postdown/up rules
2022-08-04 02:36:16 +08:00
// still have an ingress gateway so preserve it
2022-05-09 01:37:22 +08:00
if node . OS == "linux" {
2022-08-04 03:18:04 +08:00
switch node . FirewallInUse {
case models . FIREWALL_NFTABLES :
// nftables only supported on Linux
2022-09-02 23:01:19 +08:00
// assumes chains eg FORWARD and postrouting already exist
2022-08-07 06:00:20 +08:00
logger . Log ( 3 , "deleting egress gateway nftables is present" )
2022-08-08 10:22:54 +08:00
node . PostUp , node . PostDown = firewallNFTCommandsCreateIngress ( node . Interface )
2022-08-04 03:18:04 +08:00
default :
2022-08-07 06:00:20 +08:00
logger . Log ( 3 , "deleting egress gateway nftables is not present" )
2022-09-02 23:01:19 +08:00
node . PostUp , node . PostDown = firewallIPTablesCommandsCreateIngress ( node . Interface , ipv4 , ipv6 )
2022-08-02 02:12:25 +08:00
}
2022-05-09 01:37:22 +08:00
}
2022-08-04 02:36:16 +08:00
// no need to preserve ingress gateway on FreeBSD as ingress is not supported on that OS
2021-12-08 01:46:55 +08:00
}
node . SetLastModified ( )
2022-01-12 00:59:26 +08:00
2021-12-08 01:46:55 +08:00
data , err := json . Marshal ( & node )
if err != nil {
2022-12-20 04:15:35 +08:00
return models . LegacyNode { } , err
2021-12-08 01:46:55 +08:00
}
2022-01-12 00:59:26 +08:00
if err = database . Insert ( node . ID , string ( data ) , database . NODES_TABLE_NAME ) ; err != nil {
2022-12-20 04:15:35 +08:00
return models . LegacyNode { } , err
2021-12-08 01:46:55 +08:00
}
return node , nil
}
// CreateIngressGateway - creates an ingress gateway
2022-12-20 04:15:35 +08:00
func CreateIngressGateway ( netid string , nodeid string , failover bool ) ( models . LegacyNode , error ) {
2021-12-08 01:46:55 +08:00
2022-08-02 02:12:25 +08:00
var postUpCmd , postDownCmd string
2022-01-11 06:52:21 +08:00
node , err := GetNodeByID ( nodeid )
2022-12-13 20:06:36 +08:00
if node . FirewallInUse == models . FIREWALL_NONE {
2022-12-20 04:15:35 +08:00
return models . LegacyNode { } , errors . New ( "firewall is not supported for ingress gateways" )
2022-12-13 20:06:36 +08:00
}
2021-12-08 01:46:55 +08:00
if err != nil {
2022-12-20 04:15:35 +08:00
return models . LegacyNode { } , err
2021-12-08 01:46:55 +08:00
}
network , err := GetParentNetwork ( netid )
if err != nil {
2022-12-20 04:15:35 +08:00
return models . LegacyNode { } , err
2021-12-08 01:46:55 +08:00
}
node . IsIngressGateway = "yes"
2022-09-02 23:01:19 +08:00
cidrs := [ ] string { }
cidrs = append ( cidrs , network . AddressRange )
cidrs = append ( cidrs , network . AddressRange6 )
2021-12-08 01:46:55 +08:00
node . IngressGatewayRange = network . AddressRange
2022-09-02 23:01:19 +08:00
node . IngressGatewayRange6 = network . AddressRange6
ipv4 , ipv6 := getNetworkProtocols ( cidrs )
2022-10-06 01:30:27 +08:00
//no support for ipv6 and ip6tables in netmaker container
if node . IsServer == "yes" {
ipv6 = false
}
2022-08-07 06:00:20 +08:00
logger . Log ( 3 , "creating ingress gateway firewall in use is '" , node . FirewallInUse , "'" )
2022-08-04 03:18:04 +08:00
switch node . FirewallInUse {
case models . FIREWALL_NFTABLES :
// nftables only supported on Linux
2022-09-02 23:01:19 +08:00
// assumes chains eg FORWARD and postrouting already exist
2022-08-07 06:00:20 +08:00
logger . Log ( 3 , "creating ingress gateway nftables is present" )
2022-08-08 10:22:54 +08:00
postUpCmd , postDownCmd = firewallNFTCommandsCreateIngress ( node . Interface )
2022-08-04 03:18:04 +08:00
default :
2022-08-07 06:00:20 +08:00
logger . Log ( 3 , "creating ingress gateway using nftables is not present" )
2022-09-01 20:10:49 +08:00
postUpCmd , postDownCmd = firewallIPTablesCommandsCreateIngress ( node . Interface , ipv4 , ipv6 )
2022-08-02 02:12:25 +08:00
}
2021-12-08 01:46:55 +08:00
if node . PostUp != "" {
if ! strings . Contains ( node . PostUp , postUpCmd ) {
2022-09-20 23:50:15 +08:00
postUpCmd = node . PostUp + postUpCmd
2021-12-08 01:46:55 +08:00
}
}
if node . PostDown != "" {
if ! strings . Contains ( node . PostDown , postDownCmd ) {
2022-09-20 23:50:15 +08:00
postDownCmd = node . PostDown + postDownCmd
2021-12-08 01:46:55 +08:00
}
}
node . SetLastModified ( )
node . PostUp = postUpCmd
node . PostDown = postDownCmd
node . UDPHolePunch = "no"
2022-09-29 02:59:21 +08:00
if failover && servercfg . Is_EE {
node . Failover = "yes"
}
2021-12-08 01:46:55 +08:00
data , err := json . Marshal ( & node )
if err != nil {
2022-12-20 04:15:35 +08:00
return models . LegacyNode { } , err
2021-12-08 01:46:55 +08:00
}
2022-01-12 00:59:26 +08:00
err = database . Insert ( node . ID , string ( data ) , database . NODES_TABLE_NAME )
2021-12-08 01:46:55 +08:00
if err != nil {
2022-12-20 04:15:35 +08:00
return models . LegacyNode { } , err
2021-12-08 01:46:55 +08:00
}
err = SetNetworkNodesLastModified ( netid )
return node , err
}
// DeleteIngressGateway - deletes an ingress gateway
2022-12-20 04:15:35 +08:00
func DeleteIngressGateway ( networkName string , nodeid string ) ( models . LegacyNode , bool , error ) {
2021-12-08 01:46:55 +08:00
2022-01-12 02:05:22 +08:00
node , err := GetNodeByID ( nodeid )
2021-12-08 01:46:55 +08:00
if err != nil {
2022-12-20 04:15:35 +08:00
return models . LegacyNode { } , false , err
2021-12-08 01:46:55 +08:00
}
network , err := GetParentNetwork ( networkName )
if err != nil {
2022-12-20 04:15:35 +08:00
return models . LegacyNode { } , false , err
2021-12-08 01:46:55 +08:00
}
// delete ext clients belonging to ingress gateway
2022-01-12 02:05:22 +08:00
if err = DeleteGatewayExtClients ( node . ID , networkName ) ; err != nil {
2022-12-20 04:15:35 +08:00
return models . LegacyNode { } , false , err
2021-12-08 01:46:55 +08:00
}
2022-08-04 02:36:16 +08:00
logger . Log ( 3 , "deleting ingress gateway" )
2022-09-30 02:49:44 +08:00
wasFailover := node . Failover == "yes"
2022-10-17 23:04:17 +08:00
if node . IsServer != "yes" {
node . UDPHolePunch = network . DefaultUDPHolePunch
}
2021-12-08 01:46:55 +08:00
node . LastModified = time . Now ( ) . Unix ( )
node . IsIngressGateway = "no"
node . IngressGatewayRange = ""
2022-09-29 02:59:21 +08:00
node . Failover = "no"
2021-12-08 01:46:55 +08:00
2022-08-07 06:00:20 +08:00
// default to removing postup and postdown
node . PostUp = ""
node . PostDown = ""
2022-08-08 11:28:10 +08:00
2022-08-08 10:22:54 +08:00
logger . Log ( 3 , "deleting ingress gateway firewall in use is '" , node . FirewallInUse , "' and isEgressGateway is" , node . IsEgressGateway )
if node . EgressGatewayRequest . NodeID != "" {
_ , err := CreateEgressGateway ( node . EgressGatewayRequest )
if err != nil {
logger . Log ( 0 , fmt . Sprintf ( "failed to create egress gateway on node [%s] on network [%s]: %v" ,
node . EgressGatewayRequest . NodeID , node . EgressGatewayRequest . NetID , err ) )
2022-08-04 02:36:16 +08:00
}
}
2021-12-08 01:46:55 +08:00
data , err := json . Marshal ( & node )
if err != nil {
2022-12-20 04:15:35 +08:00
return models . LegacyNode { } , false , err
2021-12-08 01:46:55 +08:00
}
2022-01-12 00:59:26 +08:00
err = database . Insert ( node . ID , string ( data ) , database . NODES_TABLE_NAME )
2021-12-08 01:46:55 +08:00
if err != nil {
2022-12-20 04:15:35 +08:00
return models . LegacyNode { } , wasFailover , err
2021-12-08 01:46:55 +08:00
}
err = SetNetworkNodesLastModified ( networkName )
2022-09-30 02:49:44 +08:00
return node , wasFailover , err
2021-12-08 01:46:55 +08:00
}
// DeleteGatewayExtClients - deletes ext clients based on gateway (mac) of ingress node and network
func DeleteGatewayExtClients ( gatewayID string , networkName string ) error {
currentExtClients , err := GetNetworkExtClients ( networkName )
if err != nil && ! database . IsEmptyRecord ( err ) {
return err
}
for _ , extClient := range currentExtClients {
if extClient . IngressGatewayID == gatewayID {
if err = DeleteExtClient ( networkName , extClient . ClientID ) ; err != nil {
logger . Log ( 1 , "failed to remove ext client" , extClient . ClientID )
continue
}
}
}
return nil
}
2022-08-08 10:22:54 +08:00
// firewallNFTCommandsCreateIngress - used to centralize firewall command maintenance for creating an ingress gateway using the nftables firewall.
func firewallNFTCommandsCreateIngress ( networkInterface string ) ( string , string ) {
2022-08-25 18:59:12 +08:00
// spacing around ; is important for later parsing of postup/postdown in wireguard/common.go
2022-08-08 10:22:54 +08:00
postUp := "nft add table ip filter ; "
postUp += "nft add chain ip filter FORWARD ; "
postUp += "nft add rule ip filter FORWARD iifname " + networkInterface + " counter accept ; "
postUp += "nft add rule ip filter FORWARD oifname " + networkInterface + " counter accept ; "
postUp += "nft add table nat ; "
2022-09-02 23:01:19 +08:00
postUp += "nft add chain nat postrouting ; "
2022-10-19 13:56:42 +08:00
postUp += "nft add rule ip nat postrouting oifname " + networkInterface + " counter masquerade ; "
2022-08-08 10:22:54 +08:00
// doesn't remove potentially empty tables or chains
2022-08-25 20:35:48 +08:00
postDown := "nft flush table filter ; "
postDown += "nft flush table nat ; "
2022-08-08 10:22:54 +08:00
return postUp , postDown
}
// firewallNFTCommandsCreateEgress - used to centralize firewall command maintenance for creating an egress gateway using the nftables firewall.
2022-09-02 21:25:17 +08:00
func firewallNFTCommandsCreateEgress ( networkInterface string , gatewayInterface string , gatewayranges [ ] string , egressNatEnabled string , ipv4 , ipv6 bool ) ( string , string ) {
2022-08-25 18:59:12 +08:00
// spacing around ; is important for later parsing of postup/postdown in wireguard/common.go
2022-09-02 21:25:17 +08:00
postUp := ""
postDown := ""
if ipv4 {
postUp += "nft add table ip filter ; "
postUp += "nft add chain ip filter forward ; "
postUp += "nft add rule filter forward ct state related,established accept ; "
postUp += "nft add rule ip filter forward iifname " + networkInterface + " accept ; "
postUp += "nft add rule ip filter forward oifname " + networkInterface + " accept ; "
postUp += "nft add table nat ; "
postUp += "nft 'add chain ip nat prerouting { type nat hook prerouting priority 0 ;}' ; "
postUp += "nft 'add chain ip nat postrouting { type nat hook postrouting priority 0 ;}' ; "
postDown += "nft flush table filter ; "
if egressNatEnabled == "yes" {
postUp += "nft add table nat ; "
postUp += "nft add chain nat postrouting ; "
postUp += "nft add rule ip nat postrouting oifname " + gatewayInterface + " counter masquerade ; "
postDown += "nft flush table nat ; "
}
2022-08-26 02:55:03 +08:00
}
2022-09-02 21:25:17 +08:00
if ipv6 {
postUp += "nft add table ip6 filter ; "
postUp += "nft add chain ip6 filter forward ; "
2022-09-02 23:01:19 +08:00
postUp += "nft add rule ip6 filter forward ct state related,established accept ; "
2022-09-02 21:25:17 +08:00
postUp += "nft add rule ip6 filter forward iifname " + networkInterface + " accept ; "
postUp += "nft add rule ip6 filter forward oifname " + networkInterface + " accept ; "
2022-08-08 10:22:54 +08:00
2022-09-02 23:01:19 +08:00
postDown += "nft flush table ip6 filter ; "
2022-08-08 10:22:54 +08:00
2022-09-02 21:25:17 +08:00
if egressNatEnabled == "yes" {
postUp += "nft add table ip6 nat ; "
2022-09-02 23:01:19 +08:00
postUp += "nft 'add chain ip6 nat prerouting { type nat hook prerouting priority 0 ;}' ; "
postUp += "nft 'add chain ip6 nat postrouting { type nat hook postrouting priority 0 ;}' ; "
postUp += "nft add rule ip6 nat postrouting oifname " + gatewayInterface + " masquerade ; "
2022-08-08 10:22:54 +08:00
2022-09-02 23:01:19 +08:00
postDown += "nft flush table ip6 nat ; "
2022-09-02 21:25:17 +08:00
}
2022-08-08 10:22:54 +08:00
}
return postUp , postDown
}
// firewallIPTablesCommandsCreateIngress - used to centralize firewall command maintenance for creating an ingress gateway using the iptables firewall.
2022-09-01 20:10:49 +08:00
func firewallIPTablesCommandsCreateIngress ( networkInterface string , ipv4 , ipv6 bool ) ( string , string ) {
postUp := ""
postDown := ""
if ipv4 {
// spacing around ; is important for later parsing of postup/postdown in wireguard/common.go
postUp += "iptables -A FORWARD -i " + networkInterface + " -j ACCEPT ; "
postUp += "iptables -A FORWARD -o " + networkInterface + " -j ACCEPT ; "
2022-09-03 04:33:48 +08:00
postUp += "iptables -t nat -A POSTROUTING -o " + networkInterface + " -j MASQUERADE ; "
2022-08-08 10:22:54 +08:00
2022-09-01 20:10:49 +08:00
// doesn't remove potentially empty tables or chains
postDown += "iptables -D FORWARD -i " + networkInterface + " -j ACCEPT ; "
postDown += "iptables -D FORWARD -o " + networkInterface + " -j ACCEPT ; "
2022-09-03 04:33:48 +08:00
postDown += "iptables -t nat -D POSTROUTING -o " + networkInterface + " -j MASQUERADE ; "
2022-09-01 20:10:49 +08:00
}
if ipv6 {
// spacing around ; is important for later parsing of postup/postdown in wireguard/common.go
postUp += "ip6tables -A FORWARD -i " + networkInterface + " -j ACCEPT ; "
postUp += "ip6tables -A FORWARD -o " + networkInterface + " -j ACCEPT ; "
2022-09-20 23:50:15 +08:00
postUp += "ip6tables -t nat -A POSTROUTING -o " + networkInterface + " -j MASQUERADE ; "
2022-08-08 10:22:54 +08:00
2022-09-01 20:10:49 +08:00
// doesn't remove potentially empty tables or chains
postDown += "ip6tables -D FORWARD -i " + networkInterface + " -j ACCEPT ; "
postDown += "ip6tables -D FORWARD -o " + networkInterface + " -j ACCEPT ; "
2022-09-20 23:50:15 +08:00
postDown += "ip6tables -t nat -D POSTROUTING -o " + networkInterface + " -j MASQUERADE ; "
2022-09-01 20:10:49 +08:00
}
2022-08-08 10:22:54 +08:00
return postUp , postDown
}
// firewallIPTablesCommandsCreateEgress - used to centralize firewall command maintenance for creating an egress gateway using the iptables firewall.
2022-09-01 20:10:49 +08:00
func firewallIPTablesCommandsCreateEgress ( networkInterface string , gatewayInterface string , egressNatEnabled string , ipv4 , ipv6 bool ) ( string , string ) {
2022-08-25 18:59:12 +08:00
// spacing around ; is important for later parsing of postup/postdown in wireguard/common.go
2022-09-01 20:10:49 +08:00
postUp := ""
postDown := ""
if ipv4 {
postUp += "iptables -A FORWARD -i " + networkInterface + " -j ACCEPT ; "
2022-09-20 23:50:15 +08:00
postUp += "iptables -A FORWARD -o " + networkInterface + " -j ACCEPT ; "
2022-09-01 20:10:49 +08:00
postDown += "iptables -D FORWARD -i " + networkInterface + " -j ACCEPT ; "
2022-09-03 04:33:48 +08:00
postDown += "iptables -D FORWARD -o " + networkInterface + " -j ACCEPT ; "
2022-09-01 20:10:49 +08:00
if egressNatEnabled == "yes" {
2022-09-20 23:50:15 +08:00
postUp += "iptables -t nat -A POSTROUTING -o " + gatewayInterface + " -j MASQUERADE ; "
postDown += "iptables -t nat -D POSTROUTING -o " + gatewayInterface + " -j MASQUERADE ; "
2022-09-01 20:10:49 +08:00
}
2022-08-08 10:22:54 +08:00
}
2022-09-01 20:10:49 +08:00
if ipv6 {
postUp += "ip6tables -A FORWARD -i " + networkInterface + " -j ACCEPT ; "
2022-09-03 04:33:48 +08:00
postUp += "ip6tables -A FORWARD -o " + networkInterface + " -j ACCEPT ; "
2022-09-01 20:10:49 +08:00
postDown += "ip6tables -D FORWARD -i " + networkInterface + " -j ACCEPT ; "
2022-09-03 04:33:48 +08:00
postDown += "ip6tables -D FORWARD -o " + networkInterface + " -j ACCEPT ; "
2022-08-08 10:22:54 +08:00
2022-09-01 20:10:49 +08:00
if egressNatEnabled == "yes" {
2022-09-20 23:50:15 +08:00
postUp += "ip6tables -t nat -A POSTROUTING -o " + gatewayInterface + " -j MASQUERADE ; "
postDown += "ip6tables -t nat -D POSTROUTING -o " + gatewayInterface + " -j MASQUERADE ; "
2022-09-01 20:10:49 +08:00
}
}
2022-08-08 10:22:54 +08:00
return postUp , postDown
}