|
@@ -23,6 +23,9 @@ type Policy string
|
|
// Table refers to Nat, Filter or Mangle.
|
|
// Table refers to Nat, Filter or Mangle.
|
|
type Table string
|
|
type Table string
|
|
|
|
|
|
|
|
+// IPVersion refers to IP version, v4 or v6
|
|
|
|
+type IPVersion string
|
|
|
|
+
|
|
const (
|
|
const (
|
|
// Append appends the rule at the end of the chain.
|
|
// Append appends the rule at the end of the chain.
|
|
Append Action = "-A"
|
|
Append Action = "-A"
|
|
@@ -40,10 +43,15 @@ const (
|
|
Drop Policy = "DROP"
|
|
Drop Policy = "DROP"
|
|
// Accept is the default iptables ACCEPT policy
|
|
// Accept is the default iptables ACCEPT policy
|
|
Accept Policy = "ACCEPT"
|
|
Accept Policy = "ACCEPT"
|
|
|
|
+ // IPv4 is version 4
|
|
|
|
+ IPv4 IPVersion = "IPV4"
|
|
|
|
+ // IPv6 is version 6
|
|
|
|
+ IPv6 IPVersion = "IPV6"
|
|
)
|
|
)
|
|
|
|
|
|
var (
|
|
var (
|
|
iptablesPath string
|
|
iptablesPath string
|
|
|
|
+ ip6tablesPath string
|
|
supportsXlock = false
|
|
supportsXlock = false
|
|
supportsCOpt = false
|
|
supportsCOpt = false
|
|
xLockWaitMsg = "Another app is currently holding the xtables lock"
|
|
xLockWaitMsg = "Another app is currently holding the xtables lock"
|
|
@@ -54,11 +62,17 @@ var (
|
|
initOnce sync.Once
|
|
initOnce sync.Once
|
|
)
|
|
)
|
|
|
|
|
|
|
|
+// IPTable defines struct with IPVersion
|
|
|
|
+type IPTable struct {
|
|
|
|
+ Version IPVersion
|
|
|
|
+}
|
|
|
|
+
|
|
// ChainInfo defines the iptables chain.
|
|
// ChainInfo defines the iptables chain.
|
|
type ChainInfo struct {
|
|
type ChainInfo struct {
|
|
Name string
|
|
Name string
|
|
Table Table
|
|
Table Table
|
|
HairpinMode bool
|
|
HairpinMode bool
|
|
|
|
+ IPTable IPTable
|
|
}
|
|
}
|
|
|
|
|
|
// ChainError is returned to represent errors during ip table operation.
|
|
// ChainError is returned to represent errors during ip table operation.
|
|
@@ -80,6 +94,11 @@ func probe() {
|
|
if out, err := exec.Command(path, "--wait", "-t", "nat", "-L", "-n").CombinedOutput(); err != nil {
|
|
if out, err := exec.Command(path, "--wait", "-t", "nat", "-L", "-n").CombinedOutput(); err != nil {
|
|
logrus.Warnf("Running iptables --wait -t nat -L -n failed with message: `%s`, error: %v", strings.TrimSpace(string(out)), err)
|
|
logrus.Warnf("Running iptables --wait -t nat -L -n failed with message: `%s`, error: %v", strings.TrimSpace(string(out)), err)
|
|
}
|
|
}
|
|
|
|
+ _, err = exec.LookPath("ip6tables")
|
|
|
|
+ if err != nil {
|
|
|
|
+ logrus.Warnf("Failed to find ip6tables: %v", err)
|
|
|
|
+ return
|
|
|
|
+ }
|
|
}
|
|
}
|
|
|
|
|
|
func initFirewalld() {
|
|
func initFirewalld() {
|
|
@@ -94,6 +113,11 @@ func detectIptables() {
|
|
return
|
|
return
|
|
}
|
|
}
|
|
iptablesPath = path
|
|
iptablesPath = path
|
|
|
|
+ path, err = exec.LookPath("ip6tables")
|
|
|
|
+ if err != nil {
|
|
|
|
+ return
|
|
|
|
+ }
|
|
|
|
+ ip6tablesPath = path
|
|
supportsXlock = exec.Command(iptablesPath, "--wait", "-L", "-n").Run() == nil
|
|
supportsXlock = exec.Command(iptablesPath, "--wait", "-L", "-n").Run() == nil
|
|
mj, mn, mc, err := GetVersion()
|
|
mj, mn, mc, err := GetVersion()
|
|
if err != nil {
|
|
if err != nil {
|
|
@@ -118,20 +142,26 @@ func initCheck() error {
|
|
return nil
|
|
return nil
|
|
}
|
|
}
|
|
|
|
|
|
|
|
+// GetIptable returns an instance of IPTable with specified version
|
|
|
|
+func GetIptable(version IPVersion) *IPTable {
|
|
|
|
+ return &IPTable{Version: version}
|
|
|
|
+}
|
|
|
|
+
|
|
// NewChain adds a new chain to ip table.
|
|
// NewChain adds a new chain to ip table.
|
|
-func NewChain(name string, table Table, hairpinMode bool) (*ChainInfo, error) {
|
|
|
|
|
|
+func (iptable IPTable) NewChain(name string, table Table, hairpinMode bool) (*ChainInfo, error) {
|
|
c := &ChainInfo{
|
|
c := &ChainInfo{
|
|
Name: name,
|
|
Name: name,
|
|
Table: table,
|
|
Table: table,
|
|
HairpinMode: hairpinMode,
|
|
HairpinMode: hairpinMode,
|
|
|
|
+ IPTable: iptable,
|
|
}
|
|
}
|
|
if string(c.Table) == "" {
|
|
if string(c.Table) == "" {
|
|
c.Table = Filter
|
|
c.Table = Filter
|
|
}
|
|
}
|
|
|
|
|
|
// Add chain if it doesn't exist
|
|
// Add chain if it doesn't exist
|
|
- if _, err := Raw("-t", string(c.Table), "-n", "-L", c.Name); err != nil {
|
|
|
|
- if output, err := Raw("-t", string(c.Table), "-N", c.Name); err != nil {
|
|
|
|
|
|
+ if _, err := iptable.Raw("-t", string(c.Table), "-n", "-L", c.Name); err != nil {
|
|
|
|
+ if output, err := iptable.Raw("-t", string(c.Table), "-N", c.Name); err != nil {
|
|
return nil, err
|
|
return nil, err
|
|
} else if len(output) != 0 {
|
|
} else if len(output) != 0 {
|
|
return nil, fmt.Errorf("Could not create %s/%s chain: %s", c.Table, c.Name, output)
|
|
return nil, fmt.Errorf("Could not create %s/%s chain: %s", c.Table, c.Name, output)
|
|
@@ -140,8 +170,16 @@ func NewChain(name string, table Table, hairpinMode bool) (*ChainInfo, error) {
|
|
return c, nil
|
|
return c, nil
|
|
}
|
|
}
|
|
|
|
|
|
|
|
+// LoopbackByVersion returns loopback address by version
|
|
|
|
+func (iptable IPTable) LoopbackByVersion() string {
|
|
|
|
+ if iptable.Version == IPv6 {
|
|
|
|
+ return "::1/128"
|
|
|
|
+ }
|
|
|
|
+ return "127.0.0.0/8"
|
|
|
|
+}
|
|
|
|
+
|
|
// ProgramChain is used to add rules to a chain
|
|
// ProgramChain is used to add rules to a chain
|
|
-func ProgramChain(c *ChainInfo, bridgeName string, hairpinMode, enable bool) error {
|
|
|
|
|
|
+func (iptable IPTable) ProgramChain(c *ChainInfo, bridgeName string, hairpinMode, enable bool) error {
|
|
if c.Name == "" {
|
|
if c.Name == "" {
|
|
return errors.New("Could not program chain, missing chain name")
|
|
return errors.New("Could not program chain, missing chain name")
|
|
}
|
|
}
|
|
@@ -165,11 +203,11 @@ func ProgramChain(c *ChainInfo, bridgeName string, hairpinMode, enable bool) err
|
|
"-m", "addrtype",
|
|
"-m", "addrtype",
|
|
"--dst-type", "LOCAL",
|
|
"--dst-type", "LOCAL",
|
|
"-j", c.Name}
|
|
"-j", c.Name}
|
|
- if !Exists(Nat, "PREROUTING", preroute...) && enable {
|
|
|
|
|
|
+ if !iptable.Exists(Nat, "PREROUTING", preroute...) && enable {
|
|
if err := c.Prerouting(Append, preroute...); err != nil {
|
|
if err := c.Prerouting(Append, preroute...); err != nil {
|
|
return fmt.Errorf("Failed to inject %s in PREROUTING chain: %s", c.Name, err)
|
|
return fmt.Errorf("Failed to inject %s in PREROUTING chain: %s", c.Name, err)
|
|
}
|
|
}
|
|
- } else if Exists(Nat, "PREROUTING", preroute...) && !enable {
|
|
|
|
|
|
+ } else if iptable.Exists(Nat, "PREROUTING", preroute...) && !enable {
|
|
if err := c.Prerouting(Delete, preroute...); err != nil {
|
|
if err := c.Prerouting(Delete, preroute...); err != nil {
|
|
return fmt.Errorf("Failed to remove %s in PREROUTING chain: %s", c.Name, err)
|
|
return fmt.Errorf("Failed to remove %s in PREROUTING chain: %s", c.Name, err)
|
|
}
|
|
}
|
|
@@ -179,13 +217,13 @@ func ProgramChain(c *ChainInfo, bridgeName string, hairpinMode, enable bool) err
|
|
"--dst-type", "LOCAL",
|
|
"--dst-type", "LOCAL",
|
|
"-j", c.Name}
|
|
"-j", c.Name}
|
|
if !hairpinMode {
|
|
if !hairpinMode {
|
|
- output = append(output, "!", "--dst", "127.0.0.0/8")
|
|
|
|
|
|
+ output = append(output, "!", "--dst", iptable.LoopbackByVersion())
|
|
}
|
|
}
|
|
- if !Exists(Nat, "OUTPUT", output...) && enable {
|
|
|
|
|
|
+ if !iptable.Exists(Nat, "OUTPUT", output...) && enable {
|
|
if err := c.Output(Append, output...); err != nil {
|
|
if err := c.Output(Append, output...); err != nil {
|
|
return fmt.Errorf("Failed to inject %s in OUTPUT chain: %s", c.Name, err)
|
|
return fmt.Errorf("Failed to inject %s in OUTPUT chain: %s", c.Name, err)
|
|
}
|
|
}
|
|
- } else if Exists(Nat, "OUTPUT", output...) && !enable {
|
|
|
|
|
|
+ } else if iptable.Exists(Nat, "OUTPUT", output...) && !enable {
|
|
if err := c.Output(Delete, output...); err != nil {
|
|
if err := c.Output(Delete, output...); err != nil {
|
|
return fmt.Errorf("Failed to inject %s in OUTPUT chain: %s", c.Name, err)
|
|
return fmt.Errorf("Failed to inject %s in OUTPUT chain: %s", c.Name, err)
|
|
}
|
|
}
|
|
@@ -198,16 +236,16 @@ func ProgramChain(c *ChainInfo, bridgeName string, hairpinMode, enable bool) err
|
|
link := []string{
|
|
link := []string{
|
|
"-o", bridgeName,
|
|
"-o", bridgeName,
|
|
"-j", c.Name}
|
|
"-j", c.Name}
|
|
- if !Exists(Filter, "FORWARD", link...) && enable {
|
|
|
|
|
|
+ if !iptable.Exists(Filter, "FORWARD", link...) && enable {
|
|
insert := append([]string{string(Insert), "FORWARD"}, link...)
|
|
insert := append([]string{string(Insert), "FORWARD"}, link...)
|
|
- if output, err := Raw(insert...); err != nil {
|
|
|
|
|
|
+ if output, err := iptable.Raw(insert...); err != nil {
|
|
return err
|
|
return err
|
|
} else if len(output) != 0 {
|
|
} else if len(output) != 0 {
|
|
return fmt.Errorf("Could not create linking rule to %s/%s: %s", c.Table, c.Name, output)
|
|
return fmt.Errorf("Could not create linking rule to %s/%s: %s", c.Table, c.Name, output)
|
|
}
|
|
}
|
|
- } else if Exists(Filter, "FORWARD", link...) && !enable {
|
|
|
|
|
|
+ } else if iptable.Exists(Filter, "FORWARD", link...) && !enable {
|
|
del := append([]string{string(Delete), "FORWARD"}, link...)
|
|
del := append([]string{string(Delete), "FORWARD"}, link...)
|
|
- if output, err := Raw(del...); err != nil {
|
|
|
|
|
|
+ if output, err := iptable.Raw(del...); err != nil {
|
|
return err
|
|
return err
|
|
} else if len(output) != 0 {
|
|
} else if len(output) != 0 {
|
|
return fmt.Errorf("Could not delete linking rule from %s/%s: %s", c.Table, c.Name, output)
|
|
return fmt.Errorf("Could not delete linking rule from %s/%s: %s", c.Table, c.Name, output)
|
|
@@ -219,16 +257,16 @@ func ProgramChain(c *ChainInfo, bridgeName string, hairpinMode, enable bool) err
|
|
"-m", "conntrack",
|
|
"-m", "conntrack",
|
|
"--ctstate", "RELATED,ESTABLISHED",
|
|
"--ctstate", "RELATED,ESTABLISHED",
|
|
"-j", "ACCEPT"}
|
|
"-j", "ACCEPT"}
|
|
- if !Exists(Filter, "FORWARD", establish...) && enable {
|
|
|
|
|
|
+ if !iptable.Exists(Filter, "FORWARD", establish...) && enable {
|
|
insert := append([]string{string(Insert), "FORWARD"}, establish...)
|
|
insert := append([]string{string(Insert), "FORWARD"}, establish...)
|
|
- if output, err := Raw(insert...); err != nil {
|
|
|
|
|
|
+ if output, err := iptable.Raw(insert...); err != nil {
|
|
return err
|
|
return err
|
|
} else if len(output) != 0 {
|
|
} else if len(output) != 0 {
|
|
return fmt.Errorf("Could not create establish rule to %s: %s", c.Table, output)
|
|
return fmt.Errorf("Could not create establish rule to %s: %s", c.Table, output)
|
|
}
|
|
}
|
|
- } else if Exists(Filter, "FORWARD", establish...) && !enable {
|
|
|
|
|
|
+ } else if iptable.Exists(Filter, "FORWARD", establish...) && !enable {
|
|
del := append([]string{string(Delete), "FORWARD"}, establish...)
|
|
del := append([]string{string(Delete), "FORWARD"}, establish...)
|
|
- if output, err := Raw(del...); err != nil {
|
|
|
|
|
|
+ if output, err := iptable.Raw(del...); err != nil {
|
|
return err
|
|
return err
|
|
} else if len(output) != 0 {
|
|
} else if len(output) != 0 {
|
|
return fmt.Errorf("Could not delete establish rule from %s: %s", c.Table, output)
|
|
return fmt.Errorf("Could not delete establish rule from %s: %s", c.Table, output)
|
|
@@ -239,10 +277,11 @@ func ProgramChain(c *ChainInfo, bridgeName string, hairpinMode, enable bool) err
|
|
}
|
|
}
|
|
|
|
|
|
// RemoveExistingChain removes existing chain from the table.
|
|
// RemoveExistingChain removes existing chain from the table.
|
|
-func RemoveExistingChain(name string, table Table) error {
|
|
|
|
|
|
+func (iptable IPTable) RemoveExistingChain(name string, table Table) error {
|
|
c := &ChainInfo{
|
|
c := &ChainInfo{
|
|
- Name: name,
|
|
|
|
- Table: table,
|
|
|
|
|
|
+ Name: name,
|
|
|
|
+ Table: table,
|
|
|
|
+ IPTable: iptable,
|
|
}
|
|
}
|
|
if string(c.Table) == "" {
|
|
if string(c.Table) == "" {
|
|
c.Table = Filter
|
|
c.Table = Filter
|
|
@@ -252,6 +291,8 @@ func RemoveExistingChain(name string, table Table) error {
|
|
|
|
|
|
// Forward adds forwarding rule to 'filter' table and corresponding nat rule to 'nat' table.
|
|
// Forward adds forwarding rule to 'filter' table and corresponding nat rule to 'nat' table.
|
|
func (c *ChainInfo) Forward(action Action, ip net.IP, port int, proto, destAddr string, destPort int, bridgeName string) error {
|
|
func (c *ChainInfo) Forward(action Action, ip net.IP, port int, proto, destAddr string, destPort int, bridgeName string) error {
|
|
|
|
+
|
|
|
|
+ iptable := GetIptable(c.IPTable.Version)
|
|
daddr := ip.String()
|
|
daddr := ip.String()
|
|
if ip.IsUnspecified() {
|
|
if ip.IsUnspecified() {
|
|
// iptables interprets "0.0.0.0" as "0.0.0.0/32", whereas we
|
|
// iptables interprets "0.0.0.0" as "0.0.0.0/32", whereas we
|
|
@@ -266,10 +307,11 @@ func (c *ChainInfo) Forward(action Action, ip net.IP, port int, proto, destAddr
|
|
"--dport", strconv.Itoa(port),
|
|
"--dport", strconv.Itoa(port),
|
|
"-j", "DNAT",
|
|
"-j", "DNAT",
|
|
"--to-destination", net.JoinHostPort(destAddr, strconv.Itoa(destPort))}
|
|
"--to-destination", net.JoinHostPort(destAddr, strconv.Itoa(destPort))}
|
|
|
|
+
|
|
if !c.HairpinMode {
|
|
if !c.HairpinMode {
|
|
args = append(args, "!", "-i", bridgeName)
|
|
args = append(args, "!", "-i", bridgeName)
|
|
}
|
|
}
|
|
- if err := ProgramRule(Nat, c.Name, action, args); err != nil {
|
|
|
|
|
|
+ if err := iptable.ProgramRule(Nat, c.Name, action, args); err != nil {
|
|
return err
|
|
return err
|
|
}
|
|
}
|
|
|
|
|
|
@@ -281,7 +323,7 @@ func (c *ChainInfo) Forward(action Action, ip net.IP, port int, proto, destAddr
|
|
"--dport", strconv.Itoa(destPort),
|
|
"--dport", strconv.Itoa(destPort),
|
|
"-j", "ACCEPT",
|
|
"-j", "ACCEPT",
|
|
}
|
|
}
|
|
- if err := ProgramRule(Filter, c.Name, action, args); err != nil {
|
|
|
|
|
|
+ if err := iptable.ProgramRule(Filter, c.Name, action, args); err != nil {
|
|
return err
|
|
return err
|
|
}
|
|
}
|
|
|
|
|
|
@@ -293,7 +335,7 @@ func (c *ChainInfo) Forward(action Action, ip net.IP, port int, proto, destAddr
|
|
"-j", "MASQUERADE",
|
|
"-j", "MASQUERADE",
|
|
}
|
|
}
|
|
|
|
|
|
- if err := ProgramRule(Nat, "POSTROUTING", action, args); err != nil {
|
|
|
|
|
|
+ if err := iptable.ProgramRule(Nat, "POSTROUTING", action, args); err != nil {
|
|
return err
|
|
return err
|
|
}
|
|
}
|
|
|
|
|
|
@@ -311,7 +353,7 @@ func (c *ChainInfo) Forward(action Action, ip net.IP, port int, proto, destAddr
|
|
"-j", "CHECKSUM",
|
|
"-j", "CHECKSUM",
|
|
"--checksum-fill",
|
|
"--checksum-fill",
|
|
}
|
|
}
|
|
- if err := ProgramRule(Mangle, "POSTROUTING", action, args); err != nil {
|
|
|
|
|
|
+ if err := iptable.ProgramRule(Mangle, "POSTROUTING", action, args); err != nil {
|
|
return err
|
|
return err
|
|
}
|
|
}
|
|
}
|
|
}
|
|
@@ -322,6 +364,7 @@ func (c *ChainInfo) Forward(action Action, ip net.IP, port int, proto, destAddr
|
|
// Link adds reciprocal ACCEPT rule for two supplied IP addresses.
|
|
// Link adds reciprocal ACCEPT rule for two supplied IP addresses.
|
|
// Traffic is allowed from ip1 to ip2 and vice-versa
|
|
// Traffic is allowed from ip1 to ip2 and vice-versa
|
|
func (c *ChainInfo) Link(action Action, ip1, ip2 net.IP, port int, proto string, bridgeName string) error {
|
|
func (c *ChainInfo) Link(action Action, ip1, ip2 net.IP, port int, proto string, bridgeName string) error {
|
|
|
|
+ iptable := GetIptable(c.IPTable.Version)
|
|
// forward
|
|
// forward
|
|
args := []string{
|
|
args := []string{
|
|
"-i", bridgeName, "-o", bridgeName,
|
|
"-i", bridgeName, "-o", bridgeName,
|
|
@@ -331,32 +374,34 @@ func (c *ChainInfo) Link(action Action, ip1, ip2 net.IP, port int, proto string,
|
|
"--dport", strconv.Itoa(port),
|
|
"--dport", strconv.Itoa(port),
|
|
"-j", "ACCEPT",
|
|
"-j", "ACCEPT",
|
|
}
|
|
}
|
|
- if err := ProgramRule(Filter, c.Name, action, args); err != nil {
|
|
|
|
|
|
+
|
|
|
|
+ if err := iptable.ProgramRule(Filter, c.Name, action, args); err != nil {
|
|
return err
|
|
return err
|
|
}
|
|
}
|
|
// reverse
|
|
// reverse
|
|
args[7], args[9] = args[9], args[7]
|
|
args[7], args[9] = args[9], args[7]
|
|
args[10] = "--sport"
|
|
args[10] = "--sport"
|
|
- return ProgramRule(Filter, c.Name, action, args)
|
|
|
|
|
|
+ return iptable.ProgramRule(Filter, c.Name, action, args)
|
|
}
|
|
}
|
|
|
|
|
|
// ProgramRule adds the rule specified by args only if the
|
|
// ProgramRule adds the rule specified by args only if the
|
|
// rule is not already present in the chain. Reciprocally,
|
|
// rule is not already present in the chain. Reciprocally,
|
|
// it removes the rule only if present.
|
|
// it removes the rule only if present.
|
|
-func ProgramRule(table Table, chain string, action Action, args []string) error {
|
|
|
|
- if Exists(table, chain, args...) != (action == Delete) {
|
|
|
|
|
|
+func (iptable IPTable) ProgramRule(table Table, chain string, action Action, args []string) error {
|
|
|
|
+ if iptable.Exists(table, chain, args...) != (action == Delete) {
|
|
return nil
|
|
return nil
|
|
}
|
|
}
|
|
- return RawCombinedOutput(append([]string{"-t", string(table), string(action), chain}, args...)...)
|
|
|
|
|
|
+ return iptable.RawCombinedOutput(append([]string{"-t", string(table), string(action), chain}, args...)...)
|
|
}
|
|
}
|
|
|
|
|
|
// Prerouting adds linking rule to nat/PREROUTING chain.
|
|
// Prerouting adds linking rule to nat/PREROUTING chain.
|
|
func (c *ChainInfo) Prerouting(action Action, args ...string) error {
|
|
func (c *ChainInfo) Prerouting(action Action, args ...string) error {
|
|
|
|
+ iptable := GetIptable(c.IPTable.Version)
|
|
a := []string{"-t", string(Nat), string(action), "PREROUTING"}
|
|
a := []string{"-t", string(Nat), string(action), "PREROUTING"}
|
|
if len(args) > 0 {
|
|
if len(args) > 0 {
|
|
a = append(a, args...)
|
|
a = append(a, args...)
|
|
}
|
|
}
|
|
- if output, err := Raw(a...); err != nil {
|
|
|
|
|
|
+ if output, err := iptable.Raw(a...); err != nil {
|
|
return err
|
|
return err
|
|
} else if len(output) != 0 {
|
|
} else if len(output) != 0 {
|
|
return ChainError{Chain: "PREROUTING", Output: output}
|
|
return ChainError{Chain: "PREROUTING", Output: output}
|
|
@@ -366,11 +411,12 @@ func (c *ChainInfo) Prerouting(action Action, args ...string) error {
|
|
|
|
|
|
// Output adds linking rule to an OUTPUT chain.
|
|
// Output adds linking rule to an OUTPUT chain.
|
|
func (c *ChainInfo) Output(action Action, args ...string) error {
|
|
func (c *ChainInfo) Output(action Action, args ...string) error {
|
|
|
|
+ iptable := GetIptable(c.IPTable.Version)
|
|
a := []string{"-t", string(c.Table), string(action), "OUTPUT"}
|
|
a := []string{"-t", string(c.Table), string(action), "OUTPUT"}
|
|
if len(args) > 0 {
|
|
if len(args) > 0 {
|
|
a = append(a, args...)
|
|
a = append(a, args...)
|
|
}
|
|
}
|
|
- if output, err := Raw(a...); err != nil {
|
|
|
|
|
|
+ if output, err := iptable.Raw(a...); err != nil {
|
|
return err
|
|
return err
|
|
} else if len(output) != 0 {
|
|
} else if len(output) != 0 {
|
|
return ChainError{Chain: "OUTPUT", Output: output}
|
|
return ChainError{Chain: "OUTPUT", Output: output}
|
|
@@ -380,35 +426,36 @@ func (c *ChainInfo) Output(action Action, args ...string) error {
|
|
|
|
|
|
// Remove removes the chain.
|
|
// Remove removes the chain.
|
|
func (c *ChainInfo) Remove() error {
|
|
func (c *ChainInfo) Remove() error {
|
|
|
|
+ iptable := GetIptable(c.IPTable.Version)
|
|
// Ignore errors - This could mean the chains were never set up
|
|
// Ignore errors - This could mean the chains were never set up
|
|
if c.Table == Nat {
|
|
if c.Table == Nat {
|
|
c.Prerouting(Delete, "-m", "addrtype", "--dst-type", "LOCAL", "-j", c.Name)
|
|
c.Prerouting(Delete, "-m", "addrtype", "--dst-type", "LOCAL", "-j", c.Name)
|
|
- c.Output(Delete, "-m", "addrtype", "--dst-type", "LOCAL", "!", "--dst", "127.0.0.0/8", "-j", c.Name)
|
|
|
|
|
|
+ c.Output(Delete, "-m", "addrtype", "--dst-type", "LOCAL", "!", "--dst", iptable.LoopbackByVersion(), "-j", c.Name)
|
|
c.Output(Delete, "-m", "addrtype", "--dst-type", "LOCAL", "-j", c.Name) // Created in versions <= 0.1.6
|
|
c.Output(Delete, "-m", "addrtype", "--dst-type", "LOCAL", "-j", c.Name) // Created in versions <= 0.1.6
|
|
|
|
|
|
c.Prerouting(Delete)
|
|
c.Prerouting(Delete)
|
|
c.Output(Delete)
|
|
c.Output(Delete)
|
|
}
|
|
}
|
|
- Raw("-t", string(c.Table), "-F", c.Name)
|
|
|
|
- Raw("-t", string(c.Table), "-X", c.Name)
|
|
|
|
|
|
+ iptable.Raw("-t", string(c.Table), "-F", c.Name)
|
|
|
|
+ iptable.Raw("-t", string(c.Table), "-X", c.Name)
|
|
return nil
|
|
return nil
|
|
}
|
|
}
|
|
|
|
|
|
// Exists checks if a rule exists
|
|
// Exists checks if a rule exists
|
|
-func Exists(table Table, chain string, rule ...string) bool {
|
|
|
|
- return exists(false, table, chain, rule...)
|
|
|
|
|
|
+func (iptable IPTable) Exists(table Table, chain string, rule ...string) bool {
|
|
|
|
+ return iptable.exists(false, table, chain, rule...)
|
|
}
|
|
}
|
|
|
|
|
|
// ExistsNative behaves as Exists with the difference it
|
|
// ExistsNative behaves as Exists with the difference it
|
|
// will always invoke `iptables` binary.
|
|
// will always invoke `iptables` binary.
|
|
-func ExistsNative(table Table, chain string, rule ...string) bool {
|
|
|
|
- return exists(true, table, chain, rule...)
|
|
|
|
|
|
+func (iptable IPTable) ExistsNative(table Table, chain string, rule ...string) bool {
|
|
|
|
+ return iptable.exists(true, table, chain, rule...)
|
|
}
|
|
}
|
|
|
|
|
|
-func exists(native bool, table Table, chain string, rule ...string) bool {
|
|
|
|
- f := Raw
|
|
|
|
|
|
+func (iptable IPTable) exists(native bool, table Table, chain string, rule ...string) bool {
|
|
|
|
+ f := iptable.Raw
|
|
if native {
|
|
if native {
|
|
- f = raw
|
|
|
|
|
|
+ f = iptable.raw
|
|
}
|
|
}
|
|
|
|
|
|
if string(table) == "" {
|
|
if string(table) == "" {
|
|
@@ -429,12 +476,16 @@ func exists(native bool, table Table, chain string, rule ...string) bool {
|
|
|
|
|
|
// parse "iptables -S" for the rule (it checks rules in a specific chain
|
|
// parse "iptables -S" for the rule (it checks rules in a specific chain
|
|
// in a specific table and it is very unreliable)
|
|
// in a specific table and it is very unreliable)
|
|
- return existsRaw(table, chain, rule...)
|
|
|
|
|
|
+ return iptable.existsRaw(table, chain, rule...)
|
|
}
|
|
}
|
|
|
|
|
|
-func existsRaw(table Table, chain string, rule ...string) bool {
|
|
|
|
|
|
+func (iptable IPTable) existsRaw(table Table, chain string, rule ...string) bool {
|
|
|
|
+ path := iptablesPath
|
|
|
|
+ if iptable.Version == IPv6 {
|
|
|
|
+ path = ip6tablesPath
|
|
|
|
+ }
|
|
ruleString := fmt.Sprintf("%s %s\n", chain, strings.Join(rule, " "))
|
|
ruleString := fmt.Sprintf("%s %s\n", chain, strings.Join(rule, " "))
|
|
- existingRules, _ := exec.Command(iptablesPath, "-t", string(table), "-S", chain).Output()
|
|
|
|
|
|
+ existingRules, _ := exec.Command(path, "-t", string(table), "-S", chain).Output()
|
|
|
|
|
|
return strings.Contains(string(existingRules), ruleString)
|
|
return strings.Contains(string(existingRules), ruleString)
|
|
}
|
|
}
|
|
@@ -459,7 +510,7 @@ func filterOutput(start time.Time, output []byte, args ...string) []byte {
|
|
}
|
|
}
|
|
|
|
|
|
// Raw calls 'iptables' system command, passing supplied arguments.
|
|
// Raw calls 'iptables' system command, passing supplied arguments.
|
|
-func Raw(args ...string) ([]byte, error) {
|
|
|
|
|
|
+func (iptable IPTable) Raw(args ...string) ([]byte, error) {
|
|
if firewalldRunning {
|
|
if firewalldRunning {
|
|
startTime := time.Now()
|
|
startTime := time.Now()
|
|
output, err := Passthrough(Iptables, args...)
|
|
output, err := Passthrough(Iptables, args...)
|
|
@@ -467,10 +518,10 @@ func Raw(args ...string) ([]byte, error) {
|
|
return filterOutput(startTime, output, args...), err
|
|
return filterOutput(startTime, output, args...), err
|
|
}
|
|
}
|
|
}
|
|
}
|
|
- return raw(args...)
|
|
|
|
|
|
+ return iptable.raw(args...)
|
|
}
|
|
}
|
|
|
|
|
|
-func raw(args ...string) ([]byte, error) {
|
|
|
|
|
|
+func (iptable IPTable) raw(args ...string) ([]byte, error) {
|
|
if err := initCheck(); err != nil {
|
|
if err := initCheck(); err != nil {
|
|
return nil, err
|
|
return nil, err
|
|
}
|
|
}
|
|
@@ -481,10 +532,15 @@ func raw(args ...string) ([]byte, error) {
|
|
defer bestEffortLock.Unlock()
|
|
defer bestEffortLock.Unlock()
|
|
}
|
|
}
|
|
|
|
|
|
- logrus.Debugf("%s, %v", iptablesPath, args)
|
|
|
|
|
|
+ path := iptablesPath
|
|
|
|
+ if iptable.Version == IPv6 {
|
|
|
|
+ path = ip6tablesPath
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ logrus.Debugf("%s, %v", path, args)
|
|
|
|
|
|
startTime := time.Now()
|
|
startTime := time.Now()
|
|
- output, err := exec.Command(iptablesPath, args...).CombinedOutput()
|
|
|
|
|
|
+ output, err := exec.Command(path, args...).CombinedOutput()
|
|
if err != nil {
|
|
if err != nil {
|
|
return nil, fmt.Errorf("iptables failed: iptables %v: %s (%s)", strings.Join(args, " "), output, err)
|
|
return nil, fmt.Errorf("iptables failed: iptables %v: %s (%s)", strings.Join(args, " "), output, err)
|
|
}
|
|
}
|
|
@@ -494,8 +550,8 @@ func raw(args ...string) ([]byte, error) {
|
|
|
|
|
|
// RawCombinedOutput internally calls the Raw function and returns a non nil
|
|
// RawCombinedOutput internally calls the Raw function and returns a non nil
|
|
// error if Raw returned a non nil error or a non empty output
|
|
// error if Raw returned a non nil error or a non empty output
|
|
-func RawCombinedOutput(args ...string) error {
|
|
|
|
- if output, err := Raw(args...); err != nil || len(output) != 0 {
|
|
|
|
|
|
+func (iptable IPTable) RawCombinedOutput(args ...string) error {
|
|
|
|
+ if output, err := iptable.Raw(args...); err != nil || len(output) != 0 {
|
|
return fmt.Errorf("%s (%v)", string(output), err)
|
|
return fmt.Errorf("%s (%v)", string(output), err)
|
|
}
|
|
}
|
|
return nil
|
|
return nil
|
|
@@ -503,16 +559,16 @@ func RawCombinedOutput(args ...string) error {
|
|
|
|
|
|
// RawCombinedOutputNative behave as RawCombinedOutput with the difference it
|
|
// RawCombinedOutputNative behave as RawCombinedOutput with the difference it
|
|
// will always invoke `iptables` binary
|
|
// will always invoke `iptables` binary
|
|
-func RawCombinedOutputNative(args ...string) error {
|
|
|
|
- if output, err := raw(args...); err != nil || len(output) != 0 {
|
|
|
|
|
|
+func (iptable IPTable) RawCombinedOutputNative(args ...string) error {
|
|
|
|
+ if output, err := iptable.raw(args...); err != nil || len(output) != 0 {
|
|
return fmt.Errorf("%s (%v)", string(output), err)
|
|
return fmt.Errorf("%s (%v)", string(output), err)
|
|
}
|
|
}
|
|
return nil
|
|
return nil
|
|
}
|
|
}
|
|
|
|
|
|
// ExistChain checks if a chain exists
|
|
// ExistChain checks if a chain exists
|
|
-func ExistChain(chain string, table Table) bool {
|
|
|
|
- if _, err := Raw("-t", string(table), "-nL", chain); err == nil {
|
|
|
|
|
|
+func (iptable IPTable) ExistChain(chain string, table Table) bool {
|
|
|
|
+ if _, err := iptable.Raw("-t", string(table), "-nL", chain); err == nil {
|
|
return true
|
|
return true
|
|
}
|
|
}
|
|
return false
|
|
return false
|
|
@@ -528,8 +584,8 @@ func GetVersion() (major, minor, micro int, err error) {
|
|
}
|
|
}
|
|
|
|
|
|
// SetDefaultPolicy sets the passed default policy for the table/chain
|
|
// SetDefaultPolicy sets the passed default policy for the table/chain
|
|
-func SetDefaultPolicy(table Table, chain string, policy Policy) error {
|
|
|
|
- if err := RawCombinedOutput("-t", string(table), "-P", chain, string(policy)); err != nil {
|
|
|
|
|
|
+func (iptable IPTable) SetDefaultPolicy(table Table, chain string, policy Policy) error {
|
|
|
|
+ if err := iptable.RawCombinedOutput("-t", string(table), "-P", chain, string(policy)); err != nil {
|
|
return fmt.Errorf("setting default policy to %v in %v chain failed: %v", policy, chain, err)
|
|
return fmt.Errorf("setting default policy to %v in %v chain failed: %v", policy, chain, err)
|
|
}
|
|
}
|
|
return nil
|
|
return nil
|
|
@@ -549,17 +605,17 @@ func supportsCOption(mj, mn, mc int) bool {
|
|
}
|
|
}
|
|
|
|
|
|
// AddReturnRule adds a return rule for the chain in the filter table
|
|
// AddReturnRule adds a return rule for the chain in the filter table
|
|
-func AddReturnRule(chain string) error {
|
|
|
|
|
|
+func (iptable IPTable) AddReturnRule(chain string) error {
|
|
var (
|
|
var (
|
|
table = Filter
|
|
table = Filter
|
|
args = []string{"-j", "RETURN"}
|
|
args = []string{"-j", "RETURN"}
|
|
)
|
|
)
|
|
|
|
|
|
- if Exists(table, chain, args...) {
|
|
|
|
|
|
+ if iptable.Exists(table, chain, args...) {
|
|
return nil
|
|
return nil
|
|
}
|
|
}
|
|
|
|
|
|
- err := RawCombinedOutput(append([]string{"-A", chain}, args...)...)
|
|
|
|
|
|
+ err := iptable.RawCombinedOutput(append([]string{"-A", chain}, args...)...)
|
|
if err != nil {
|
|
if err != nil {
|
|
return fmt.Errorf("unable to add return rule in %s chain: %s", chain, err.Error())
|
|
return fmt.Errorf("unable to add return rule in %s chain: %s", chain, err.Error())
|
|
}
|
|
}
|
|
@@ -568,20 +624,20 @@ func AddReturnRule(chain string) error {
|
|
}
|
|
}
|
|
|
|
|
|
// EnsureJumpRule ensures the jump rule is on top
|
|
// EnsureJumpRule ensures the jump rule is on top
|
|
-func EnsureJumpRule(fromChain, toChain string) error {
|
|
|
|
|
|
+func (iptable IPTable) EnsureJumpRule(fromChain, toChain string) error {
|
|
var (
|
|
var (
|
|
table = Filter
|
|
table = Filter
|
|
args = []string{"-j", toChain}
|
|
args = []string{"-j", toChain}
|
|
)
|
|
)
|
|
|
|
|
|
- if Exists(table, fromChain, args...) {
|
|
|
|
- err := RawCombinedOutput(append([]string{"-D", fromChain}, args...)...)
|
|
|
|
|
|
+ if iptable.Exists(table, fromChain, args...) {
|
|
|
|
+ err := iptable.RawCombinedOutput(append([]string{"-D", fromChain}, args...)...)
|
|
if err != nil {
|
|
if err != nil {
|
|
return fmt.Errorf("unable to remove jump to %s rule in %s chain: %s", toChain, fromChain, err.Error())
|
|
return fmt.Errorf("unable to remove jump to %s rule in %s chain: %s", toChain, fromChain, err.Error())
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
- err := RawCombinedOutput(append([]string{"-I", fromChain}, args...)...)
|
|
|
|
|
|
+ err := iptable.RawCombinedOutput(append([]string{"-I", fromChain}, args...)...)
|
|
if err != nil {
|
|
if err != nil {
|
|
return fmt.Errorf("unable to insert jump to %s rule in %s chain: %s", toChain, fromChain, err.Error())
|
|
return fmt.Errorf("unable to insert jump to %s rule in %s chain: %s", toChain, fromChain, err.Error())
|
|
}
|
|
}
|