2013-10-04 22:25:15 -04:00
|
|
|
package iptables
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
"net"
|
2013-11-06 10:37:43 -05:00
|
|
|
"os"
|
2013-10-04 22:25:15 -04:00
|
|
|
"os/exec"
|
|
|
|
"strconv"
|
|
|
|
"strings"
|
|
|
|
)
|
|
|
|
|
|
|
|
type Action string
|
|
|
|
|
|
|
|
const (
|
|
|
|
Add Action = "-A"
|
|
|
|
Delete Action = "-D"
|
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
ErrIptablesNotFound = errors.New("Iptables not found")
|
|
|
|
nat = []string{"-t", "nat"}
|
2014-05-23 17:18:50 -04:00
|
|
|
supportsXlock = false
|
2013-10-04 22:25:15 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
type Chain struct {
|
|
|
|
Name string
|
|
|
|
Bridge string
|
|
|
|
}
|
|
|
|
|
2014-05-23 17:18:50 -04:00
|
|
|
func init() {
|
|
|
|
supportsXlock = exec.Command("iptables", "--wait", "-L", "-n").Run() == nil
|
|
|
|
}
|
|
|
|
|
2013-10-04 22:25:15 -04:00
|
|
|
func NewChain(name, bridge string) (*Chain, error) {
|
2013-11-05 10:33:07 -05:00
|
|
|
if output, err := Raw("-t", "nat", "-N", name); err != nil {
|
2013-10-04 22:25:15 -04:00
|
|
|
return nil, err
|
2013-11-05 10:33:07 -05:00
|
|
|
} else if len(output) != 0 {
|
|
|
|
return nil, fmt.Errorf("Error creating new iptables chain: %s", output)
|
2013-10-04 22:25:15 -04:00
|
|
|
}
|
|
|
|
chain := &Chain{
|
|
|
|
Name: name,
|
|
|
|
Bridge: bridge,
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := chain.Prerouting(Add, "-m", "addrtype", "--dst-type", "LOCAL"); err != nil {
|
|
|
|
return nil, fmt.Errorf("Failed to inject docker in PREROUTING chain: %s", err)
|
|
|
|
}
|
|
|
|
if err := chain.Output(Add, "-m", "addrtype", "--dst-type", "LOCAL", "!", "--dst", "127.0.0.0/8"); err != nil {
|
|
|
|
return nil, fmt.Errorf("Failed to inject docker in OUTPUT chain: %s", err)
|
|
|
|
}
|
|
|
|
return chain, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func RemoveExistingChain(name string) error {
|
|
|
|
chain := &Chain{
|
|
|
|
Name: name,
|
|
|
|
}
|
|
|
|
return chain.Remove()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Chain) Forward(action Action, ip net.IP, port int, proto, dest_addr string, dest_port 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"
|
|
|
|
}
|
2013-11-05 10:33:07 -05:00
|
|
|
if output, err := Raw("-t", "nat", fmt.Sprint(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-04-09 07:55:08 -04:00
|
|
|
"!", "-i", c.Bridge,
|
2013-10-04 22:25:15 -04:00
|
|
|
"-j", "DNAT",
|
2013-11-05 10:33:07 -05:00
|
|
|
"--to-destination", net.JoinHostPort(dest_addr, strconv.Itoa(dest_port))); err != nil {
|
|
|
|
return err
|
|
|
|
} else if len(output) != 0 {
|
|
|
|
return fmt.Errorf("Error iptables forward: %s", output)
|
|
|
|
}
|
2014-01-24 23:22:53 -05:00
|
|
|
|
|
|
|
fAction := action
|
|
|
|
if fAction == Add {
|
|
|
|
fAction = "-I"
|
|
|
|
}
|
|
|
|
if output, err := Raw(string(fAction), "FORWARD",
|
|
|
|
"!", "-i", c.Bridge,
|
|
|
|
"-o", c.Bridge,
|
|
|
|
"-p", proto,
|
2014-02-04 08:08:31 -05:00
|
|
|
"-d", dest_addr,
|
|
|
|
"--dport", strconv.Itoa(dest_port),
|
2014-01-24 23:22:53 -05:00
|
|
|
"-j", "ACCEPT"); err != nil {
|
|
|
|
return err
|
|
|
|
} else if len(output) != 0 {
|
|
|
|
return fmt.Errorf("Error iptables forward: %s", output)
|
|
|
|
}
|
|
|
|
|
2013-11-05 10:33:07 -05:00
|
|
|
return nil
|
2013-10-04 22:25:15 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Chain) Prerouting(action Action, args ...string) error {
|
|
|
|
a := append(nat, fmt.Sprint(action), "PREROUTING")
|
|
|
|
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 {
|
|
|
|
return fmt.Errorf("Error iptables prerouting: %s", output)
|
|
|
|
}
|
|
|
|
return nil
|
2013-10-04 22:25:15 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Chain) Output(action Action, args ...string) error {
|
|
|
|
a := append(nat, fmt.Sprint(action), "OUTPUT")
|
|
|
|
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 {
|
|
|
|
return fmt.Errorf("Error iptables output: %s", output)
|
|
|
|
}
|
|
|
|
return nil
|
2013-10-04 22:25:15 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Chain) Remove() error {
|
|
|
|
// 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
|
|
|
|
|
|
|
|
c.Prerouting(Delete)
|
|
|
|
c.Output(Delete)
|
|
|
|
|
|
|
|
Raw("-t", "nat", "-F", c.Name)
|
|
|
|
Raw("-t", "nat", "-X", c.Name)
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2013-10-24 12:08:50 -04:00
|
|
|
// Check if an existing rule exists
|
|
|
|
func Exists(args ...string) bool {
|
2013-11-05 10:33:07 -05:00
|
|
|
if _, err := Raw(append([]string{"-C"}, args...)...); err != nil {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
return true
|
2013-10-24 12:08:50 -04:00
|
|
|
}
|
|
|
|
|
2013-11-05 10:33:07 -05:00
|
|
|
func Raw(args ...string) ([]byte, error) {
|
2013-10-04 22:25:15 -04:00
|
|
|
path, err := exec.LookPath("iptables")
|
|
|
|
if err != nil {
|
2013-11-05 10:33:07 -05:00
|
|
|
return nil, ErrIptablesNotFound
|
2013-10-04 22:25:15 -04:00
|
|
|
}
|
2014-05-23 17:18:50 -04:00
|
|
|
|
|
|
|
if supportsXlock {
|
|
|
|
args = append([]string{"--wait"}, args...)
|
|
|
|
}
|
|
|
|
|
2013-11-06 10:37:43 -05:00
|
|
|
if os.Getenv("DEBUG") != "" {
|
|
|
|
fmt.Printf("[DEBUG] [iptables]: %s, %v\n", path, args)
|
|
|
|
}
|
2014-05-23 17:18:50 -04:00
|
|
|
|
|
|
|
output, err := exec.Command(path, 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
|
|
|
|
2013-11-05 10:33:07 -05:00
|
|
|
return output, err
|
2013-10-04 22:25:15 -04:00
|
|
|
}
|