Skip to content
This repository was archived by the owner on Jun 20, 2024. It is now read-only.

Commit

Permalink
Merge pull request #1053 from weaveworks/437-check-address
Browse files Browse the repository at this point in the history
Check addresses supplied by users for conflict with existing routes.

Fixes #437. Fixes #480.
  • Loading branch information
rade committed Jul 1, 2015
2 parents edf3ec9 + 84238a0 commit ecfc5d0
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 6 deletions.
1 change: 1 addition & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ $(WEAVER_EXE) $(WEAVEDNS_EXE) $(WEAVEPROXY_EXE) $(NETCHECK_EXE): common/*.go com
$(WEAVER_EXE): router/*.go ipam/*.go ipam/*/*.go prog/weaver/main.go
$(WEAVEDNS_EXE): nameserver/*.go prog/weavedns/main.go
$(WEAVEPROXY_EXE): proxy/*.go prog/weaveproxy/main.go
$(NETCHECK_EXE): prog/netcheck/netcheck.go

# Sigproxy and weavewait need separate rules as they fail the netgo check in
# the main build stanza due to not importing net package
Expand Down
17 changes: 16 additions & 1 deletion net/route.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ func CheckNetworkFree(subnet *net.IPNet) error {
}
for _, route := range routes {
if route.Dst != nil && overlaps(route.Dst, subnet) {
return fmt.Errorf("network %s would overlap with route %s", subnet, route.Dst)
return fmt.Errorf("Network %s overlaps with existing route %s", subnet, route.Dst)
}
}
return nil
Expand All @@ -26,3 +26,18 @@ func CheckNetworkFree(subnet *net.IPNet) error {
func overlaps(n1, n2 *net.IPNet) bool {
return n1.Contains(n2.IP) || n2.Contains(n1.IP)
}

// For a specific address, we only care if it is actually *inside* an
// existing route, because weave-local traffic never hits IP routing.
func CheckAddressOverlap(addr net.IP) error {
routes, err := netlink.RouteList(nil, netlink.FAMILY_V4)
if err != nil {
return err
}
for _, route := range routes {
if route.Dst != nil && route.Dst.Contains(addr) {
return fmt.Errorf("Address %s overlaps with existing route %s", addr, route.Dst)
}
}
return nil
}
15 changes: 10 additions & 5 deletions prog/netcheck/netcheck.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/* netcheck: check whether a given network overlaps with any existing routes */
/* netcheck: check whether a given network or address overlaps with any existing routes */
package main

import (
Expand All @@ -10,7 +10,7 @@ import (
)

func fatal(err error) {
fmt.Println(err)
fmt.Fprintln(os.Stderr, err)
os.Exit(1)
}

Expand All @@ -19,12 +19,17 @@ func main() {
os.Exit(0)
}

ipRangeStr := os.Args[1]
_, ipnet, err := net.ParseCIDR(ipRangeStr)
cidrStr := os.Args[1]
addr, ipnet, err := net.ParseCIDR(cidrStr)
if err != nil {
fatal(err)
}
if err := weavenet.CheckNetworkFree(ipnet); err != nil {
if ipnet.IP.Equal(addr) {
err = weavenet.CheckNetworkFree(ipnet)
} else {
err = weavenet.CheckAddressOverlap(addr)
}
if err != nil {
fatal(err)
}
os.Exit(0)
Expand Down
2 changes: 2 additions & 0 deletions weave
Original file line number Diff line number Diff line change
Expand Up @@ -858,6 +858,8 @@ ipam_cidrs() {
IPAM_CIDRS="$IPAM_CIDRS $CIDR"
ALL_CIDRS="$ALL_CIDRS $CIDR"
else
# This is a plain IP address; warn if it clashes but carry on
command_exists netcheck && netcheck $1 || true
ALL_CIDRS="$ALL_CIDRS $1"
fi
shift 1
Expand Down

0 comments on commit ecfc5d0

Please sign in to comment.