2013-12-23 23:36:58 +00:00
|
|
|
package iptables
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
"net"
|
|
|
|
"os/exec"
|
2014-07-10 04:22:01 +00:00
|
|
|
"regexp"
|
2013-12-23 23:36:58 +00:00
|
|
|
"strconv"
|
|
|
|
"strings"
|
2014-09-25 16:55:53 +00:00
|
|
|
|
2014-10-24 17:12:35 +00:00
|
|
|
log "github.com/Sirupsen/logrus"
|
2013-12-23 23:36:58 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type Action string
|
2014-07-16 12:03:01 +00:00
|
|
|
type Table string
|
2013-12-23 23:36:58 +00:00
|
|
|
|
|
|
|
const (
|
2014-06-27 07:29:55 +00:00
|
|
|
Append Action = "-A"
|
2013-12-23 23:36:58 +00:00
|
|
|
Delete Action = "-D"
|
2014-06-27 07:29:55 +00:00
|
|
|
Insert Action = "-I"
|
2014-07-16 12:03:01 +00:00
|
|
|
Nat Table = "nat"
|
|
|
|
Filter Table = "filter"
|
2013-12-23 23:36:58 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
2014-05-23 21:18:50 +00:00
|
|
|
supportsXlock = false
|
2014-11-21 00:07:55 +00:00
|
|
|
ErrIptablesNotFound = errors.New("Iptables not found")
|
2013-12-23 23:36:58 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type Chain struct {
|
|
|
|
Name string
|
|
|
|
Bridge string
|
2014-07-16 12:03:01 +00:00
|
|
|
Table Table
|
2013-12-23 23:36:58 +00:00
|
|
|
}
|
|
|
|
|
2014-11-21 00:07:55 +00:00
|
|
|
type ChainError struct {
|
|
|
|
Chain string
|
|
|
|
Output []byte
|
|
|
|
}
|
|
|
|
|
|
|
|
func (e *ChainError) Error() string {
|
|
|
|
return fmt.Sprintf("Error iptables %s: %s", e.Chain, string(e.Output))
|
|
|
|
}
|
|
|
|
|
2014-05-23 21:18:50 +00:00
|
|
|
func init() {
|
|
|
|
supportsXlock = exec.Command("iptables", "--wait", "-L", "-n").Run() == nil
|
|
|
|
}
|
|
|
|
|
2014-07-16 12:03:01 +00:00
|
|
|
func NewChain(name, bridge string, table Table) (*Chain, error) {
|
|
|
|
c := &Chain{
|
2013-12-23 23:36:58 +00:00
|
|
|
Name: name,
|
|
|
|
Bridge: bridge,
|
2014-07-16 12:03:01 +00:00
|
|
|
Table: table,
|
|
|
|
}
|
|
|
|
|
|
|
|
if string(c.Table) == "" {
|
|
|
|
c.Table = Filter
|
2013-12-23 23:36:58 +00:00
|
|
|
}
|
|
|
|
|
2014-07-16 12:03:01 +00:00
|
|
|
// 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 {
|
|
|
|
return nil, err
|
|
|
|
} else if len(output) != 0 {
|
|
|
|
return nil, fmt.Errorf("Could not create %s/%s chain: %s", c.Table, c.Name, output)
|
|
|
|
}
|
2013-12-23 23:36:58 +00:00
|
|
|
}
|
2014-07-16 12:03:01 +00:00
|
|
|
|
|
|
|
switch table {
|
|
|
|
case Nat:
|
|
|
|
preroute := []string{
|
|
|
|
"-m", "addrtype",
|
|
|
|
"--dst-type", "LOCAL"}
|
|
|
|
if !Exists(preroute...) {
|
|
|
|
if err := c.Prerouting(Append, preroute...); err != nil {
|
|
|
|
return nil, fmt.Errorf("Failed to inject docker in PREROUTING chain: %s", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
output := []string{
|
|
|
|
"-m", "addrtype",
|
|
|
|
"--dst-type", "LOCAL",
|
|
|
|
"!", "--dst", "127.0.0.0/8"}
|
|
|
|
if !Exists(output...) {
|
|
|
|
if err := c.Output(Append, output...); err != nil {
|
|
|
|
return nil, fmt.Errorf("Failed to inject docker in OUTPUT chain: %s", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
case Filter:
|
|
|
|
link := []string{"FORWARD",
|
|
|
|
"-o", c.Bridge,
|
|
|
|
"-j", c.Name}
|
|
|
|
if !Exists(link...) {
|
|
|
|
insert := append([]string{string(Insert)}, link...)
|
|
|
|
if output, err := Raw(insert...); err != nil {
|
|
|
|
return nil, err
|
|
|
|
} else if len(output) != 0 {
|
|
|
|
return nil, fmt.Errorf("Could not create linking rule to %s/%s: %s", c.Table, c.Name, output)
|
|
|
|
}
|
|
|
|
}
|
2013-12-23 23:36:58 +00:00
|
|
|
}
|
2014-07-16 12:03:01 +00:00
|
|
|
return c, nil
|
2013-12-23 23:36:58 +00:00
|
|
|
}
|
|
|
|
|
2014-07-16 12:03:01 +00:00
|
|
|
func RemoveExistingChain(name string, table Table) error {
|
|
|
|
c := &Chain{
|
|
|
|
Name: name,
|
|
|
|
Table: table,
|
|
|
|
}
|
|
|
|
if string(c.Table) == "" {
|
|
|
|
c.Table = Filter
|
2013-12-23 23:36:58 +00:00
|
|
|
}
|
2014-07-16 12:03:01 +00:00
|
|
|
return c.Remove()
|
2013-12-23 23:36:58 +00:00
|
|
|
}
|
|
|
|
|
2014-07-16 12:03:01 +00:00
|
|
|
// Add forwarding rule to 'filter' table and corresponding nat rule to 'nat' table
|
2013-12-23 23:36:58 +00:00
|
|
|
func (c *Chain) Forward(action Action, ip net.IP, port int, proto, dest_addr string, dest_port int) error {
|
|
|
|
daddr := ip.String()
|
|
|
|
if ip.IsUnspecified() {
|
|
|
|
// iptables interprets "0.0.0.0" as "0.0.0.0/32", whereas we
|
|
|
|
// want "0.0.0.0/0". "0/0" is correctly interpreted as "any
|
|
|
|
// value" by both iptables and ip6tables.
|
|
|
|
daddr = "0/0"
|
|
|
|
}
|
2014-07-16 12:03:01 +00:00
|
|
|
if output, err := Raw("-t", string(Nat), string(action), c.Name,
|
2013-12-23 23:36:58 +00:00
|
|
|
"-p", proto,
|
|
|
|
"-d", daddr,
|
|
|
|
"--dport", strconv.Itoa(port),
|
2014-11-20 22:22:22 +00:00
|
|
|
"!", "-i", c.Bridge,
|
2013-12-23 23:36:58 +00:00
|
|
|
"-j", "DNAT",
|
|
|
|
"--to-destination", net.JoinHostPort(dest_addr, strconv.Itoa(dest_port))); err != nil {
|
|
|
|
return err
|
|
|
|
} else if len(output) != 0 {
|
2014-11-21 00:07:55 +00:00
|
|
|
return &ChainError{Chain: "FORWARD", Output: output}
|
2013-12-23 23:36:58 +00:00
|
|
|
}
|
2014-01-25 04:22:53 +00:00
|
|
|
|
2014-07-16 12:03:01 +00:00
|
|
|
if output, err := Raw("-t", string(Filter), string(action), c.Name,
|
2014-01-25 04:22:53 +00:00
|
|
|
"!", "-i", c.Bridge,
|
|
|
|
"-o", c.Bridge,
|
|
|
|
"-p", proto,
|
2014-02-04 13:08:31 +00:00
|
|
|
"-d", dest_addr,
|
|
|
|
"--dport", strconv.Itoa(dest_port),
|
2014-01-25 04:22:53 +00:00
|
|
|
"-j", "ACCEPT"); err != nil {
|
|
|
|
return err
|
|
|
|
} else if len(output) != 0 {
|
2014-11-21 00:07:55 +00:00
|
|
|
return &ChainError{Chain: "FORWARD", Output: output}
|
2014-01-25 04:22:53 +00:00
|
|
|
}
|
|
|
|
|
2013-12-23 23:36:58 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2014-07-16 12:03:01 +00:00
|
|
|
// Add reciprocal ACCEPT rule for two supplied IP addresses.
|
|
|
|
// Traffic is allowed from ip1 to ip2 and vice-versa
|
2014-06-27 07:29:55 +00:00
|
|
|
func (c *Chain) Link(action Action, ip1, ip2 net.IP, port int, proto string) error {
|
2014-07-16 12:03:01 +00:00
|
|
|
if output, err := Raw("-t", string(Filter), string(action), c.Name,
|
2014-06-27 07:29:55 +00:00
|
|
|
"-i", c.Bridge, "-o", c.Bridge,
|
|
|
|
"-p", proto,
|
|
|
|
"-s", ip1.String(),
|
|
|
|
"--dport", strconv.Itoa(port),
|
|
|
|
"-d", ip2.String(),
|
|
|
|
"-j", "ACCEPT"); err != nil {
|
|
|
|
return err
|
|
|
|
} else if len(output) != 0 {
|
2014-07-16 12:03:01 +00:00
|
|
|
return fmt.Errorf("Error iptables forward: %s", output)
|
2014-06-27 07:29:55 +00:00
|
|
|
}
|
2014-07-16 12:03:01 +00:00
|
|
|
if output, err := Raw("-t", string(Filter), string(action), c.Name,
|
2014-06-27 07:29:55 +00:00
|
|
|
"-i", c.Bridge, "-o", c.Bridge,
|
|
|
|
"-p", proto,
|
|
|
|
"-s", ip2.String(),
|
|
|
|
"--dport", strconv.Itoa(port),
|
|
|
|
"-d", ip1.String(),
|
|
|
|
"-j", "ACCEPT"); err != nil {
|
|
|
|
return err
|
|
|
|
} else if len(output) != 0 {
|
2014-07-16 12:03:01 +00:00
|
|
|
return fmt.Errorf("Error iptables forward: %s", output)
|
2014-06-27 07:29:55 +00:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2014-07-16 12:03:01 +00:00
|
|
|
// Add linking rule to nat/PREROUTING chain.
|
2013-12-23 23:36:58 +00:00
|
|
|
func (c *Chain) Prerouting(action Action, args ...string) error {
|
2014-07-16 12:03:01 +00:00
|
|
|
a := []string{"-t", string(Nat), string(action), "PREROUTING"}
|
2013-12-23 23:36:58 +00:00
|
|
|
if len(args) > 0 {
|
|
|
|
a = append(a, args...)
|
|
|
|
}
|
|
|
|
if output, err := Raw(append(a, "-j", c.Name)...); err != nil {
|
|
|
|
return err
|
|
|
|
} else if len(output) != 0 {
|
2014-11-21 00:07:55 +00:00
|
|
|
return &ChainError{Chain: "PREROUTING", Output: output}
|
2013-12-23 23:36:58 +00:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2014-07-16 12:03:01 +00:00
|
|
|
// Add linking rule to an OUTPUT chain
|
2013-12-23 23:36:58 +00:00
|
|
|
func (c *Chain) Output(action Action, args ...string) error {
|
2014-07-16 12:03:01 +00:00
|
|
|
a := []string{"-t", string(c.Table), string(action), "OUTPUT"}
|
2013-12-23 23:36:58 +00:00
|
|
|
if len(args) > 0 {
|
|
|
|
a = append(a, args...)
|
|
|
|
}
|
|
|
|
if output, err := Raw(append(a, "-j", c.Name)...); err != nil {
|
|
|
|
return err
|
|
|
|
} else if len(output) != 0 {
|
2014-11-21 00:07:55 +00:00
|
|
|
return &ChainError{Chain: "OUTPUT", Output: output}
|
2013-12-23 23:36:58 +00:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Chain) Remove() error {
|
2014-07-16 12:03:01 +00:00
|
|
|
if c.Table == Nat {
|
|
|
|
// Ignore errors - This could mean the chains were never set up
|
|
|
|
c.Prerouting(Delete, "-m", "addrtype", "--dst-type", "LOCAL")
|
|
|
|
c.Output(Delete, "-m", "addrtype", "--dst-type", "LOCAL", "!", "--dst", "127.0.0.0/8")
|
|
|
|
c.Output(Delete, "-m", "addrtype", "--dst-type", "LOCAL") // Created in versions <= 0.1.6
|
2013-12-23 23:36:58 +00:00
|
|
|
|
2014-07-16 12:03:01 +00:00
|
|
|
c.Prerouting(Delete)
|
|
|
|
c.Output(Delete)
|
2013-12-23 23:36:58 +00:00
|
|
|
|
2014-07-16 12:03:01 +00:00
|
|
|
Raw("-t", string(Nat), "-F", c.Name)
|
|
|
|
Raw("-t", string(Nat), "-X", c.Name)
|
|
|
|
}
|
2013-12-23 23:36:58 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2014-07-16 12:03:01 +00:00
|
|
|
// Check if a rule exists
|
2013-12-23 23:36:58 +00:00
|
|
|
func Exists(args ...string) bool {
|
2014-07-10 04:22:01 +00:00
|
|
|
// iptables -C, --check option was added in v.1.4.11
|
|
|
|
// http://ftp.netfilter.org/pub/iptables/changes-iptables-1.4.11.txt
|
|
|
|
|
|
|
|
// try -C
|
|
|
|
// if exit status is 0 then return true, the rule exists
|
|
|
|
if _, err := Raw(append([]string{"-C"}, args...)...); err == nil {
|
|
|
|
return true
|
2013-12-23 23:36:58 +00:00
|
|
|
}
|
2014-07-10 04:22:01 +00:00
|
|
|
|
|
|
|
// parse iptables-save for the rule
|
|
|
|
rule := strings.Replace(strings.Join(args, " "), "-t nat ", "", -1)
|
|
|
|
existingRules, _ := exec.Command("iptables-save").Output()
|
|
|
|
|
|
|
|
// regex to replace ips in rule
|
|
|
|
// because MASQUERADE rule will not be exactly what was passed
|
|
|
|
re := regexp.MustCompile(`[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\/[0-9]{1,2}`)
|
|
|
|
|
|
|
|
return strings.Contains(
|
|
|
|
re.ReplaceAllString(string(existingRules), "?"),
|
|
|
|
re.ReplaceAllString(rule, "?"),
|
|
|
|
)
|
2013-12-23 23:36:58 +00:00
|
|
|
}
|
|
|
|
|
2014-07-16 12:03:01 +00:00
|
|
|
// Call 'iptables' system command, passing supplied arguments
|
2013-12-23 23:36:58 +00:00
|
|
|
func Raw(args ...string) ([]byte, error) {
|
|
|
|
path, err := exec.LookPath("iptables")
|
|
|
|
if err != nil {
|
|
|
|
return nil, ErrIptablesNotFound
|
|
|
|
}
|
2014-05-23 21:18:50 +00:00
|
|
|
|
|
|
|
if supportsXlock {
|
|
|
|
args = append([]string{"--wait"}, args...)
|
|
|
|
}
|
|
|
|
|
2014-09-25 16:55:53 +00:00
|
|
|
log.Debugf("%s, %v", path, args)
|
2014-05-23 21:18:50 +00:00
|
|
|
|
|
|
|
output, err := exec.Command(path, args...).CombinedOutput()
|
2013-12-23 23:36:58 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("iptables failed: iptables %v: %s (%s)", strings.Join(args, " "), output, err)
|
|
|
|
}
|
2014-05-23 21:18:50 +00:00
|
|
|
|
2014-05-29 13:57:29 +00:00
|
|
|
// ignore iptables' message about xtables lock
|
|
|
|
if strings.Contains(string(output), "waiting for it to exit") {
|
|
|
|
output = []byte("")
|
|
|
|
}
|
|
|
|
|
2013-12-23 23:36:58 +00:00
|
|
|
return output, err
|
|
|
|
}
|