From 9c5cd1cfcaadfc751bace54caaa96174fb384553 Mon Sep 17 00:00:00 2001 From: Kevin Krakauer Date: Fri, 2 Dec 2022 15:54:05 -0800 Subject: [PATCH] netstack: fallback to IPv4 when checking hostinet interface features This enables hostinet to start on both IPv4-only and IPv6-only hosts. Note that hostinet doesn't have a way to prevent applications inside gVisor from trying to send, for example, IPv6 packets on an IPv4-only host. The result of trying to do so is environment-dependent, but will likely fail during calls to connect(), send(), and the like. Fixes #8253. PiperOrigin-RevId: 492579750 --- pkg/sentry/socket/hostinet/stack_unsafe.go | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/pkg/sentry/socket/hostinet/stack_unsafe.go b/pkg/sentry/socket/hostinet/stack_unsafe.go index 066c2d0ac..e16aef32b 100644 --- a/pkg/sentry/socket/hostinet/stack_unsafe.go +++ b/pkg/sentry/socket/hostinet/stack_unsafe.go @@ -25,11 +25,12 @@ import ( ) func queryInterfaceFeatures(interfaces map[int32]*inet.Interface) error { - fd, err := unix.Socket(unix.AF_INET6, unix.SOCK_STREAM, 0) + fd, err := queryFD() if err != nil { return err } defer unix.Close(fd) + for idx, nic := range interfaces { var ifr linux.IFReq copy(ifr.IFName[:], nic.Name) @@ -85,3 +86,16 @@ func queryInterfaceFeatures(interfaces map[int32]*inet.Interface) error { } return nil } + +func queryFD() (int, error) { + // Try both AF_INET and AF_INET6 in case only one is supported. + var fd int + var err error + for _, family := range []int{unix.AF_INET6, unix.AF_INET} { + fd, err = unix.Socket(family, unix.SOCK_STREAM, 0) + if err == nil { + return fd, err + } + } + return fd, err +}