-
Notifications
You must be signed in to change notification settings - Fork 20
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
15 changed files
with
1,099 additions
and
481 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,116 @@ | ||
package controllers | ||
|
||
import ( | ||
"github.com/go-logr/logr" | ||
|
||
"github.com/kakao/network-node-manager/pkg/configs" | ||
"github.com/kakao/network-node-manager/pkg/iptables" | ||
) | ||
|
||
// Constants | ||
const ( | ||
ChainFilterInput = "INPUT" | ||
ChainNATPrerouting = "PREROUTING" | ||
ChainNATOutput = "OUTPUT" | ||
|
||
ChainNATKubeMarkMasq = "KUBE-MARK-MASQ" | ||
|
||
ChainFilterBaseInput = "NMANAGER_INPUT" | ||
ChainNATBasePrerouting = "NMANAGER_PREROUTING" | ||
ChainNATBaseOutput = "NMANAGER_OUTPUT" | ||
|
||
ChainFilterDropInvalidInput = "NMANAGER_DROP_INVALID_INPUT" | ||
ChainNATExternalClusterPrerouting = "NMANAGER_EX_CLUS_PREROUTING" | ||
ChainNATExternalClusterOutput = "NMANAGER_EX_CLUS_OUTPUT" | ||
) | ||
|
||
func initBaseChains(logger logr.Logger) error { | ||
// Get network stack config | ||
configIPv4Enabled, configIPv6Enabled, err := configs.GetConfigNetStack() | ||
if err != nil { | ||
return err | ||
} | ||
|
||
// IPv4 | ||
if configIPv4Enabled { | ||
// Create base chain in tables | ||
out, err := iptables.CreateChainIPv4(iptables.TableFilter, ChainFilterBaseInput) | ||
if err != nil { | ||
logger.Error(err, out) | ||
return err | ||
} | ||
out, err = iptables.CreateChainIPv4(iptables.TableNAT, ChainNATBasePrerouting) | ||
if err != nil { | ||
logger.Error(err, out) | ||
return err | ||
} | ||
out, err = iptables.CreateChainIPv4(iptables.TableNAT, ChainNATBaseOutput) | ||
if err != nil { | ||
logger.Error(err, out) | ||
return err | ||
} | ||
|
||
// Create jump rule to each chain in tables | ||
ruleJumpFilterInput := []string{"-j", ChainFilterBaseInput} | ||
out, err = iptables.CreateRuleFirstIPv4(iptables.TableFilter, ChainFilterInput, "", ruleJumpFilterInput...) | ||
if err != nil { | ||
logger.Error(err, out) | ||
return err | ||
} | ||
ruleJumpNATPre := []string{"-j", ChainNATBasePrerouting} | ||
out, err = iptables.CreateRuleFirstIPv4(iptables.TableNAT, ChainNATPrerouting, "", ruleJumpNATPre...) | ||
if err != nil { | ||
logger.Error(err, out) | ||
return err | ||
} | ||
ruleJumpNATOut := []string{"-j", ChainNATBaseOutput} | ||
out, err = iptables.CreateRuleFirstIPv4(iptables.TableNAT, ChainNATOutput, "", ruleJumpNATOut...) | ||
if err != nil { | ||
logger.Error(err, out) | ||
return err | ||
} | ||
|
||
} | ||
|
||
// IPv6 | ||
if configIPv6Enabled { | ||
// Create base chain in nat table | ||
out, err := iptables.CreateChainIPv6(iptables.TableFilter, ChainFilterBaseInput) | ||
if err != nil { | ||
logger.Error(err, out) | ||
return err | ||
} | ||
out, err = iptables.CreateChainIPv6(iptables.TableNAT, ChainNATBasePrerouting) | ||
if err != nil { | ||
logger.Error(err, out) | ||
return err | ||
} | ||
out, err = iptables.CreateChainIPv6(iptables.TableNAT, ChainNATBaseOutput) | ||
if err != nil { | ||
logger.Error(err, out) | ||
return err | ||
} | ||
|
||
// Create jump rule to each chain in nat table | ||
ruleJumpFilterInput := []string{"-j", ChainFilterBaseInput} | ||
out, err = iptables.CreateRuleFirstIPv6(iptables.TableFilter, ChainFilterInput, "", ruleJumpFilterInput...) | ||
if err != nil { | ||
logger.Error(err, out) | ||
return err | ||
} | ||
ruleJumpNATPre := []string{"-j", ChainNATBasePrerouting} | ||
out, err = iptables.CreateRuleFirstIPv6(iptables.TableNAT, ChainNATPrerouting, "", ruleJumpNATPre...) | ||
if err != nil { | ||
logger.Error(err, out) | ||
return err | ||
} | ||
ruleJumpNATOut := []string{"-j", ChainNATBaseOutput} | ||
out, err = iptables.CreateRuleFirstIPv6(iptables.TableNAT, ChainNATOutput, "", ruleJumpNATOut...) | ||
if err != nil { | ||
logger.Error(err, out) | ||
return err | ||
} | ||
} | ||
|
||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,108 @@ | ||
package controllers | ||
|
||
import ( | ||
"github.com/go-logr/logr" | ||
|
||
"github.com/kakao/network-node-manager/pkg/iptables" | ||
) | ||
|
||
func createRulesDropInvalidInput(logger logr.Logger) error { | ||
if err := initBaseChains(logger); err != nil { | ||
logger.Error(err, "failed to init base chain for externalIP to clusterIP Rules") | ||
return err | ||
} | ||
|
||
// IPv4 | ||
if configIPv4Enabled { | ||
// Create chain | ||
out, err := iptables.CreateChainIPv4(iptables.TableFilter, ChainFilterDropInvalidInput) | ||
if err != nil { | ||
logger.Error(err, out) | ||
return err | ||
} | ||
|
||
// Set drop rule | ||
ruleDrop := []string{"-m", "conntrack", "--ctstate", "INVALID", "-j", "DROP"} | ||
out, err = iptables.CreateRuleFirstIPv4(iptables.TableFilter, ChainFilterDropInvalidInput, "", ruleDrop...) | ||
if err != nil { | ||
logger.Error(err, out) | ||
return err | ||
} | ||
|
||
// Set jump rule | ||
ruleJump := []string{"-j", ChainFilterDropInvalidInput} | ||
out, err = iptables.CreateRuleFirstIPv4(iptables.TableFilter, ChainFilterBaseInput, "", ruleJump...) | ||
if err != nil { | ||
logger.Error(err, out) | ||
return err | ||
} | ||
} | ||
|
||
// IPv6 | ||
if configIPv6Enabled { | ||
// Create chain | ||
out, err := iptables.CreateChainIPv6(iptables.TableFilter, ChainFilterDropInvalidInput) | ||
if err != nil { | ||
logger.Error(err, out) | ||
return err | ||
} | ||
|
||
// Set drop rule | ||
ruleDrop := []string{"-m", "conntrack", "--ctstate", "INVALID", "-j", "DROP"} | ||
out, err = iptables.CreateRuleFirstIPv6(iptables.TableFilter, ChainFilterDropInvalidInput, "", ruleDrop...) | ||
if err != nil { | ||
logger.Error(err, out) | ||
return err | ||
} | ||
|
||
// Set jump rule | ||
ruleJump := []string{"-j", ChainFilterDropInvalidInput} | ||
out, err = iptables.CreateRuleFirstIPv6(iptables.TableFilter, ChainFilterBaseInput, "", ruleJump...) | ||
if err != nil { | ||
logger.Error(err, out) | ||
return err | ||
} | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func deleteRulesDropInvalidInput(logger logr.Logger) error { | ||
// IPv4 | ||
if configIPv4Enabled { | ||
// Delete jump rule | ||
ruleJump := []string{"-j", ChainFilterDropInvalidInput} | ||
out, err := iptables.DeleteRuleIPv4(iptables.TableFilter, ChainFilterBaseInput, "", ruleJump...) | ||
if err != nil { | ||
logger.Error(err, out) | ||
return err | ||
} | ||
|
||
// Delete chain | ||
out, err = iptables.DeleteChainIPv4(iptables.TableFilter, ChainFilterDropInvalidInput) | ||
if err != nil { | ||
logger.Error(err, out) | ||
return err | ||
} | ||
} | ||
|
||
// IPv6 | ||
if configIPv6Enabled { | ||
// Delete jump rule | ||
ruleJump := []string{"-j", ChainFilterDropInvalidInput} | ||
out, err := iptables.DeleteRuleIPv6(iptables.TableFilter, ChainFilterBaseInput, "", ruleJump...) | ||
if err != nil { | ||
logger.Error(err, out) | ||
return err | ||
} | ||
|
||
// Delete chain | ||
out, err = iptables.DeleteChainIPv6(iptables.TableFilter, ChainFilterDropInvalidInput) | ||
if err != nil { | ||
logger.Error(err, out) | ||
return err | ||
} | ||
} | ||
|
||
return nil | ||
} |
Oops, something went wrong.