1
0
Fork 0
mirror of https://github.com/moby/moby.git synced 2022-11-09 12:21:53 -05:00

Merge pull request #1374 from dotcloud/steeve-patch-1

- Runtime: Handle ip route showing mask-less IP addresses
This commit is contained in:
Guillaume J. Charmes 2013-08-05 14:13:20 -07:00
commit 2f1c05d997
2 changed files with 44 additions and 12 deletions

View file

@ -93,20 +93,29 @@ func iptables(args ...string) error {
return nil
}
func checkRouteOverlaps(dockerNetwork *net.IPNet) error {
output, err := ip("route")
if err != nil {
return err
}
utils.Debugf("Routes:\n\n%s", output)
for _, line := range strings.Split(output, "\n") {
func checkRouteOverlaps(routes string, dockerNetwork *net.IPNet) error {
utils.Debugf("Routes:\n\n%s", routes)
for _, line := range strings.Split(routes, "\n") {
if strings.Trim(line, "\r\n\t ") == "" || strings.Contains(line, "default") {
continue
}
if _, network, err := net.ParseCIDR(strings.Split(line, " ")[0]); err != nil {
return fmt.Errorf("Unexpected ip route output: %s (%s)", err, line)
} else if networkOverlaps(dockerNetwork, network) {
return fmt.Errorf("Network %s is already routed: '%s'", dockerNetwork.String(), line)
_, network, err := net.ParseCIDR(strings.Split(line, " ")[0])
if err != nil {
// is this a mask-less IP address?
if ip := net.ParseIP(strings.Split(line, " ")[0]); ip == nil {
// fail only if it's neither a network nor a mask-less IP address
return fmt.Errorf("Unexpected ip route output: %s (%s)", err, line)
} else {
_, network, err = net.ParseCIDR(ip.String() + "/32")
if err != nil {
return err
}
}
}
if err == nil && network != nil {
if networkOverlaps(dockerNetwork, network) {
return fmt.Errorf("Network %s is already routed: '%s'", dockerNetwork, line)
}
}
}
return nil
@ -142,7 +151,11 @@ func CreateBridgeIface(ifaceName string) error {
if err != nil {
return err
}
if err := checkRouteOverlaps(dockerNetwork); err == nil {
routes, err := ip("route")
if err != nil {
return err
}
if err := checkRouteOverlaps(routes, dockerNetwork); err == nil {
ifaceAddr = addr
break
} else {

View file

@ -383,3 +383,22 @@ func TestNetworkOverlaps(t *testing.T) {
//netX starts and ends before netY
AssertNoOverlap("172.16.1.1/25", "172.16.2.1/24", t)
}
func TestCheckRouteOverlaps(t *testing.T) {
routes := `default via 10.0.2.2 dev eth0
10.0.2.0 dev eth0 proto kernel scope link src 10.0.2.15
10.0.3.0/24 dev lxcbr0 proto kernel scope link src 10.0.3.1
10.0.42.0/24 dev testdockbr0 proto kernel scope link src 10.0.42.1
172.16.42.0/24 dev docker0 proto kernel scope link src 172.16.42.1
192.168.142.0/24 dev eth1 proto kernel scope link src 192.168.142.142`
_, netX, _ := net.ParseCIDR("172.16.0.1/24")
if err := checkRouteOverlaps(routes, netX); err != nil {
t.Fatal(err)
}
_, netX, _ = net.ParseCIDR("10.0.2.0/24")
if err := checkRouteOverlaps(routes, netX); err == nil {
t.Fatalf("10.0.2.0/24 and 10.0.2.0 should overlap but it doesn't")
}
}