internal/nettest: make SupportsIPv6 return false on Darwin kernel version 12 or below

The kernel is used in OS X Mountain Lion or below, or iOS version 8 or
below.

Updates golang/go#17015.

Change-Id: I8a849dc2ab4e04535f893b776bf704079cc91977
Reviewed-on: https://go-review.googlesource.com/33250
Run-TryBot: Mikio Hara <mikioh.mikioh@gmail.com>
Reviewed-by: Ian Lance Taylor <iant@golang.org>
diff --git a/internal/nettest/helper_bsd.go b/internal/nettest/helper_bsd.go
index b2308a0..a6e433b 100644
--- a/internal/nettest/helper_bsd.go
+++ b/internal/nettest/helper_bsd.go
@@ -13,6 +13,23 @@
 	"syscall"
 )
 
+var darwinVersion int
+
+func init() {
+	if runtime.GOOS == "darwin" {
+		// See http://support.apple.com/kb/HT1633.
+		s, err := syscall.Sysctl("kern.osrelease")
+		if err != nil {
+			return
+		}
+		ss := strings.Split(s, ".")
+		if len(ss) == 0 {
+			return
+		}
+		darwinVersion, _ = strconv.Atoi(ss[0])
+	}
+}
+
 func supportsIPv6MulticastDeliveryOnLoopback() bool {
 	switch runtime.GOOS {
 	case "freebsd":
@@ -22,27 +39,15 @@
 		// packets correctly.
 		return false
 	case "darwin":
-		// See http://support.apple.com/kb/HT1633.
-		s, err := syscall.Sysctl("kern.osrelease")
-		if err != nil {
-			return false
-		}
-		ss := strings.Split(s, ".")
-		if len(ss) == 0 {
-			return false
-		}
-		// OS X 10.9 (Darwin 13) or above seems to do the
-		// right thing; preserving the packet header as it's
-		// needed for the checksum calcuration with pseudo
-		// header on loopback multicast delivery process.
-		// If not, you'll probably see what is the slow-acting
-		// kernel crash caused by lazy mbuf corruption.
-		// See ip6_mloopback in netinet6/ip6_output.c.
-		if mjver, err := strconv.Atoi(ss[0]); err != nil || mjver < 13 {
-			return false
-		}
-		return true
+		return !causesIPv6Crash()
 	default:
 		return true
 	}
 }
+
+func causesIPv6Crash() bool {
+	// We see some kernel crash when running IPv6 with IP-level
+	// options on Darwin kernel version 12 or below.
+	// See golang.org/issues/17015.
+	return darwinVersion < 13
+}
diff --git a/internal/nettest/helper_nobsd.go b/internal/nettest/helper_nobsd.go
index a42b807..bc7da5e 100644
--- a/internal/nettest/helper_nobsd.go
+++ b/internal/nettest/helper_nobsd.go
@@ -9,3 +9,7 @@
 func supportsIPv6MulticastDeliveryOnLoopback() bool {
 	return true
 }
+
+func causesIPv6Crash() bool {
+	return false
+}
diff --git a/internal/nettest/helper_stub.go b/internal/nettest/helper_stub.go
index 22d4935..ea61b6f 100644
--- a/internal/nettest/helper_stub.go
+++ b/internal/nettest/helper_stub.go
@@ -23,6 +23,10 @@
 	return false
 }
 
+func causesIPv6Crash() bool {
+	return false
+}
+
 func protocolNotSupported(err error) bool {
 	return false
 }
diff --git a/internal/nettest/helper_windows.go b/internal/nettest/helper_windows.go
index b0a6a30..3dcb727 100644
--- a/internal/nettest/helper_windows.go
+++ b/internal/nettest/helper_windows.go
@@ -36,3 +36,7 @@
 func supportsIPv6MulticastDeliveryOnLoopback() bool {
 	return true
 }
+
+func causesIPv6Crash() bool {
+	return false
+}
diff --git a/internal/nettest/stack.go b/internal/nettest/stack.go
index 86de277..5ab9530 100644
--- a/internal/nettest/stack.go
+++ b/internal/nettest/stack.go
@@ -21,6 +21,9 @@
 // SupportsIPv6 reports whether the platform supports IPv6 networking
 // functionality.
 func SupportsIPv6() bool {
+	if causesIPv6Crash() {
+		return false
+	}
 	ln, err := net.Listen("tcp6", "[::1]:0")
 	if err != nil {
 		return false