Skip to content

Commit c65b789

Browse files
author
Kathryn Baldauf
authored
Fix issue with mask length of gatway addresses that are out of the (microsoft#2305)
interface's subnet * See documentation at https://pkg.go.dev/net#ParseIP Signed-off-by: Kathryn Baldauf <[email protected]>
1 parent 1c29e9d commit c65b789

1 file changed

Lines changed: 12 additions & 1 deletion

File tree

internal/guest/network/netns.go

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -237,7 +237,18 @@ func assignIPToLink(ctx context.Context,
237237
// unreachable error when adding this out-of-subnet gw route
238238
entry.Debugf("gw is outside of the subnet: Configure %s in %d with: %s/%d gw=%s\n",
239239
ifStr, nsPid, allocatedIP, prefixLen, gatewayIP)
240-
ml := len(gw) * 8
240+
241+
// net library's ParseIP call always returns an array of length 16, so we
242+
// need to first check if the address is IPv4 or IPv6 before calculating
243+
// the mask length. See https://pkg.go.dev/net#ParseIP.
244+
ml := 8
245+
if gw.To4() != nil {
246+
ml *= net.IPv4len
247+
} else if gw.To16() != nil {
248+
ml *= net.IPv6len
249+
} else {
250+
return fmt.Errorf("gw IP is neither IPv4 nor IPv6: %v", gw)
251+
}
241252
addr2 := &net.IPNet{
242253
IP: gw,
243254
Mask: net.CIDRMask(ml, ml)}

0 commit comments

Comments
 (0)