2021-08-23 09:14:53 -04:00
|
|
|
//go:build linux
|
2021-05-25 19:48:54 -04:00
|
|
|
// +build linux
|
|
|
|
|
2015-05-01 20:14:04 -04:00
|
|
|
package bridge
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"net"
|
|
|
|
|
2021-04-05 20:24:47 -04:00
|
|
|
"github.com/docker/docker/libnetwork/iptables"
|
|
|
|
"github.com/docker/docker/libnetwork/types"
|
2017-07-26 17:18:31 -04:00
|
|
|
"github.com/sirupsen/logrus"
|
2015-05-01 20:14:04 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
type link struct {
|
|
|
|
parentIP string
|
|
|
|
childIP string
|
2015-05-20 16:28:46 -04:00
|
|
|
ports []types.TransportPort
|
2015-05-01 20:14:04 -04:00
|
|
|
bridge string
|
|
|
|
}
|
|
|
|
|
|
|
|
func (l *link) String() string {
|
|
|
|
return fmt.Sprintf("%s <-> %s [%v] on %s", l.parentIP, l.childIP, l.ports, l.bridge)
|
|
|
|
}
|
|
|
|
|
2015-05-20 16:28:46 -04:00
|
|
|
func newLink(parentIP, childIP string, ports []types.TransportPort, bridge string) *link {
|
2015-05-01 20:14:04 -04:00
|
|
|
return &link{
|
|
|
|
childIP: childIP,
|
|
|
|
parentIP: parentIP,
|
|
|
|
ports: ports,
|
|
|
|
bridge: bridge,
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
func (l *link) Enable() error {
|
|
|
|
// -A == iptables append flag
|
2015-07-24 13:20:48 -04:00
|
|
|
linkFunction := func() error {
|
|
|
|
return linkContainers("-A", l.parentIP, l.childIP, l.ports, l.bridge, false)
|
|
|
|
}
|
|
|
|
|
|
|
|
iptables.OnReloaded(func() { linkFunction() })
|
|
|
|
return linkFunction()
|
2015-05-01 20:14:04 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func (l *link) Disable() {
|
|
|
|
// -D == iptables delete flag
|
|
|
|
err := linkContainers("-D", l.parentIP, l.childIP, l.ports, l.bridge, true)
|
|
|
|
if err != nil {
|
2016-11-01 00:26:14 -04:00
|
|
|
logrus.Errorf("Error removing IPTables rules for a link %s due to %s", l.String(), err.Error())
|
2015-05-01 20:14:04 -04:00
|
|
|
}
|
|
|
|
// Return proper error once we move to use a proper iptables package
|
|
|
|
// that returns typed errors
|
|
|
|
}
|
|
|
|
|
2015-05-20 16:28:46 -04:00
|
|
|
func linkContainers(action, parentIP, childIP string, ports []types.TransportPort, bridge string,
|
2015-05-01 20:14:04 -04:00
|
|
|
ignoreErrors bool) error {
|
|
|
|
var nfAction iptables.Action
|
|
|
|
|
|
|
|
switch action {
|
|
|
|
case "-A":
|
|
|
|
nfAction = iptables.Append
|
|
|
|
case "-I":
|
|
|
|
nfAction = iptables.Insert
|
|
|
|
case "-D":
|
|
|
|
nfAction = iptables.Delete
|
|
|
|
default:
|
2015-05-14 17:56:15 -04:00
|
|
|
return InvalidIPTablesCfgError(action)
|
2015-05-01 20:14:04 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
ip1 := net.ParseIP(parentIP)
|
|
|
|
if ip1 == nil {
|
|
|
|
return InvalidLinkIPAddrError(parentIP)
|
|
|
|
}
|
|
|
|
ip2 := net.ParseIP(childIP)
|
|
|
|
if ip2 == nil {
|
|
|
|
return InvalidLinkIPAddrError(childIP)
|
|
|
|
}
|
|
|
|
|
2015-06-11 21:12:00 -04:00
|
|
|
chain := iptables.ChainInfo{Name: DockerChain}
|
2015-05-01 20:14:04 -04:00
|
|
|
for _, port := range ports {
|
2015-06-11 21:12:00 -04:00
|
|
|
err := chain.Link(nfAction, ip1, ip2, int(port.Port), port.Proto.String(), bridge)
|
2015-05-01 20:14:04 -04:00
|
|
|
if !ignoreErrors && err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|