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
|
|
|
|
2015-03-26 22:22:04 +00:00
|
|
|
"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"
|
2015-02-14 03:10:14 +00:00
|
|
|
Mangle Table = "mangle"
|
2013-12-23 23:36:58 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
2015-01-20 23:05:39 +00:00
|
|
|
iptablesPath string
|
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))
|
|
|
|
}
|
|
|
|
|
2015-01-20 23:05:39 +00: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 21:18:50 +00:00
|
|
|
}
|
|
|
|
|
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"}
|
2015-02-14 03:10:14 +00:00
|
|
|
if !Exists(Nat, "PREROUTING", preroute...) {
|
2014-07-16 12:03:01 +00:00
|
|
|
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"}
|
2015-02-14 03:10:14 +00:00
|
|
|
if !Exists(Nat, "OUTPUT", output...) {
|
2014-07-16 12:03:01 +00:00
|
|
|
if err := c.Output(Append, output...); err != nil {
|
|
|
|
return nil, fmt.Errorf("Failed to inject docker in OUTPUT chain: %s", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
case Filter:
|
2015-02-14 03:10:14 +00:00
|
|
|
link := []string{
|
2014-07-16 12:03:01 +00:00
|
|
|
"-o", c.Bridge,
|
|
|
|
"-j", c.Name}
|
2015-02-14 03:10:14 +00:00
|
|
|
if !Exists(Filter, "FORWARD", link...) {
|
|
|
|
insert := append([]string{string(Insert), "FORWARD"}, link...)
|
2014-07-16 12:03:01 +00:00
|
|
|
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
|
2014-11-15 01:36:38 +00:00
|
|
|
func (c *Chain) Forward(action Action, ip net.IP, port int, proto, destAddr string, destPort int) error {
|
2013-12-23 23:36:58 +00: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 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",
|
2014-11-15 01:36:38 +00:00
|
|
|
"--to-destination", net.JoinHostPort(destAddr, strconv.Itoa(destPort))); err != nil {
|
2013-12-23 23:36:58 +00:00
|
|
|
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-11-15 01:36:38 +00:00
|
|
|
"-d", destAddr,
|
|
|
|
"--dport", strconv.Itoa(destPort),
|
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
|
|
|
}
|
|
|
|
|
2014-11-15 01:36:38 +00: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-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(),
|
|
|
|
"-d", ip2.String(),
|
2014-11-15 01:36:38 +00:00
|
|
|
"--dport", strconv.Itoa(port),
|
2014-06-27 07:29:55 +00:00
|
|
|
"-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(),
|
|
|
|
"-d", ip1.String(),
|
2014-11-15 01:36:38 +00:00
|
|
|
"--sport", strconv.Itoa(port),
|
2014-06-27 07:29:55 +00:00
|
|
|
"-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-11-15 01:36:38 +00:00
|
|
|
// Ignore errors - This could mean the chains were never set up
|
2014-07-16 12:03:01 +00: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-12-23 23:36:58 +00:00
|
|
|
|
2014-07-16 12:03:01 +00:00
|
|
|
c.Prerouting(Delete)
|
|
|
|
c.Output(Delete)
|
|
|
|
}
|
2014-11-15 01:36:38 +00:00
|
|
|
Raw("-t", string(c.Table), "-F", c.Name)
|
|
|
|
Raw("-t", string(c.Table), "-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
|
2015-02-14 03:10:14 +00:00
|
|
|
func Exists(table Table, chain string, rule ...string) bool {
|
|
|
|
if string(table) == "" {
|
|
|
|
table = Filter
|
|
|
|
}
|
|
|
|
|
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
|
2015-02-14 03:10:14 +00:00
|
|
|
if _, err := Raw(append([]string{
|
|
|
|
"-t", string(table), "-C", chain}, rule...)...); err == nil {
|
2014-07-10 04:22:01 +00:00
|
|
|
return true
|
2013-12-23 23:36:58 +00:00
|
|
|
}
|
2014-07-10 04:22:01 +00:00
|
|
|
|
2015-02-14 03:10:14 +00:00
|
|
|
// parse "iptables -S" for the rule (this checks rules in a specific chain
|
|
|
|
// in a specific table)
|
2015-03-26 01:40:23 +00:00
|
|
|
ruleString := strings.Join(rule, " ")
|
2015-02-14 03:10:14 +00:00
|
|
|
existingRules, _ := exec.Command("iptables", "-t", string(table), "-S", chain).Output()
|
2014-07-10 04:22:01 +00:00
|
|
|
|
|
|
|
// 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), "?"),
|
2015-03-26 01:40:23 +00:00
|
|
|
re.ReplaceAllString(ruleString, "?"),
|
2014-07-10 04:22:01 +00:00
|
|
|
)
|
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) {
|
2014-05-23 21:18:50 +00:00
|
|
|
|
2015-01-20 23:05:39 +00:00
|
|
|
if err := initCheck(); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2014-05-23 21:18:50 +00:00
|
|
|
if supportsXlock {
|
|
|
|
args = append([]string{"--wait"}, args...)
|
|
|
|
}
|
|
|
|
|
2015-03-26 22:22:04 +00:00
|
|
|
logrus.Debugf("%s, %v", iptablesPath, args)
|
2014-05-23 21:18:50 +00:00
|
|
|
|
2015-01-20 23:05:39 +00:00
|
|
|
output, err := exec.Command(iptablesPath, 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
|
|
|
|
}
|