2016-04-13 04:28:18 -04:00
|
|
|
// +build !windows
|
|
|
|
|
|
|
|
package libnetwork
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"net"
|
|
|
|
"os"
|
|
|
|
"os/exec"
|
|
|
|
"runtime"
|
|
|
|
|
|
|
|
log "github.com/Sirupsen/logrus"
|
|
|
|
"github.com/docker/docker/pkg/reexec"
|
|
|
|
"github.com/docker/libnetwork/iptables"
|
|
|
|
"github.com/vishvananda/netns"
|
|
|
|
)
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
reexec.Register("setup-resolver", reexecSetupResolver)
|
|
|
|
}
|
|
|
|
|
2016-06-10 20:32:19 -04:00
|
|
|
const (
|
|
|
|
// outputChain used for docker embed dns
|
|
|
|
outputChain = "DOCKER_OUTPUT"
|
|
|
|
//postroutingchain used for docker embed dns
|
|
|
|
postroutingchain = "DOCKER_POSTROUTING"
|
|
|
|
)
|
|
|
|
|
2016-04-13 04:28:18 -04:00
|
|
|
func reexecSetupResolver() {
|
|
|
|
runtime.LockOSThread()
|
|
|
|
defer runtime.UnlockOSThread()
|
|
|
|
|
|
|
|
if len(os.Args) < 4 {
|
|
|
|
log.Error("invalid number of arguments..")
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
|
2016-09-19 18:48:06 -04:00
|
|
|
resolverIP, ipPort, _ := net.SplitHostPort(os.Args[2])
|
2016-04-13 04:28:18 -04:00
|
|
|
_, tcpPort, _ := net.SplitHostPort(os.Args[3])
|
|
|
|
rules := [][]string{
|
2016-06-10 20:32:19 -04:00
|
|
|
{"-t", "nat", "-I", outputChain, "-d", resolverIP, "-p", "udp", "--dport", dnsPort, "-j", "DNAT", "--to-destination", os.Args[2]},
|
|
|
|
{"-t", "nat", "-I", postroutingchain, "-s", resolverIP, "-p", "udp", "--sport", ipPort, "-j", "SNAT", "--to-source", ":" + dnsPort},
|
|
|
|
{"-t", "nat", "-I", outputChain, "-d", resolverIP, "-p", "tcp", "--dport", dnsPort, "-j", "DNAT", "--to-destination", os.Args[3]},
|
|
|
|
{"-t", "nat", "-I", postroutingchain, "-s", resolverIP, "-p", "tcp", "--sport", tcpPort, "-j", "SNAT", "--to-source", ":" + dnsPort},
|
2016-04-13 04:28:18 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
f, err := os.OpenFile(os.Args[1], os.O_RDONLY, 0)
|
|
|
|
if err != nil {
|
|
|
|
log.Errorf("failed get network namespace %q: %v", os.Args[1], err)
|
|
|
|
os.Exit(2)
|
|
|
|
}
|
|
|
|
defer f.Close()
|
|
|
|
|
|
|
|
nsFD := f.Fd()
|
|
|
|
if err = netns.Set(netns.NsHandle(nsFD)); err != nil {
|
|
|
|
log.Errorf("setting into container net ns %v failed, %v", os.Args[1], err)
|
|
|
|
os.Exit(3)
|
|
|
|
}
|
|
|
|
|
2016-06-10 20:32:19 -04:00
|
|
|
// insert outputChain and postroutingchain
|
|
|
|
err = iptables.RawCombinedOutputNative("-t", "nat", "-C", "OUTPUT", "-d", resolverIP, "-j", outputChain)
|
|
|
|
if err == nil {
|
|
|
|
iptables.RawCombinedOutputNative("-t", "nat", "-F", outputChain)
|
|
|
|
} else {
|
|
|
|
iptables.RawCombinedOutputNative("-t", "nat", "-N", outputChain)
|
|
|
|
iptables.RawCombinedOutputNative("-t", "nat", "-I", "OUTPUT", "-d", resolverIP, "-j", outputChain)
|
|
|
|
}
|
|
|
|
|
|
|
|
err = iptables.RawCombinedOutputNative("-t", "nat", "-C", "POSTROUTING", "-d", resolverIP, "-j", postroutingchain)
|
|
|
|
if err == nil {
|
|
|
|
iptables.RawCombinedOutputNative("-t", "nat", "-F", postroutingchain)
|
|
|
|
} else {
|
|
|
|
iptables.RawCombinedOutputNative("-t", "nat", "-N", postroutingchain)
|
|
|
|
iptables.RawCombinedOutputNative("-t", "nat", "-I", "POSTROUTING", "-d", resolverIP, "-j", postroutingchain)
|
|
|
|
}
|
|
|
|
|
2016-04-13 04:28:18 -04:00
|
|
|
for _, rule := range rules {
|
|
|
|
if iptables.RawCombinedOutputNative(rule...) != nil {
|
|
|
|
log.Errorf("setting up rule failed, %v", rule)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *resolver) setupIPTable() error {
|
|
|
|
if r.err != nil {
|
|
|
|
return r.err
|
|
|
|
}
|
|
|
|
laddr := r.conn.LocalAddr().String()
|
|
|
|
ltcpaddr := r.tcpListen.Addr().String()
|
|
|
|
|
|
|
|
cmd := &exec.Cmd{
|
|
|
|
Path: reexec.Self(),
|
2016-09-19 18:48:06 -04:00
|
|
|
Args: append([]string{"setup-resolver"}, r.resolverKey, laddr, ltcpaddr),
|
2016-04-13 04:28:18 -04:00
|
|
|
Stdout: os.Stdout,
|
|
|
|
Stderr: os.Stderr,
|
|
|
|
}
|
|
|
|
if err := cmd.Run(); err != nil {
|
|
|
|
return fmt.Errorf("reexec failed: %v", err)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|