Commit b4bf0663 authored by Mikio Hara's avatar Mikio Hara

net: golang.org/x/net/route plumbing

This change makes use of new routing message APIs for BSD variants to
support FreeBSD 11 and newer versions of other BSDs.

Fixes #7849.
Fixes #14724.

Change-Id: I56c7886d6622cdeddd7cc29c8a8062dcc06216d5
Reviewed-on: https://go-review.googlesource.com/22451Reviewed-by: default avatarIan Lance Taylor <iant@golang.org>
Run-TryBot: Mikio Hara <mikioh.mikioh@gmail.com>
TryBot-Result: Gobot Gobot <gobot@golang.org>
parent 0bc14f57
...@@ -292,10 +292,13 @@ var pkgDeps = map[string][]string{ ...@@ -292,10 +292,13 @@ var pkgDeps = map[string][]string{
// Basic networking. // Basic networking.
// Because net must be used by any package that wants to // Because net must be used by any package that wants to
// do networking portably, it must have a small dependency set: just L0+basic os. // do networking portably, it must have a small dependency set: just L0+basic os.
"net": {"L0", "CGO", "net": {
"L0", "CGO",
"context", "math/rand", "os", "sort", "syscall", "time", "context", "math/rand", "os", "sort", "syscall", "time",
"internal/nettrace", "internal/nettrace",
"internal/syscall/windows", "internal/singleflight", "internal/race"}, "internal/syscall/windows", "internal/singleflight", "internal/race",
"golang.org/x/net/route",
},
// NET enables use of basic network-related packages. // NET enables use of basic network-related packages.
"NET": { "NET": {
......
...@@ -7,74 +7,58 @@ ...@@ -7,74 +7,58 @@
package net package net
import ( import (
"os"
"syscall" "syscall"
"unsafe"
"golang.org/x/net/route"
) )
// If the ifindex is zero, interfaceTable returns mappings of all // If the ifindex is zero, interfaceTable returns mappings of all
// network interfaces. Otherwise it returns a mapping of a specific // network interfaces. Otherwise it returns a mapping of a specific
// interface. // interface.
func interfaceTable(ifindex int) ([]Interface, error) { func interfaceTable(ifindex int) ([]Interface, error) {
tab, err := syscall.RouteRIB(syscall.NET_RT_IFLIST, ifindex) msgs, err := interfaceMessages(ifindex)
if err != nil {
return nil, os.NewSyscallError("routerib", err)
}
msgs, err := syscall.ParseRoutingMessage(tab)
if err != nil { if err != nil {
return nil, os.NewSyscallError("parseroutingmessage", err) return nil, err
} }
return parseInterfaceTable(ifindex, msgs) return parseInterfaceTable(ifindex, msgs)
} }
func parseInterfaceTable(ifindex int, msgs []syscall.RoutingMessage) ([]Interface, error) { func parseInterfaceTable(ifindex int, msgs []route.Message) ([]Interface, error) {
var ift []Interface n := len(msgs)
loop: if ifindex != 0 {
n = 1
}
ift := make([]Interface, n)
n = 0
for _, m := range msgs { for _, m := range msgs {
switch m := m.(type) { switch m := m.(type) {
case *syscall.InterfaceMessage: case *route.InterfaceMessage:
if ifindex == 0 || ifindex == int(m.Header.Index) { if ifindex != 0 && ifindex != m.Index {
ifi, err := newLink(m) continue
if err != nil { }
return nil, err ift[n].Index = m.Index
} ift[n].Name = m.Name
ift = append(ift, *ifi) ift[n].Flags = linkFlags(m.Flags)
if ifindex == int(m.Header.Index) { if sa, ok := m.Addrs[syscall.RTAX_IFP].(*route.LinkAddr); ok && len(sa.Addr) > 0 {
break loop ift[n].HardwareAddr = make([]byte, len(sa.Addr))
copy(ift[n].HardwareAddr, sa.Addr)
}
for _, sys := range m.Sys() {
if imx, ok := sys.(*route.InterfaceMetrics); ok {
ift[n].MTU = imx.MTU
break
} }
} }
n++
if ifindex == m.Index {
return ift[:n], nil
}
} }
} }
return ift, nil return ift[:n], nil
}
func newLink(m *syscall.InterfaceMessage) (*Interface, error) {
sas, err := syscall.ParseRoutingSockaddr(m)
if err != nil {
return nil, os.NewSyscallError("parseroutingsockaddr", err)
}
ifi := &Interface{Index: int(m.Header.Index), Flags: linkFlags(m.Header.Flags)}
sa, _ := sas[syscall.RTAX_IFP].(*syscall.SockaddrDatalink)
if sa != nil {
// NOTE: SockaddrDatalink.Data is minimum work area,
// can be larger.
m.Data = m.Data[unsafe.Offsetof(sa.Data):]
var name [syscall.IFNAMSIZ]byte
for i := 0; i < int(sa.Nlen); i++ {
name[i] = m.Data[i]
}
ifi.Name = string(name[:sa.Nlen])
ifi.MTU = int(m.Header.Data.Mtu)
addr := make([]byte, sa.Alen)
for i := 0; i < int(sa.Alen); i++ {
addr[i] = m.Data[int(sa.Nlen)+i]
}
ifi.HardwareAddr = addr[:sa.Alen]
}
return ifi, nil
} }
func linkFlags(rawFlags int32) Flags { func linkFlags(rawFlags int) Flags {
var f Flags var f Flags
if rawFlags&syscall.IFF_UP != 0 { if rawFlags&syscall.IFF_UP != 0 {
f |= FlagUp f |= FlagUp
...@@ -102,74 +86,37 @@ func interfaceAddrTable(ifi *Interface) ([]Addr, error) { ...@@ -102,74 +86,37 @@ func interfaceAddrTable(ifi *Interface) ([]Addr, error) {
if ifi != nil { if ifi != nil {
index = ifi.Index index = ifi.Index
} }
tab, err := syscall.RouteRIB(syscall.NET_RT_IFLIST, index) msgs, err := interfaceMessages(index)
if err != nil {
return nil, os.NewSyscallError("routerib", err)
}
msgs, err := syscall.ParseRoutingMessage(tab)
if err != nil { if err != nil {
return nil, os.NewSyscallError("parseroutingmessage", err) return nil, err
} }
var ift []Interface ifat := make([]Addr, 0, len(msgs))
if index == 0 {
ift, err = parseInterfaceTable(index, msgs)
if err != nil {
return nil, err
}
}
var ifat []Addr
for _, m := range msgs { for _, m := range msgs {
switch m := m.(type) { switch m := m.(type) {
case *syscall.InterfaceAddrMessage: case *route.InterfaceAddrMessage:
if index == 0 || index == int(m.Header.Index) { if index != 0 && index != m.Index {
if index == 0 { continue
var err error }
ifi, err = interfaceByIndex(ift, int(m.Header.Index)) var mask IPMask
if err != nil { switch sa := m.Addrs[syscall.RTAX_NETMASK].(type) {
return nil, err case *route.Inet4Addr:
} mask = IPv4Mask(sa.IP[0], sa.IP[1], sa.IP[2], sa.IP[3])
} case *route.Inet6Addr:
ifa, err := newAddr(ifi, m) mask = make(IPMask, IPv6len)
if err != nil { copy(mask, sa.IP[:])
return nil, err }
} var ip IP
if ifa != nil { switch sa := m.Addrs[syscall.RTAX_IFA].(type) {
ifat = append(ifat, ifa) case *route.Inet4Addr:
} ip = IPv4(sa.IP[0], sa.IP[1], sa.IP[2], sa.IP[3])
case *route.Inet6Addr:
ip = make(IP, IPv6len)
copy(ip, sa.IP[:])
}
if ip != nil && mask != nil { // NetBSD may contain route.LinkAddr
ifat = append(ifat, &IPNet{IP: ip, Mask: mask})
} }
} }
} }
return ifat, nil return ifat, nil
} }
func newAddr(ifi *Interface, m *syscall.InterfaceAddrMessage) (*IPNet, error) {
sas, err := syscall.ParseRoutingSockaddr(m)
if err != nil {
return nil, os.NewSyscallError("parseroutingsockaddr", err)
}
ifa := &IPNet{}
switch sa := sas[syscall.RTAX_NETMASK].(type) {
case *syscall.SockaddrInet4:
ifa.Mask = IPv4Mask(sa.Addr[0], sa.Addr[1], sa.Addr[2], sa.Addr[3])
case *syscall.SockaddrInet6:
ifa.Mask = make(IPMask, IPv6len)
copy(ifa.Mask, sa.Addr[:])
}
switch sa := sas[syscall.RTAX_IFA].(type) {
case *syscall.SockaddrInet4:
ifa.IP = IPv4(sa.Addr[0], sa.Addr[1], sa.Addr[2], sa.Addr[3])
case *syscall.SockaddrInet6:
ifa.IP = make(IP, IPv6len)
copy(ifa.IP, sa.Addr[:])
// NOTE: KAME based IPv6 protocol stack usually embeds
// the interface index in the interface-local or
// link-local address as the kernel-internal form.
if ifa.IP.IsLinkLocalUnicast() {
ifa.IP[2], ifa.IP[3] = 0, 0
}
}
if ifa.IP == nil || ifa.Mask == nil {
return nil, nil // Sockaddrs contain syscall.SockaddrDatalink on NetBSD
}
return ifa, nil
}
...@@ -2,8 +2,24 @@ ...@@ -2,8 +2,24 @@
// Use of this source code is governed by a BSD-style // Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file. // license that can be found in the LICENSE file.
// +build dragonfly netbsd openbsd
package net package net
import (
"syscall"
"golang.org/x/net/route"
)
func interfaceMessages(ifindex int) ([]route.Message, error) {
rib, err := route.FetchRIB(syscall.AF_UNSPEC, syscall.NET_RT_IFLIST, ifindex)
if err != nil {
return nil, err
}
return route.ParseRIB(syscall.NET_RT_IFLIST, rib)
}
// interfaceMulticastAddrTable returns addresses for a specific // interfaceMulticastAddrTable returns addresses for a specific
// interface. // interface.
func interfaceMulticastAddrTable(ifi *Interface) ([]Addr, error) { func interfaceMulticastAddrTable(ifi *Interface) ([]Addr, error) {
......
...@@ -5,58 +5,49 @@ ...@@ -5,58 +5,49 @@
package net package net
import ( import (
"os"
"syscall" "syscall"
"golang.org/x/net/route"
) )
func interfaceMessages(ifindex int) ([]route.Message, error) {
rib, err := route.FetchRIB(syscall.AF_UNSPEC, syscall.NET_RT_IFLIST, ifindex)
if err != nil {
return nil, err
}
return route.ParseRIB(syscall.NET_RT_IFLIST, rib)
}
// interfaceMulticastAddrTable returns addresses for a specific // interfaceMulticastAddrTable returns addresses for a specific
// interface. // interface.
func interfaceMulticastAddrTable(ifi *Interface) ([]Addr, error) { func interfaceMulticastAddrTable(ifi *Interface) ([]Addr, error) {
tab, err := syscall.RouteRIB(syscall.NET_RT_IFLIST2, ifi.Index) rib, err := route.FetchRIB(syscall.AF_UNSPEC, syscall.NET_RT_IFLIST2, ifi.Index)
if err != nil { if err != nil {
return nil, os.NewSyscallError("routerib", err) return nil, err
} }
msgs, err := syscall.ParseRoutingMessage(tab) msgs, err := route.ParseRIB(syscall.NET_RT_IFLIST2, rib)
if err != nil { if err != nil {
return nil, os.NewSyscallError("parseroutingmessage", err) return nil, err
} }
var ifmat []Addr ifmat := make([]Addr, 0, len(msgs))
for _, m := range msgs { for _, m := range msgs {
switch m := m.(type) { switch m := m.(type) {
case *syscall.InterfaceMulticastAddrMessage: case *route.InterfaceMulticastAddrMessage:
if ifi.Index == int(m.Header.Index) { if ifi.Index != m.Index {
ifma, err := newMulticastAddr(ifi, m) continue
if err != nil { }
return nil, err var ip IP
} switch sa := m.Addrs[syscall.RTAX_IFA].(type) {
if ifma != nil { case *route.Inet4Addr:
ifmat = append(ifmat, ifma) ip = IPv4(sa.IP[0], sa.IP[1], sa.IP[2], sa.IP[3])
} case *route.Inet6Addr:
ip = make(IP, IPv6len)
copy(ip, sa.IP[:])
}
if ip != nil {
ifmat = append(ifmat, &IPAddr{IP: ip})
} }
} }
} }
return ifmat, nil return ifmat, nil
} }
func newMulticastAddr(ifi *Interface, m *syscall.InterfaceMulticastAddrMessage) (*IPAddr, error) {
sas, err := syscall.ParseRoutingSockaddr(m)
if err != nil {
return nil, os.NewSyscallError("parseroutingsockaddr", err)
}
switch sa := sas[syscall.RTAX_IFA].(type) {
case *syscall.SockaddrInet4:
return &IPAddr{IP: IPv4(sa.Addr[0], sa.Addr[1], sa.Addr[2], sa.Addr[3])}, nil
case *syscall.SockaddrInet6:
ifma := IPAddr{IP: make(IP, IPv6len)}
copy(ifma.IP, sa.Addr[:])
// NOTE: KAME based IPv6 protocol stack usually embeds
// the interface index in the interface-local or
// link-local address as the kernel-internal form.
if ifma.IP.IsInterfaceLocalMulticast() || ifma.IP.IsLinkLocalMulticast() {
ifma.IP[2], ifma.IP[3] = 0, 0
}
return &ifma, nil
default:
return nil, nil
}
}
...@@ -5,58 +5,54 @@ ...@@ -5,58 +5,54 @@
package net package net
import ( import (
"os"
"syscall" "syscall"
"golang.org/x/net/route"
) )
func interfaceMessages(ifindex int) ([]route.Message, error) {
typ := route.RIBType(syscall.NET_RT_IFLISTL)
rib, err := route.FetchRIB(syscall.AF_UNSPEC, typ, ifindex)
if err != nil {
typ = route.RIBType(syscall.NET_RT_IFLIST)
rib, err = route.FetchRIB(syscall.AF_UNSPEC, typ, ifindex)
}
if err != nil {
return nil, err
}
return route.ParseRIB(typ, rib)
}
// interfaceMulticastAddrTable returns addresses for a specific // interfaceMulticastAddrTable returns addresses for a specific
// interface. // interface.
func interfaceMulticastAddrTable(ifi *Interface) ([]Addr, error) { func interfaceMulticastAddrTable(ifi *Interface) ([]Addr, error) {
tab, err := syscall.RouteRIB(syscall.NET_RT_IFMALIST, ifi.Index) rib, err := route.FetchRIB(syscall.AF_UNSPEC, syscall.NET_RT_IFMALIST, ifi.Index)
if err != nil { if err != nil {
return nil, os.NewSyscallError("routerib", err) return nil, err
} }
msgs, err := syscall.ParseRoutingMessage(tab) msgs, err := route.ParseRIB(syscall.NET_RT_IFMALIST, rib)
if err != nil { if err != nil {
return nil, os.NewSyscallError("parseroutingmessage", err) return nil, err
} }
var ifmat []Addr ifmat := make([]Addr, 0, len(msgs))
for _, m := range msgs { for _, m := range msgs {
switch m := m.(type) { switch m := m.(type) {
case *syscall.InterfaceMulticastAddrMessage: case *route.InterfaceMulticastAddrMessage:
if ifi.Index == int(m.Header.Index) { if ifi.Index != m.Index {
ifma, err := newMulticastAddr(ifi, m) continue
if err != nil { }
return nil, err var ip IP
} switch sa := m.Addrs[syscall.RTAX_IFA].(type) {
if ifma != nil { case *route.Inet4Addr:
ifmat = append(ifmat, ifma) ip = IPv4(sa.IP[0], sa.IP[1], sa.IP[2], sa.IP[3])
} case *route.Inet6Addr:
ip = make(IP, IPv6len)
copy(ip, sa.IP[:])
}
if ip != nil {
ifmat = append(ifmat, &IPAddr{IP: ip})
} }
} }
} }
return ifmat, nil return ifmat, nil
} }
func newMulticastAddr(ifi *Interface, m *syscall.InterfaceMulticastAddrMessage) (*IPAddr, error) {
sas, err := syscall.ParseRoutingSockaddr(m)
if err != nil {
return nil, os.NewSyscallError("parseroutingsockaddr", err)
}
switch sa := sas[syscall.RTAX_IFA].(type) {
case *syscall.SockaddrInet4:
return &IPAddr{IP: IPv4(sa.Addr[0], sa.Addr[1], sa.Addr[2], sa.Addr[3])}, nil
case *syscall.SockaddrInet6:
ifma := IPAddr{IP: make(IP, IPv6len)}
copy(ifma.IP, sa.Addr[:])
// NOTE: KAME based IPv6 protocol stack usually embeds
// the interface index in the interface-local or
// link-local address as the kernel-internal form.
if ifma.IP.IsInterfaceLocalMulticast() || ifma.IP.IsLinkLocalMulticast() {
ifma.IP[2], ifma.IP[3] = 0, 0
}
return &ifma, nil
default:
return nil, nil
}
}
// Copyright 2011 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
// interfaceMulticastAddrTable returns addresses for a specific
// interface.
func interfaceMulticastAddrTable(ifi *Interface) ([]Addr, error) {
// TODO(mikio): Implement this like other platforms.
return nil, nil
}
// Copyright 2011 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
// interfaceMulticastAddrTable returns addresses for a specific
// interface.
func interfaceMulticastAddrTable(ifi *Interface) ([]Addr, error) {
// TODO(mikio): Implement this like other platforms.
return nil, nil
}
...@@ -6,7 +6,6 @@ package net ...@@ -6,7 +6,6 @@ package net
import ( import (
"fmt" "fmt"
"internal/testenv"
"reflect" "reflect"
"runtime" "runtime"
"testing" "testing"
...@@ -50,11 +49,6 @@ func ipv6LinkLocalUnicastAddr(ifi *Interface) string { ...@@ -50,11 +49,6 @@ func ipv6LinkLocalUnicastAddr(ifi *Interface) string {
} }
func TestInterfaces(t *testing.T) { func TestInterfaces(t *testing.T) {
if runtime.GOOS == "freebsd" && runtime.GOARCH == "arm" {
// 100% flaky on FreeBSD 11-CURRENT and above.
testenv.SkipFlaky(t, 7849)
}
ift, err := Interfaces() ift, err := Interfaces()
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
...@@ -79,11 +73,6 @@ func TestInterfaces(t *testing.T) { ...@@ -79,11 +73,6 @@ func TestInterfaces(t *testing.T) {
} }
func TestInterfaceAddrs(t *testing.T) { func TestInterfaceAddrs(t *testing.T) {
if runtime.GOOS == "freebsd" && runtime.GOARCH == "arm" {
// 100% flaky on FreeBSD 11-CURRENT and above.
testenv.SkipFlaky(t, 7849)
}
ift, err := Interfaces() ift, err := Interfaces()
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
...@@ -103,11 +92,6 @@ func TestInterfaceAddrs(t *testing.T) { ...@@ -103,11 +92,6 @@ func TestInterfaceAddrs(t *testing.T) {
} }
func TestInterfaceUnicastAddrs(t *testing.T) { func TestInterfaceUnicastAddrs(t *testing.T) {
if runtime.GOOS == "freebsd" && runtime.GOARCH == "arm" {
// 100% flaky on FreeBSD 11-CURRENT and above.
testenv.SkipFlaky(t, 7849)
}
ift, err := Interfaces() ift, err := Interfaces()
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
...@@ -135,11 +119,6 @@ func TestInterfaceUnicastAddrs(t *testing.T) { ...@@ -135,11 +119,6 @@ func TestInterfaceUnicastAddrs(t *testing.T) {
} }
func TestInterfaceMulticastAddrs(t *testing.T) { func TestInterfaceMulticastAddrs(t *testing.T) {
if runtime.GOOS == "freebsd" && runtime.GOARCH == "arm" {
// 100% flaky on FreeBSD 11-CURRENT and above.
testenv.SkipFlaky(t, 7849)
}
ift, err := Interfaces() ift, err := Interfaces()
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
......
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