2015-05-01 20:14:04 -04:00
|
|
|
package bridge
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"net"
|
|
|
|
|
|
|
|
log "github.com/Sirupsen/logrus"
|
2015-05-16 19:02:51 -04:00
|
|
|
"github.com/docker/libnetwork/iptables"
|
2015-05-20 16:28:46 -04:00
|
|
|
"github.com/docker/libnetwork/types"
|
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 {
|
|
|
|
log.Errorf("Error removing IPTables rules for a link %s due to %s", l.String(), err.Error())
|
|
|
|
}
|
|
|
|
// 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
|
|
|
|
}
|