Commit e05b48e2 authored by Mikio Hara's avatar Mikio Hara

net, internal/syscall/windows: fix interface and address identification on windows

The current implementation including Go 1.5 through 1.5.2 misuses
Windows API and mishandles the returned values from GetAdapterAddresses
on Windows. This change fixes various issues related to network facility
information by readjusting interface and interface address parsers.

Updates #5395.
Updates #10530.
Updates #12301.
Updates #12551.
Updates #13542.
Fixes #12691.
Fixes #12811.
Fixes #13476.
Fixes #13544.

Also fixes fragile screen scraping test cases in net_windows_test.go.

Additional information for reviewers:

It seems like almost all the issues above have the same root cause and
it is misunderstanding of Windows API. If my interpretation of the
information on MSDN is correctly, current implementation contains the
following bugs:

- SIO_GET_INTERFACE_LIST should not be used for IPv6. The behavior of
  SIO_GET_INTERFACE_LIST is different on kernels and probably it doesn't
  work correctly for IPv6 on old kernels such as Windows XP w/ SP2.
  Unfortunately MSDN doesn't describe the detail of
  SIO_GET_INTERFACE_LIST, but information on the net suggests so.

- Fetching IP_ADAPTER_ADDRESSES structures with fixed size area may not
  work when using IPv6. IPv6 generates ton of interface addresses for
  various addressing scopes. We need to adjust the area appropriately.

- PhysicalAddress field of IP_ADAPTER_ADDRESSES structure may have extra
  space. We cannot ignore PhysicalAddressLength field of
  IP_ADAPTER_ADDRESS structure.

- Flags field of IP_ADAPTER_ADDRESSES structure doesn't represent any of
  administratively and operatinal statuses. It just represents settings
  for windows network adapter.

- MTU field of IP_ADAPTER_ADDRESSES structure may have a uint32(-1) on
  64-bit platform. We need to convert the value to interger
  appropriately.

- IfType field of IP_ADAPTER_ADDRESSES structure is not a bit field.
  Bitwire operation for the field is completely wrong.

- OperStatus field of IP_ADAPTER_ADDRESSES structure is not a bit field.
  Bitwire operation for the field is completely wrong.

- IPv6IfIndex field of IP_ADAPTER_ADDRESSES structure is just a
  substitute for IfIndex field. We cannot prefer IPv6IfIndex to IfIndex.

- Windows XP, 2003 server and below don't set OnLinkPrefixLength field
  of IP_ADAPTER_UNICAST_ADDRESS structure. We cannot rely on the field
  on old kernels. We can use FirstPrefix field of IP_ADAPTER_ADDRESSES
  structure and IP_ADAPTER_PREFIX structure instead.

- Length field of IP_ADAPTER_{UNICAST,ANYCAST,MULTICAST}_ADDRESS
  sturecures doesn't represent an address prefix length. It just
  represents a socket address length.

Change-Id: Icabdaf7bd1d41360a981d2dad0b830b02b584528
Reviewed-on: https://go-review.googlesource.com/17412Reviewed-by: 's avatarAlex Brainman <alex.brainman@gmail.com>
parent c2ef0054
...@@ -10,7 +10,17 @@ import "syscall" ...@@ -10,7 +10,17 @@ import "syscall"
const GAA_FLAG_INCLUDE_PREFIX = 0x00000010 const GAA_FLAG_INCLUDE_PREFIX = 0x00000010
const IF_TYPE_SOFTWARE_LOOPBACK = 24 const (
IF_TYPE_OTHER = 1
IF_TYPE_ETHERNET_CSMACD = 6
IF_TYPE_ISO88025_TOKENRING = 9
IF_TYPE_PPP = 23
IF_TYPE_SOFTWARE_LOOPBACK = 24
IF_TYPE_ATM = 37
IF_TYPE_IEEE80211 = 71
IF_TYPE_TUNNEL = 131
IF_TYPE_IEEE1394 = 144
)
type SocketAddress struct { type SocketAddress struct {
Sockaddr *syscall.RawSockaddrAny Sockaddr *syscall.RawSockaddrAny
...@@ -94,7 +104,7 @@ const ( ...@@ -94,7 +104,7 @@ const (
IfOperStatusLowerLayerDown = 7 IfOperStatusLowerLayerDown = 7
) )
//sys GetAdaptersAddresses(family uint32, flags uint32, reserved uintptr, adapterAddresses *IpAdapterAddresses, sizeOfPointer *uint32) (errcode error) = iphlpapi.GetAdaptersAddresses //sys GetAdaptersAddresses(family uint32, flags uint32, reserved uintptr, adapterAddresses *IpAdapterAddresses, sizePointer *uint32) (errcode error) = iphlpapi.GetAdaptersAddresses
//sys GetComputerNameEx(nameformat uint32, buf *uint16, n *uint32) (err error) = GetComputerNameExW //sys GetComputerNameEx(nameformat uint32, buf *uint16, n *uint32) (err error) = GetComputerNameExW
//sys MoveFileEx(from *uint16, to *uint16, flags uint32) (err error) = MoveFileExW //sys MoveFileEx(from *uint16, to *uint16, flags uint32) (err error) = MoveFileExW
......
...@@ -16,8 +16,8 @@ var ( ...@@ -16,8 +16,8 @@ var (
procMoveFileExW = modkernel32.NewProc("MoveFileExW") procMoveFileExW = modkernel32.NewProc("MoveFileExW")
) )
func GetAdaptersAddresses(family uint32, flags uint32, reserved uintptr, adapterAddresses *IpAdapterAddresses, sizeOfPointer *uint32) (errcode error) { func GetAdaptersAddresses(family uint32, flags uint32, reserved uintptr, adapterAddresses *IpAdapterAddresses, sizePointer *uint32) (errcode error) {
r0, _, _ := syscall.Syscall6(procGetAdaptersAddresses.Addr(), 5, uintptr(family), uintptr(flags), uintptr(reserved), uintptr(unsafe.Pointer(adapterAddresses)), uintptr(unsafe.Pointer(sizeOfPointer)), 0) r0, _, _ := syscall.Syscall6(procGetAdaptersAddresses.Addr(), 5, uintptr(family), uintptr(flags), uintptr(reserved), uintptr(unsafe.Pointer(adapterAddresses)), uintptr(unsafe.Pointer(sizePointer)), 0)
if r0 != 0 { if r0 != 0 {
errcode = syscall.Errno(r0) errcode = syscall.Errno(r0)
} }
......
...@@ -188,18 +188,39 @@ func testAddrs(t *testing.T, ifat []Addr) (naf4, naf6 int) { ...@@ -188,18 +188,39 @@ func testAddrs(t *testing.T, ifat []Addr) (naf4, naf6 int) {
prefixLen, maxPrefixLen := ifa.Mask.Size() prefixLen, maxPrefixLen := ifa.Mask.Size()
if ifa.IP.To4() != nil { if ifa.IP.To4() != nil {
if 0 >= prefixLen || prefixLen > 8*IPv4len || maxPrefixLen != 8*IPv4len { if 0 >= prefixLen || prefixLen > 8*IPv4len || maxPrefixLen != 8*IPv4len {
t.Errorf("unexpected prefix length: %v/%v", prefixLen, maxPrefixLen) t.Errorf("unexpected prefix length: %d/%d", prefixLen, maxPrefixLen)
continue
}
if ifa.IP.IsLoopback() && (prefixLen != 8 && prefixLen != 8*IPv4len) { // see RFC 1122
t.Errorf("unexpected prefix length for IPv4 loopback: %d/%d", prefixLen, maxPrefixLen)
continue continue
} }
naf4++ naf4++
} else if ifa.IP.To16() != nil { }
if ifa.IP.To16() != nil && ifa.IP.To4() == nil {
if 0 >= prefixLen || prefixLen > 8*IPv6len || maxPrefixLen != 8*IPv6len { if 0 >= prefixLen || prefixLen > 8*IPv6len || maxPrefixLen != 8*IPv6len {
t.Errorf("unexpected prefix length: %v/%v", prefixLen, maxPrefixLen) t.Errorf("unexpected prefix length: %d/%d", prefixLen, maxPrefixLen)
continue
}
if ifa.IP.IsLoopback() && prefixLen != 8*IPv6len { // see RFC 4291
t.Errorf("unexpected prefix length for IPv6 loopback: %d/%d", prefixLen, maxPrefixLen)
continue continue
} }
naf6++ naf6++
} }
t.Logf("interface address %q", ifa.String()) t.Logf("interface address %q", ifa.String())
case *IPAddr:
if ifa == nil || ifa.IP == nil || ifa.IP.IsUnspecified() || ifa.IP.IsMulticast() {
t.Errorf("unexpected value: %+v", ifa)
continue
}
if ifa.IP.To4() != nil {
naf4++
}
if ifa.IP.To16() != nil && ifa.IP.To4() == nil {
naf6++
}
t.Logf("interface address %s", ifa.String())
default: default:
t.Errorf("unexpected type: %T", ifa) t.Errorf("unexpected type: %T", ifa)
} }
...@@ -217,7 +238,8 @@ func testMulticastAddrs(t *testing.T, ifmat []Addr) (nmaf4, nmaf6 int) { ...@@ -217,7 +238,8 @@ func testMulticastAddrs(t *testing.T, ifmat []Addr) (nmaf4, nmaf6 int) {
} }
if ifma.IP.To4() != nil { if ifma.IP.To4() != nil {
nmaf4++ nmaf4++
} else if ifma.IP.To16() != nil { }
if ifma.IP.To16() != nil && ifma.IP.To4() == nil {
nmaf6++ nmaf6++
} }
t.Logf("joined group address %q", ifma.String()) t.Logf("joined group address %q", ifma.String())
......
This diff is collapsed.
// Copyright 2015 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package net
import (
"bytes"
"internal/syscall/windows"
"sort"
"testing"
)
func TestWindowsInterfaces(t *testing.T) {
aas, err := adapterAddresses()
if err != nil {
t.Fatal(err)
}
ift, err := Interfaces()
if err != nil {
t.Fatal(err)
}
for i, ifi := range ift {
aa := aas[i]
if len(ifi.HardwareAddr) != int(aa.PhysicalAddressLength) {
t.Errorf("got %d; want %d", len(ifi.HardwareAddr), aa.PhysicalAddressLength)
}
if ifi.MTU > 0x7fffffff {
t.Errorf("%s: got %d; want less than or equal to 1<<31 - 1", ifi.Name, ifi.MTU)
}
if ifi.Flags&FlagUp != 0 && aa.OperStatus != windows.IfOperStatusUp {
t.Errorf("%s: got %v; should not include FlagUp", ifi.Name, ifi.Flags)
}
if ifi.Flags&FlagLoopback != 0 && aa.IfType != windows.IF_TYPE_SOFTWARE_LOOPBACK {
t.Errorf("%s: got %v; should not include FlagLoopback", ifi.Name, ifi.Flags)
}
if _, _, err := addrPrefixTable(aa); err != nil {
t.Errorf("%s: %v", ifi.Name, err)
}
}
}
type byAddrLen []IPNet
func (ps byAddrLen) Len() int { return len(ps) }
func (ps byAddrLen) Less(i, j int) bool {
if n := bytes.Compare(ps[i].IP, ps[j].IP); n != 0 {
return n < 0
}
if n := bytes.Compare(ps[i].Mask, ps[j].Mask); n != 0 {
return n < 0
}
return false
}
func (ps byAddrLen) Swap(i, j int) { ps[i], ps[j] = ps[j], ps[i] }
var windowsAddrPrefixLenTests = []struct {
pfxs []IPNet
ip IP
out int
}{
{
[]IPNet{
{IP: IPv4(172, 16, 0, 0), Mask: IPv4Mask(255, 255, 0, 0)},
{IP: IPv4(192, 168, 0, 0), Mask: IPv4Mask(255, 255, 255, 0)},
{IP: IPv4(192, 168, 0, 0), Mask: IPv4Mask(255, 255, 255, 128)},
{IP: IPv4(192, 168, 0, 0), Mask: IPv4Mask(255, 255, 255, 192)},
},
IPv4(192, 168, 0, 1),
26,
},
{
[]IPNet{
{IP: ParseIP("2001:db8::"), Mask: IPMask(ParseIP("ffff:ffff:ffff:ffff:ffff:ffff:ffff:fff0"))},
{IP: ParseIP("2001:db8::"), Mask: IPMask(ParseIP("ffff:ffff:ffff:ffff:ffff:ffff:ffff:fff8"))},
{IP: ParseIP("2001:db8::"), Mask: IPMask(ParseIP("ffff:ffff:ffff:ffff:ffff:ffff:ffff:fffc"))},
},
ParseIP("2001:db8::1"),
126,
},
// Fallback cases. It may happen on Windows XP or 2003 server.
{
[]IPNet{
{IP: IPv4(127, 0, 0, 0).To4(), Mask: IPv4Mask(255, 0, 0, 0)},
{IP: IPv4(10, 0, 0, 0).To4(), Mask: IPv4Mask(255, 0, 0, 0)},
{IP: IPv4(172, 16, 0, 0).To4(), Mask: IPv4Mask(255, 255, 0, 0)},
{IP: IPv4(192, 168, 255, 0), Mask: IPv4Mask(255, 255, 255, 0)},
{IP: IPv4zero, Mask: IPv4Mask(0, 0, 0, 0)},
},
IPv4(192, 168, 0, 1),
8 * IPv4len,
},
{
nil,
IPv4(192, 168, 0, 1),
8 * IPv4len,
},
{
[]IPNet{
{IP: IPv6loopback, Mask: IPMask(ParseIP("ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff"))},
{IP: ParseIP("2001:db8:1::"), Mask: IPMask(ParseIP("ffff:ffff:ffff:ffff:ffff:ffff:ffff:fff0"))},
{IP: ParseIP("2001:db8:2::"), Mask: IPMask(ParseIP("ffff:ffff:ffff:ffff:ffff:ffff:ffff:fff8"))},
{IP: ParseIP("2001:db8:3::"), Mask: IPMask(ParseIP("ffff:ffff:ffff:ffff:ffff:ffff:ffff:fffc"))},
{IP: IPv6unspecified, Mask: IPMask(ParseIP("::"))},
},
ParseIP("2001:db8::1"),
8 * IPv6len,
},
{
nil,
ParseIP("2001:db8::1"),
8 * IPv6len,
},
}
func TestWindowsAddrPrefixLen(t *testing.T) {
for i, tt := range windowsAddrPrefixLenTests {
sort.Sort(byAddrLen(tt.pfxs))
l := addrPrefixLen(tt.pfxs, tt.ip)
if l != tt.out {
t.Errorf("#%d: got %d; want %d", i, l, tt.out)
}
sort.Sort(sort.Reverse(byAddrLen(tt.pfxs)))
l = addrPrefixLen(tt.pfxs, tt.ip)
if l != tt.out {
t.Errorf("#%d: got %d; want %d", i, l, tt.out)
}
}
}
...@@ -315,13 +315,12 @@ func netshInterfaceIPv6ShowAddress(name string) ([]string, error) { ...@@ -315,13 +315,12 @@ func netshInterfaceIPv6ShowAddress(name string) ([]string, error) {
} }
// remove scope ID if present // remove scope ID if present
f = bytes.Split(f[1], []byte{'%'}) f = bytes.Split(f[1], []byte{'%'})
addrs = append(addrs, string(bytes.TrimSpace(f[0]))) addrs = append(addrs, string(bytes.ToLower(bytes.TrimSpace(f[0]))))
} }
return addrs, nil return addrs, nil
} }
func TestInterfaceAddrsWithNetsh(t *testing.T) { func TestInterfaceAddrsWithNetsh(t *testing.T) {
t.Skip("skipping test; see https://golang.org/issue/12811")
if isWindowsXP(t) { if isWindowsXP(t) {
t.Skip("Windows XP netsh command does not provide required functionality") t.Skip("Windows XP netsh command does not provide required functionality")
} }
...@@ -375,7 +374,6 @@ func TestInterfaceAddrsWithNetsh(t *testing.T) { ...@@ -375,7 +374,6 @@ func TestInterfaceAddrsWithNetsh(t *testing.T) {
} }
func TestInterfaceHardwareAddrWithGetmac(t *testing.T) { func TestInterfaceHardwareAddrWithGetmac(t *testing.T) {
t.Skip("skipping test; see https://golang.org/issue/12691")
if isWindowsXP(t) { if isWindowsXP(t) {
t.Skip("Windows XP does not have powershell command") t.Skip("Windows XP does not have powershell command")
} }
...@@ -386,7 +384,7 @@ func TestInterfaceHardwareAddrWithGetmac(t *testing.T) { ...@@ -386,7 +384,7 @@ func TestInterfaceHardwareAddrWithGetmac(t *testing.T) {
have := make([]string, 0) have := make([]string, 0)
for _, ifi := range ift { for _, ifi := range ift {
if ifi.Flags&FlagLoopback != 0 { if ifi.Flags&FlagLoopback != 0 {
// no MAC for loopback interfaces // no MAC address for loopback interfaces
continue continue
} }
have = append(have, ifi.Name+"="+ifi.HardwareAddr.String()) have = append(have, ifi.Name+"="+ifi.HardwareAddr.String())
...@@ -436,7 +434,7 @@ func TestInterfaceHardwareAddrWithGetmac(t *testing.T) { ...@@ -436,7 +434,7 @@ func TestInterfaceHardwareAddrWithGetmac(t *testing.T) {
if len(f) != 2 { if len(f) != 2 {
t.Fatal("unexpected \"Physical Address\" line: %q", line) t.Fatal("unexpected \"Physical Address\" line: %q", line)
} }
addr := string(bytes.TrimSpace(f[1])) addr := string(bytes.ToLower(bytes.TrimSpace(f[1])))
if addr == "" { if addr == "" {
t.Fatal("empty address on \"Physical Address\" line: %q", line) t.Fatal("empty address on \"Physical Address\" line: %q", line)
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment