2013-10-04 22:25:15 -04:00
|
|
|
package iptables
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
"net"
|
|
|
|
"os/exec"
|
2014-07-10 00:22:01 -04:00
|
|
|
"regexp"
|
2013-10-04 22:25:15 -04:00
|
|
|
"strconv"
|
|
|
|
"strings"
|
2014-09-25 12:55:53 -04:00
|
|
|
|
2014-10-24 13:12:35 -04:00
|
|
|
log "github.com/Sirupsen/logrus"
|
2013-10-04 22:25:15 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
type Action string
|
2014-07-16 08:03:01 -04:00
|
|
|
type Table string
|
2013-10-04 22:25:15 -04:00
|
|
|
|
|
|
|
const (
|
2014-06-27 03:29:55 -04:00
|
|
|
Append Action = "-A"
|
2013-10-04 22:25:15 -04:00
|
|
|
Delete Action = "-D"
|
2014-06-27 03:29:55 -04:00
|
|
|
Insert Action = "-I"
|
2014-07-16 08:03:01 -04:00
|
|
|
Nat Table = "nat"
|
|
|
|
Filter Table = "filter"
|
2013-10-04 22:25:15 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
2015-01-20 18:05:39 -05:00
|
|
|
iptablesPath string
|
2014-05-23 17:18:50 -04:00
|
|
|
supportsXlock = false
|
2014-11-20 19:07:55 -05:00
|
|
|
ErrIptablesNotFound = errors.New("Iptables not found")
|
2013-10-04 22:25:15 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
type Chain struct {
|
|
|
|
Name string
|
|
|
|
Bridge string
|
2014-07-16 08:03:01 -04:00
|
|
|
Table Table
|
2013-10-04 22:25:15 -04:00
|
|
|
}
|
|
|
|
|
2014-11-20 19:07:55 -05: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))
|
|
|
|
}
|
|
|
|
|
2015-01-20 18:05:39 -05:00
|
|
|
func initCheck() error {
|
|
|
|
|
|
|
|
if iptablesPath == "" {
|
|
|
|
path, err := exec.LookPath("iptables")
|
|
|
|
if err != nil {
|
|
|
|
return ErrIptablesNotFound
|
|
|
|
}
|
|
|
|
iptablesPath = path
|
|
|
|
supportsXlock = exec.Command(iptablesPath, "--wait", "-L", "-n").Run() == nil
|
|
|
|
}
|
|
|
|
return nil
|
2014-05-23 17:18:50 -04:00
|
|
|
}
|
|
|
|
|
2014-07-16 08:03:01 -04:00
|
|
|
func NewChain(name, bridge string, table Table) (*Chain, error) {
|
|
|
|
c := &Chain{
|
2013-10-04 22:25:15 -04:00
|
|
|
Name: name,
|
|
|
|
Bridge: bridge,
|
2014-07-16 08:03:01 -04:00
|
|
|
Table: table,
|
|
|
|
}
|
|
|
|
|
|
|
|
if string(c.Table) == "" {
|
|
|
|
c.Table = Filter
|
2013-10-04 22:25:15 -04:00
|
|
|
}
|
|
|
|
|
2014-07-16 08:03:01 -04: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-10-04 22:25:15 -04:00
|
|
|
}
|
2014-07-16 08:03:01 -04: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-10-04 22:25:15 -04:00
|
|
|
}
|
2014-07-16 08:03:01 -04:00
|
|
|
return c, nil
|
2013-10-04 22:25:15 -04:00
|
|
|
}
|
|
|
|
|
2014-07-16 08:03:01 -04:00
|
|
|
func RemoveExistingChain(name string, table Table) error {
|
|
|
|
c := &Chain{
|
|
|
|
Name: name,
|
|
|
|
Table: table,
|
|
|
|
}
|
|
|
|
if string(c.Table) == "" {
|
|
|
|
c.Table = Filter
|
2013-10-04 22:25:15 -04:00
|
|
|
}
|
2014-07-16 08:03:01 -04:00
|
|
|
return c.Remove()
|
2013-10-04 22:25:15 -04:00
|
|
|
}
|
|
|
|
|
2014-07-16 08:03:01 -04:00
|
|
|
// Add forwarding rule to 'filter' table and corresponding nat rule to 'nat' table
|
2014-11-14 20:36:38 -05:00
|
|
|
func (c *Chain) Forward(action Action, ip net.IP, port int, proto, destAddr string, destPort int) error {
|
2013-11-09 22:31:08 -05:00
|
|
|
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 08:03:01 -04:00
|
|
|
if output, err := Raw("-t", string(Nat), string(action), c.Name,
|
2013-10-04 22:25:15 -04:00
|
|
|
"-p", proto,
|
2013-11-09 22:31:08 -05:00
|
|
|
"-d", daddr,
|
2013-10-04 22:25:15 -04:00
|
|
|
"--dport", strconv.Itoa(port),
|
2014-11-20 17:22:22 -05:00
|
|
|
"!", "-i", c.Bridge,
|
2013-10-04 22:25:15 -04:00
|
|
|
"-j", "DNAT",
|
2014-11-14 20:36:38 -05:00
|
|
|
"--to-destination", net.JoinHostPort(destAddr, strconv.Itoa(destPort))); err != nil {
|
2013-11-05 10:33:07 -05:00
|
|
|
return err
|
|
|
|
} else if len(output) != 0 {
|
2014-11-20 19:07:55 -05:00
|
|
|
return &ChainError{Chain: "FORWARD", Output: output}
|
2013-11-05 10:33:07 -05:00
|
|
|
}
|
2014-01-24 23:22:53 -05:00
|
|
|
|
2014-07-16 08:03:01 -04:00
|
|
|
if output, err := Raw("-t", string(Filter), string(action), c.Name,
|
2014-01-24 23:22:53 -05:00
|
|
|
"!", "-i", c.Bridge,
|
|
|
|
"-o", c.Bridge,
|
|
|
|
"-p", proto,
|
2014-11-14 20:36:38 -05:00
|
|
|
"-d", destAddr,
|
|
|
|
"--dport", strconv.Itoa(destPort),
|
2014-01-24 23:22:53 -05:00
|
|
|
"-j", "ACCEPT"); err != nil {
|
|
|
|
return err
|
|
|
|
} else if len(output) != 0 {
|
2014-11-20 19:07:55 -05:00
|
|
|
return &ChainError{Chain: "FORWARD", Output: output}
|
2014-01-24 23:22:53 -05:00
|
|
|
}
|
|
|
|
|
2014-11-14 20:36:38 -05:00
|
|
|
if output, err := Raw("-t", string(Nat), string(action), "POSTROUTING",
|
|
|
|
"-p", proto,
|
|
|
|
"-s", destAddr,
|
|
|
|
"-d", destAddr,
|
|
|
|
"--dport", strconv.Itoa(destPort),
|
|
|
|
"-j", "MASQUERADE"); err != nil {
|
|
|
|
return err
|
|
|
|
} else if len(output) != 0 {
|
|
|
|
return &ChainError{Chain: "FORWARD", Output: output}
|
|
|
|
}
|
|
|
|
|
2013-11-05 10:33:07 -05:00
|
|
|
return nil
|
2013-10-04 22:25:15 -04:00
|
|
|
}
|
|
|
|
|
2014-07-16 08:03:01 -04:00
|
|
|
// Add reciprocal ACCEPT rule for two supplied IP addresses.
|
|
|
|
// Traffic is allowed from ip1 to ip2 and vice-versa
|
2014-06-27 03:29:55 -04:00
|
|
|
func (c *Chain) Link(action Action, ip1, ip2 net.IP, port int, proto string) error {
|
2014-07-16 08:03:01 -04:00
|
|
|
if output, err := Raw("-t", string(Filter), string(action), c.Name,
|
2014-06-27 03:29:55 -04:00
|
|
|
"-i", c.Bridge, "-o", c.Bridge,
|
|
|
|
"-p", proto,
|
|
|
|
"-s", ip1.String(),
|
|
|
|
"-d", ip2.String(),
|
2014-11-14 20:36:38 -05:00
|
|
|
"--dport", strconv.Itoa(port),
|
2014-06-27 03:29:55 -04:00
|
|
|
"-j", "ACCEPT"); err != nil {
|
|
|
|
return err
|
|
|
|
} else if len(output) != 0 {
|
2014-07-16 08:03:01 -04:00
|
|
|
return fmt.Errorf("Error iptables forward: %s", output)
|
2014-06-27 03:29:55 -04:00
|
|
|
}
|
2014-07-16 08:03:01 -04:00
|
|
|
if output, err := Raw("-t", string(Filter), string(action), c.Name,
|
2014-06-27 03:29:55 -04:00
|
|
|
"-i", c.Bridge, "-o", c.Bridge,
|
|
|
|
"-p", proto,
|
|
|
|
"-s", ip2.String(),
|
|
|
|
"-d", ip1.String(),
|
2014-11-14 20:36:38 -05:00
|
|
|
"--sport", strconv.Itoa(port),
|
2014-06-27 03:29:55 -04:00
|
|
|
"-j", "ACCEPT"); err != nil {
|
|
|
|
return err
|
|
|
|
} else if len(output) != 0 {
|
2014-07-16 08:03:01 -04:00
|
|
|
return fmt.Errorf("Error iptables forward: %s", output)
|
2014-06-27 03:29:55 -04:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2014-07-16 08:03:01 -04:00
|
|
|
// Add linking rule to nat/PREROUTING chain.
|
2013-10-04 22:25:15 -04:00
|
|
|
func (c *Chain) Prerouting(action Action, args ...string) error {
|
2014-07-16 08:03:01 -04:00
|
|
|
a := []string{"-t", string(Nat), string(action), "PREROUTING"}
|
2013-10-04 22:25:15 -04:00
|
|
|
if len(args) > 0 {
|
|
|
|
a = append(a, args...)
|
|
|
|
}
|
2013-11-05 10:33:07 -05:00
|
|
|
if output, err := Raw(append(a, "-j", c.Name)...); err != nil {
|
|
|
|
return err
|
|
|
|
} else if len(output) != 0 {
|
2014-11-20 19:07:55 -05:00
|
|
|
return &ChainError{Chain: "PREROUTING", Output: output}
|
2013-11-05 10:33:07 -05:00
|
|
|
}
|
|
|
|
return nil
|
2013-10-04 22:25:15 -04:00
|
|
|
}
|
|
|
|
|
2014-07-16 08:03:01 -04:00
|
|
|
// Add linking rule to an OUTPUT chain
|
2013-10-04 22:25:15 -04:00
|
|
|
func (c *Chain) Output(action Action, args ...string) error {
|
2014-07-16 08:03:01 -04:00
|
|
|
a := []string{"-t", string(c.Table), string(action), "OUTPUT"}
|
2013-10-04 22:25:15 -04:00
|
|
|
if len(args) > 0 {
|
|
|
|
a = append(a, args...)
|
|
|
|
}
|
2013-11-05 10:33:07 -05:00
|
|
|
if output, err := Raw(append(a, "-j", c.Name)...); err != nil {
|
|
|
|
return err
|
|
|
|
} else if len(output) != 0 {
|
2014-11-20 19:07:55 -05:00
|
|
|
return &ChainError{Chain: "OUTPUT", Output: output}
|
2013-11-05 10:33:07 -05:00
|
|
|
}
|
|
|
|
return nil
|
2013-10-04 22:25:15 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Chain) Remove() error {
|
2014-11-14 20:36:38 -05:00
|
|
|
// Ignore errors - This could mean the chains were never set up
|
2014-07-16 08:03:01 -04:00
|
|
|
if c.Table == Nat {
|
|
|
|
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-10-04 22:25:15 -04:00
|
|
|
|
2014-07-16 08:03:01 -04:00
|
|
|
c.Prerouting(Delete)
|
|
|
|
c.Output(Delete)
|
|
|
|
}
|
2014-11-14 20:36:38 -05:00
|
|
|
Raw("-t", string(c.Table), "-F", c.Name)
|
|
|
|
Raw("-t", string(c.Table), "-X", c.Name)
|
2013-10-04 22:25:15 -04:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2014-07-16 08:03:01 -04:00
|
|
|
// Check if a rule exists
|
2013-10-24 12:08:50 -04:00
|
|
|
func Exists(args ...string) bool {
|
2014-07-10 00:22:01 -04: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-11-05 10:33:07 -05:00
|
|
|
}
|
2014-07-10 00:22:01 -04: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-10-24 12:08:50 -04:00
|
|
|
}
|
|
|
|
|
2014-07-16 08:03:01 -04:00
|
|
|
// Call 'iptables' system command, passing supplied arguments
|
2013-11-05 10:33:07 -05:00
|
|
|
func Raw(args ...string) ([]byte, error) {
|
2014-05-23 17:18:50 -04:00
|
|
|
|
2015-01-20 18:05:39 -05:00
|
|
|
if err := initCheck(); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2014-05-23 17:18:50 -04:00
|
|
|
if supportsXlock {
|
|
|
|
args = append([]string{"--wait"}, args...)
|
|
|
|
}
|
|
|
|
|
2015-01-20 18:05:39 -05:00
|
|
|
log.Debugf("%s, %v", iptablesPath, args)
|
2014-05-23 17:18:50 -04:00
|
|
|
|
2015-01-20 18:05:39 -05:00
|
|
|
output, err := exec.Command(iptablesPath, args...).CombinedOutput()
|
2013-11-05 10:33:07 -05:00
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("iptables failed: iptables %v: %s (%s)", strings.Join(args, " "), output, err)
|
2013-10-04 22:25:15 -04:00
|
|
|
}
|
2014-05-23 17:18:50 -04:00
|
|
|
|
2014-05-29 09:57:29 -04:00
|
|
|
// ignore iptables' message about xtables lock
|
|
|
|
if strings.Contains(string(output), "waiting for it to exit") {
|
|
|
|
output = []byte("")
|
|
|
|
}
|
|
|
|
|
2013-11-05 10:33:07 -05:00
|
|
|
return output, err
|
2013-10-04 22:25:15 -04:00
|
|
|
}
|