2021-08-23 13:14:53 +00:00
|
|
|
//go:build !windows
|
2016-04-13 08:28:18 +00:00
|
|
|
|
|
|
|
package libnetwork
|
|
|
|
|
|
|
|
import (
|
2023-01-07 00:50:18 +00:00
|
|
|
"fmt"
|
2016-04-13 08:28:18 +00:00
|
|
|
"net"
|
|
|
|
|
2021-04-06 00:24:47 +00:00
|
|
|
"github.com/docker/docker/libnetwork/iptables"
|
2016-04-13 08:28:18 +00:00
|
|
|
)
|
|
|
|
|
2016-06-11 00:32:19 +00:00
|
|
|
const (
|
2023-01-07 00:50:18 +00:00
|
|
|
// output chain used for docker embedded DNS resolver
|
2016-06-11 00:32:19 +00:00
|
|
|
outputChain = "DOCKER_OUTPUT"
|
2023-01-07 00:50:18 +00:00
|
|
|
// postrouting chain used for docker embedded DNS resolver
|
|
|
|
postroutingChain = "DOCKER_POSTROUTING"
|
2016-06-11 00:32:19 +00:00
|
|
|
)
|
|
|
|
|
2023-01-20 21:58:23 +00:00
|
|
|
func (r *Resolver) setupIPTable() error {
|
2022-10-31 21:19:07 +00:00
|
|
|
if r.err != nil {
|
|
|
|
return r.err
|
2016-04-13 08:28:18 +00:00
|
|
|
}
|
2022-10-31 21:19:07 +00:00
|
|
|
laddr := r.conn.LocalAddr().String()
|
|
|
|
ltcpaddr := r.tcpListen.Addr().String()
|
|
|
|
resolverIP, ipPort, _ := net.SplitHostPort(laddr)
|
|
|
|
_, tcpPort, _ := net.SplitHostPort(ltcpaddr)
|
2016-04-13 08:28:18 +00:00
|
|
|
rules := [][]string{
|
2022-10-31 21:19:07 +00:00
|
|
|
{"-t", "nat", "-I", outputChain, "-d", resolverIP, "-p", "udp", "--dport", dnsPort, "-j", "DNAT", "--to-destination", laddr},
|
2023-01-07 00:50:18 +00:00
|
|
|
{"-t", "nat", "-I", postroutingChain, "-s", resolverIP, "-p", "udp", "--sport", ipPort, "-j", "SNAT", "--to-source", ":" + dnsPort},
|
2022-10-31 21:19:07 +00:00
|
|
|
{"-t", "nat", "-I", outputChain, "-d", resolverIP, "-p", "tcp", "--dport", dnsPort, "-j", "DNAT", "--to-destination", ltcpaddr},
|
2023-01-07 00:50:18 +00:00
|
|
|
{"-t", "nat", "-I", postroutingChain, "-s", resolverIP, "-p", "tcp", "--sport", tcpPort, "-j", "SNAT", "--to-source", ":" + dnsPort},
|
2016-04-13 08:28:18 +00:00
|
|
|
}
|
|
|
|
|
2023-01-07 00:50:18 +00:00
|
|
|
var setupErr error
|
|
|
|
err := r.backend.ExecFunc(func() {
|
2022-10-31 21:19:07 +00:00
|
|
|
// TODO IPv6 support
|
|
|
|
iptable := iptables.GetIptable(iptables.IPv4)
|
2016-06-11 00:32:19 +00:00
|
|
|
|
2022-10-31 21:19:07 +00:00
|
|
|
// insert outputChain and postroutingchain
|
2023-05-30 18:32:27 +00:00
|
|
|
if iptable.ExistsNative("nat", "OUTPUT", "-d", resolverIP, "-j", outputChain) {
|
2023-01-07 00:50:18 +00:00
|
|
|
if err := iptable.RawCombinedOutputNative("-t", "nat", "-F", outputChain); err != nil {
|
|
|
|
setupErr = err
|
|
|
|
return
|
|
|
|
}
|
2022-10-31 21:19:07 +00:00
|
|
|
} else {
|
2023-01-07 00:50:18 +00:00
|
|
|
if err := iptable.RawCombinedOutputNative("-t", "nat", "-N", outputChain); err != nil {
|
|
|
|
setupErr = err
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if err := iptable.RawCombinedOutputNative("-t", "nat", "-I", "OUTPUT", "-d", resolverIP, "-j", outputChain); err != nil {
|
|
|
|
setupErr = err
|
|
|
|
return
|
|
|
|
}
|
2016-04-13 08:28:18 +00:00
|
|
|
}
|
|
|
|
|
2023-05-30 18:32:27 +00:00
|
|
|
if iptable.ExistsNative("nat", "POSTROUTING", "-d", resolverIP, "-j", postroutingChain) {
|
2023-01-07 00:50:18 +00:00
|
|
|
if err := iptable.RawCombinedOutputNative("-t", "nat", "-F", postroutingChain); err != nil {
|
|
|
|
setupErr = err
|
|
|
|
return
|
|
|
|
}
|
2022-10-31 21:19:07 +00:00
|
|
|
} else {
|
2023-01-07 00:50:18 +00:00
|
|
|
if err := iptable.RawCombinedOutputNative("-t", "nat", "-N", postroutingChain); err != nil {
|
|
|
|
setupErr = err
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if err := iptable.RawCombinedOutputNative("-t", "nat", "-I", "POSTROUTING", "-d", resolverIP, "-j", postroutingChain); err != nil {
|
|
|
|
setupErr = err
|
|
|
|
return
|
|
|
|
}
|
2022-10-31 21:19:07 +00:00
|
|
|
}
|
2016-04-13 08:28:18 +00:00
|
|
|
|
2022-10-31 21:19:07 +00:00
|
|
|
for _, rule := range rules {
|
|
|
|
if iptable.RawCombinedOutputNative(rule...) != nil {
|
2023-01-07 00:50:18 +00:00
|
|
|
setupErr = fmt.Errorf("set up rule failed, %v", rule)
|
|
|
|
return
|
2022-10-31 21:19:07 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
2023-01-07 00:50:18 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return setupErr
|
2016-04-13 08:28:18 +00:00
|
|
|
}
|