Skip to content

Commit

Permalink
nettest: use RoutedInterface for probing network stack capability
Browse files Browse the repository at this point in the history
The ipv4/ipv6 support capability is done by explicitly listening on
loopback interface. However, it can lead to false positive, especially
for ipv6 case. For example, ipv6 can be enabled, but explicitly disable
for loopback interface (for security, policy ...).

This CL changes probeStack to use another approach, by looking for any
interface that can route IP traffic and in "UP" state. If there's one,
then the platform can do ipv4/ipv6 networking functionality.

Fixes golang/go#57386

Change-Id: If911bc223b52c5a4562d3f61b4ee1032bdbec47c
Reviewed-on: https://go-review.googlesource.com/c/net/+/458096
TryBot-Result: Gopher Robot <[email protected]>
Auto-Submit: Cuong Manh Le <[email protected]>
Reviewed-by: Benny Siegert <[email protected]>
Reviewed-by: Matt Layher <[email protected]>
Run-TryBot: Cuong Manh Le <[email protected]>
Reviewed-by: David Chase <[email protected]>
  • Loading branch information
cuonglm authored and gopherbot committed Dec 23, 2022
1 parent ad92d3d commit 2aa8215
Show file tree
Hide file tree
Showing 6 changed files with 18 additions and 20 deletions.
4 changes: 2 additions & 2 deletions ipv6/bpf_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ func TestBPF(t *testing.T) {
if runtime.GOOS != "linux" {
t.Skipf("not supported on %s", runtime.GOOS)
}
if !nettest.SupportsIPv6() {
t.Skip("ipv6 is not supported")
if _, err := nettest.RoutedInterface("ip6", net.FlagUp|net.FlagLoopback); err != nil {
t.Skip("ipv6 is not enabled for loopback interface")
}

l, err := net.ListenPacket("udp6", "[::1]:0")
Expand Down
7 changes: 3 additions & 4 deletions ipv6/readwrite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -223,10 +223,10 @@ func TestPacketConnConcurrentReadWriteUnicastUDP(t *testing.T) {
case "fuchsia", "hurd", "js", "nacl", "plan9", "windows":
t.Skipf("not supported on %s", runtime.GOOS)
}
if !nettest.SupportsIPv6() {
t.Skip("ipv6 is not supported")
ifi, err := nettest.RoutedInterface("ip6", net.FlagUp|net.FlagLoopback)
if err != nil {
t.Skip("ipv6 is not enabled for loopback interface")
}

c, err := nettest.NewLocalPacketListener("udp6")
if err != nil {
t.Fatal(err)
Expand All @@ -236,7 +236,6 @@ func TestPacketConnConcurrentReadWriteUnicastUDP(t *testing.T) {
defer p.Close()

dst := c.LocalAddr()
ifi, _ := nettest.RoutedInterface("ip6", net.FlagUp|net.FlagLoopback)
cf := ipv6.FlagTrafficClass | ipv6.FlagHopLimit | ipv6.FlagSrc | ipv6.FlagDst | ipv6.FlagInterface | ipv6.FlagPathMTU
wb := []byte("HELLO-R-U-THERE")

Expand Down
9 changes: 5 additions & 4 deletions ipv6/sockopt_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,9 @@ func TestConnInitiatorPathMTU(t *testing.T) {
case "fuchsia", "hurd", "js", "nacl", "plan9", "windows", "zos":
t.Skipf("not supported on %s", runtime.GOOS)
}
if !nettest.SupportsIPv6() {
t.Skip("ipv6 is not supported")

if _, err := nettest.RoutedInterface("ip6", net.FlagUp|net.FlagLoopback); err != nil {
t.Skip("ipv6 is not enabled for loopback interface")
}

ln, err := net.Listen("tcp6", "[::1]:0")
Expand Down Expand Up @@ -53,8 +54,8 @@ func TestConnResponderPathMTU(t *testing.T) {
case "fuchsia", "hurd", "js", "nacl", "plan9", "windows", "zos":
t.Skipf("not supported on %s", runtime.GOOS)
}
if !nettest.SupportsIPv6() {
t.Skip("ipv6 is not supported")
if _, err := nettest.RoutedInterface("ip6", net.FlagUp|net.FlagLoopback); err != nil {
t.Skip("ipv6 is not enabled for loopback interface")
}

ln, err := net.Listen("tcp6", "[::1]:0")
Expand Down
4 changes: 2 additions & 2 deletions ipv6/unicast_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@ func TestPacketConnReadWriteUnicastUDP(t *testing.T) {
case "fuchsia", "hurd", "js", "nacl", "plan9", "windows":
t.Skipf("not supported on %s", runtime.GOOS)
}
if !nettest.SupportsIPv6() {
t.Skip("ipv6 is not supported")
if _, err := nettest.RoutedInterface("ip6", net.FlagUp|net.FlagLoopback); err != nil {
t.Skip("ipv6 is not enabled for loopback interface")
}

c, err := nettest.NewLocalPacketListener("udp6")
Expand Down
8 changes: 4 additions & 4 deletions ipv6/unicastsockopt_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ func TestConnUnicastSocketOptions(t *testing.T) {
case "fuchsia", "hurd", "js", "nacl", "plan9", "windows":
t.Skipf("not supported on %s", runtime.GOOS)
}
if !nettest.SupportsIPv6() {
t.Skip("ipv6 is not supported")
if _, err := nettest.RoutedInterface("ip6", net.FlagUp|net.FlagLoopback); err != nil {
t.Skip("ipv6 is not enabled for loopback interface")
}

ln, err := net.Listen("tcp6", "[::1]:0")
Expand Down Expand Up @@ -64,8 +64,8 @@ func TestPacketConnUnicastSocketOptions(t *testing.T) {
case "fuchsia", "hurd", "js", "nacl", "plan9", "windows":
t.Skipf("not supported on %s", runtime.GOOS)
}
if !nettest.SupportsIPv6() {
t.Skip("ipv6 is not supported")
if _, err := nettest.RoutedInterface("ip6", net.FlagUp|net.FlagLoopback); err != nil {
t.Skip("ipv6 is not enabled for loopback interface")
}

ok := nettest.SupportsRawSocket()
Expand Down
6 changes: 2 additions & 4 deletions nettest/nettest.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,12 +34,10 @@ var (
)

func probeStack() {
if ln, err := net.Listen("tcp4", "127.0.0.1:0"); err == nil {
ln.Close()
if _, err := RoutedInterface("ip4", net.FlagUp); err == nil {
ipv4Enabled = true
}
if ln, err := net.Listen("tcp6", "[::1]:0"); err == nil {
ln.Close()
if _, err := RoutedInterface("ip6", net.FlagUp); err == nil {
ipv6Enabled = true
}
rawSocketSess = supportsRawSocket()
Expand Down

0 comments on commit 2aa8215

Please sign in to comment.