Merge pull request #9271 from jfrazelle/iptables-errors

Typed errors for iptables chain raw command output. YAYYYYYY.
This commit is contained in:
Alexander Morozov 2014-11-20 19:03:38 -08:00
commit e3f3259e00
2 changed files with 17 additions and 8 deletions

View File

@ -195,7 +195,7 @@ func setupIPTables(addr net.Addr, icc, ipmasq bool) error {
if output, err := iptables.Raw(append([]string{"-I"}, natArgs...)...); err != nil { if output, err := iptables.Raw(append([]string{"-I"}, natArgs...)...); err != nil {
return fmt.Errorf("Unable to enable network bridge NAT: %s", err) return fmt.Errorf("Unable to enable network bridge NAT: %s", err)
} else if len(output) != 0 { } else if len(output) != 0 {
return fmt.Errorf("Error iptables postrouting: %s", output) return &iptables.ChainError{Chain: "POSTROUTING", Output: output}
} }
} }
} }
@ -236,7 +236,7 @@ func setupIPTables(addr net.Addr, icc, ipmasq bool) error {
if output, err := iptables.Raw(append([]string{"-I"}, outgoingArgs...)...); err != nil { if output, err := iptables.Raw(append([]string{"-I"}, outgoingArgs...)...); err != nil {
return fmt.Errorf("Unable to allow outgoing packets: %s", err) return fmt.Errorf("Unable to allow outgoing packets: %s", err)
} else if len(output) != 0 { } else if len(output) != 0 {
return fmt.Errorf("Error iptables allow outgoing: %s", output) return &iptables.ChainError{Chain: "FORWARD outgoing", Output: output}
} }
} }
@ -247,7 +247,7 @@ func setupIPTables(addr net.Addr, icc, ipmasq bool) error {
if output, err := iptables.Raw(append([]string{"-I"}, existingArgs...)...); err != nil { if output, err := iptables.Raw(append([]string{"-I"}, existingArgs...)...); err != nil {
return fmt.Errorf("Unable to allow incoming packets: %s", err) return fmt.Errorf("Unable to allow incoming packets: %s", err)
} else if len(output) != 0 { } else if len(output) != 0 {
return fmt.Errorf("Error iptables allow incoming: %s", output) return &iptables.ChainError{Chain: "FORWARD incoming", Output: output}
} }
} }
return nil return nil

View File

@ -20,9 +20,9 @@ const (
) )
var ( var (
ErrIptablesNotFound = errors.New("Iptables not found")
nat = []string{"-t", "nat"} nat = []string{"-t", "nat"}
supportsXlock = false supportsXlock = false
ErrIptablesNotFound = errors.New("Iptables not found")
) )
type Chain struct { type Chain struct {
@ -30,6 +30,15 @@ type Chain struct {
Bridge string Bridge string
} }
type ChainError struct {
Chain string
Output []byte
}
func (e *ChainError) Error() string {
return fmt.Sprintf("Error iptables %s: %s", e.Chain, string(e.Output))
}
func init() { func init() {
supportsXlock = exec.Command("iptables", "--wait", "-L", "-n").Run() == nil supportsXlock = exec.Command("iptables", "--wait", "-L", "-n").Run() == nil
} }
@ -78,7 +87,7 @@ func (c *Chain) Forward(action Action, ip net.IP, port int, proto, dest_addr str
"--to-destination", net.JoinHostPort(dest_addr, strconv.Itoa(dest_port))); err != nil { "--to-destination", net.JoinHostPort(dest_addr, strconv.Itoa(dest_port))); err != nil {
return err return err
} else if len(output) != 0 { } else if len(output) != 0 {
return fmt.Errorf("Error iptables forward: %s", output) return &ChainError{Chain: "FORWARD", Output: output}
} }
fAction := action fAction := action
@ -94,7 +103,7 @@ func (c *Chain) Forward(action Action, ip net.IP, port int, proto, dest_addr str
"-j", "ACCEPT"); err != nil { "-j", "ACCEPT"); err != nil {
return err return err
} else if len(output) != 0 { } else if len(output) != 0 {
return fmt.Errorf("Error iptables forward: %s", output) return &ChainError{Chain: "FORWARD", Output: output}
} }
return nil return nil
@ -108,7 +117,7 @@ func (c *Chain) Prerouting(action Action, args ...string) error {
if output, err := Raw(append(a, "-j", c.Name)...); err != nil { if output, err := Raw(append(a, "-j", c.Name)...); err != nil {
return err return err
} else if len(output) != 0 { } else if len(output) != 0 {
return fmt.Errorf("Error iptables prerouting: %s", output) return &ChainError{Chain: "PREROUTING", Output: output}
} }
return nil return nil
} }
@ -121,7 +130,7 @@ func (c *Chain) Output(action Action, args ...string) error {
if output, err := Raw(append(a, "-j", c.Name)...); err != nil { if output, err := Raw(append(a, "-j", c.Name)...); err != nil {
return err return err
} else if len(output) != 0 { } else if len(output) != 0 {
return fmt.Errorf("Error iptables output: %s", output) return &ChainError{Chain: "OUTPUT", Output: output}
} }
return nil return nil
} }