2015-02-22 20:24:22 -05:00
|
|
|
package bridge
|
|
|
|
|
|
|
|
import (
|
2016-11-21 20:29:53 -05:00
|
|
|
"errors"
|
2015-05-18 19:49:12 -04:00
|
|
|
"fmt"
|
|
|
|
"io/ioutil"
|
2016-09-17 01:40:44 -04:00
|
|
|
"net"
|
2015-05-18 19:49:12 -04:00
|
|
|
"path/filepath"
|
|
|
|
|
2016-11-01 00:26:14 -04:00
|
|
|
"github.com/Sirupsen/logrus"
|
2015-10-05 17:53:25 -04:00
|
|
|
"github.com/docker/libnetwork/types"
|
2015-02-22 20:24:22 -05:00
|
|
|
"github.com/vishvananda/netlink"
|
|
|
|
)
|
|
|
|
|
2016-09-17 01:40:44 -04:00
|
|
|
func selectIPv4Address(addresses []netlink.Addr, selector *net.IPNet) (netlink.Addr, error) {
|
|
|
|
if len(addresses) == 0 {
|
2016-11-21 20:29:53 -05:00
|
|
|
return netlink.Addr{}, errors.New("unable to select an address as the address pool is empty")
|
2016-09-17 01:40:44 -04:00
|
|
|
}
|
|
|
|
if selector != nil {
|
|
|
|
for _, addr := range addresses {
|
|
|
|
if selector.Contains(addr.IP) {
|
|
|
|
return addr, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return addresses[0], nil
|
|
|
|
}
|
|
|
|
|
2015-05-22 13:56:36 -04:00
|
|
|
func setupBridgeIPv4(config *networkConfiguration, i *bridgeInterface) error {
|
2016-09-17 01:40:44 -04:00
|
|
|
addrv4List, _, err := i.addresses()
|
2015-05-03 16:23:52 -04:00
|
|
|
if err != nil {
|
2015-10-05 17:53:25 -04:00
|
|
|
return fmt.Errorf("failed to retrieve bridge interface addresses: %v", err)
|
2015-02-22 20:24:22 -05:00
|
|
|
}
|
|
|
|
|
2016-09-17 01:40:44 -04:00
|
|
|
addrv4, _ := selectIPv4Address(addrv4List, config.AddressIPv4)
|
|
|
|
|
2015-10-05 17:53:25 -04:00
|
|
|
if !types.CompareIPNet(addrv4.IPNet, config.AddressIPv4) {
|
|
|
|
if addrv4.IPNet != nil {
|
2016-05-16 14:51:40 -04:00
|
|
|
if err := i.nlh.AddrDel(i.Link, &addrv4); err != nil {
|
2015-10-05 17:53:25 -04:00
|
|
|
return fmt.Errorf("failed to remove current ip address from bridge: %v", err)
|
|
|
|
}
|
|
|
|
}
|
2016-11-01 00:26:14 -04:00
|
|
|
logrus.Debugf("Assigning address to bridge interface %s: %s", config.BridgeName, config.AddressIPv4)
|
2016-05-16 14:51:40 -04:00
|
|
|
if err := i.nlh.AddrAdd(i.Link, &netlink.Addr{IPNet: config.AddressIPv4}); err != nil {
|
2015-10-05 17:53:25 -04:00
|
|
|
return &IPv4AddrAddError{IP: config.AddressIPv4, Err: err}
|
|
|
|
}
|
2015-02-27 12:11:53 -05:00
|
|
|
}
|
|
|
|
|
2015-04-24 18:13:44 -04:00
|
|
|
// Store bridge network and default gateway
|
2015-10-05 17:53:25 -04:00
|
|
|
i.bridgeIPv4 = config.AddressIPv4
|
|
|
|
i.gatewayIPv4 = config.AddressIPv4.IP
|
2015-04-10 12:02:25 -04:00
|
|
|
|
2015-02-27 12:11:53 -05:00
|
|
|
return nil
|
2015-02-22 20:24:22 -05:00
|
|
|
}
|
|
|
|
|
2015-05-22 13:56:36 -04:00
|
|
|
func setupGatewayIPv4(config *networkConfiguration, i *bridgeInterface) error {
|
2015-04-24 18:13:44 -04:00
|
|
|
if !i.bridgeIPv4.Contains(config.DefaultGatewayIPv4) {
|
2015-05-14 17:56:15 -04:00
|
|
|
return &ErrInvalidGateway{}
|
2015-04-24 18:13:44 -04:00
|
|
|
}
|
2015-06-12 02:21:50 -04:00
|
|
|
|
2015-04-24 18:13:44 -04:00
|
|
|
// Store requested default gateway
|
|
|
|
i.gatewayIPv4 = config.DefaultGatewayIPv4
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
2015-05-18 19:49:12 -04:00
|
|
|
|
2015-05-22 13:56:36 -04:00
|
|
|
func setupLoopbackAdressesRouting(config *networkConfiguration, i *bridgeInterface) error {
|
2015-05-18 19:49:12 -04:00
|
|
|
sysPath := filepath.Join("/proc/sys/net/ipv4/conf", config.BridgeName, "route_localnet")
|
2015-07-27 07:31:03 -04:00
|
|
|
ipv4LoRoutingData, err := ioutil.ReadFile(sysPath)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("Cannot read IPv4 local routing setup: %v", err)
|
|
|
|
}
|
|
|
|
// Enable loopback adresses routing only if it isn't already enabled
|
|
|
|
if ipv4LoRoutingData[0] != '1' {
|
|
|
|
if err := ioutil.WriteFile(sysPath, []byte{'1', '\n'}, 0644); err != nil {
|
|
|
|
return fmt.Errorf("Unable to enable local routing for hairpin mode: %v", err)
|
|
|
|
}
|
2015-05-18 19:49:12 -04:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|