Use RTA_PREFSRC instead of RTA_SRC to make AddRoute() works with a provided source ip address.

Docker-DCO-1.1-Signed-off-by: Albert Zhang <zhgwenming@gmail.com> (github: zhgwenming)
This commit is contained in:
Albert Zhang 2014-11-01 19:47:09 +08:00
parent d3eb885553
commit 7caee2a772
1 changed files with 3 additions and 8 deletions

View File

@ -1002,28 +1002,23 @@ func AddRoute(destination, source, gateway, device string) error {
} }
if source != "" { if source != "" {
srcIP, srcNet, err := net.ParseCIDR(source) srcIP := net.ParseIP(source)
if err != nil { if err != nil {
return fmt.Errorf("source CIDR %s couldn't be parsed", source) return fmt.Errorf("source IP %s couldn't be parsed", source)
} }
srcFamily := getIpFamily(srcIP) srcFamily := getIpFamily(srcIP)
if currentFamily != -1 && currentFamily != srcFamily { if currentFamily != -1 && currentFamily != srcFamily {
return fmt.Errorf("source and destination ip were not the same IP family") return fmt.Errorf("source and destination ip were not the same IP family")
} }
currentFamily = srcFamily currentFamily = srcFamily
srcLen, bits := srcNet.Mask.Size()
if srcLen == 0 && bits == 0 {
return fmt.Errorf("source CIDR %s generated a non-canonical Mask", source)
}
msg.Family = uint8(srcFamily) msg.Family = uint8(srcFamily)
msg.Src_len = uint8(srcLen)
var srcData []byte var srcData []byte
if srcFamily == syscall.AF_INET { if srcFamily == syscall.AF_INET {
srcData = srcIP.To4() srcData = srcIP.To4()
} else { } else {
srcData = srcIP.To16() srcData = srcIP.To16()
} }
rtAttrs = append(rtAttrs, newRtAttr(syscall.RTA_SRC, srcData)) rtAttrs = append(rtAttrs, newRtAttr(syscall.RTA_PREFSRC, srcData))
} }
if gateway != "" { if gateway != "" {