From 7caee2a77292467cdc611aeafe4b937dc00ef21a Mon Sep 17 00:00:00 2001 From: Albert Zhang Date: Sat, 1 Nov 2014 19:47:09 +0800 Subject: [PATCH] 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 (github: zhgwenming) --- netlink/netlink_linux.go | 11 +++-------- 1 file changed, 3 insertions(+), 8 deletions(-) diff --git a/netlink/netlink_linux.go b/netlink/netlink_linux.go index c858b1129..f89e4aacf 100644 --- a/netlink/netlink_linux.go +++ b/netlink/netlink_linux.go @@ -1002,28 +1002,23 @@ func AddRoute(destination, source, gateway, device string) error { } if source != "" { - srcIP, srcNet, err := net.ParseCIDR(source) + srcIP := net.ParseIP(source) 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) if currentFamily != -1 && currentFamily != srcFamily { return fmt.Errorf("source and destination ip were not the same IP family") } 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.Src_len = uint8(srcLen) var srcData []byte if srcFamily == syscall.AF_INET { srcData = srcIP.To4() } else { srcData = srcIP.To16() } - rtAttrs = append(rtAttrs, newRtAttr(syscall.RTA_SRC, srcData)) + rtAttrs = append(rtAttrs, newRtAttr(syscall.RTA_PREFSRC, srcData)) } if gateway != "" {