summaryrefslogtreecommitdiffhomepage
path: root/pkg/sentry/socket/netstack/stack.go
diff options
context:
space:
mode:
authorMikael Magnusson <mikma@users.sourceforge.net>2021-02-05 02:04:06 +0100
committerMikael Magnusson <mikma@users.sourceforge.net>2021-09-26 23:34:44 +0200
commite30c52d292d5f03883a83d0a6bec71335ab99342 (patch)
tree60d49c63deb8fa6ed4677428e49e7eacf9dc3225 /pkg/sentry/socket/netstack/stack.go
parentea43d9ffc0fd7bc1f42bada76006dbcf26f752af (diff)
Implement dummy network interfacesfeature/dummy-iface
Diffstat (limited to 'pkg/sentry/socket/netstack/stack.go')
-rw-r--r--pkg/sentry/socket/netstack/stack.go24
1 files changed, 24 insertions, 0 deletions
diff --git a/pkg/sentry/socket/netstack/stack.go b/pkg/sentry/socket/netstack/stack.go
index 3fceb4cd5..360b270c1 100644
--- a/pkg/sentry/socket/netstack/stack.go
+++ b/pkg/sentry/socket/netstack/stack.go
@@ -24,6 +24,7 @@ import (
"gvisor.dev/gvisor/pkg/syserr"
"gvisor.dev/gvisor/pkg/tcpip"
"gvisor.dev/gvisor/pkg/tcpip/header"
+ "gvisor.dev/gvisor/pkg/tcpip/link/dummy"
"gvisor.dev/gvisor/pkg/tcpip/network/ipv4"
"gvisor.dev/gvisor/pkg/tcpip/network/ipv6"
"gvisor.dev/gvisor/pkg/tcpip/stack"
@@ -71,6 +72,17 @@ func (s *Stack) Interfaces() map[int32]inet.Interface {
return is
}
+func (s *Stack) AddDummyInterface(name string) (int32, error) {
+ var idx tcpip.NICID = s.nextInterfaceIndex()
+
+ dummyEP := dummy.New()
+ if err := s.Stack.CreateNICWithOptions(idx, dummyEP, stack.NICOptions{Name: name}); err != nil {
+ return -1, syserr.TranslateNetstackError(err).ToError()
+ }
+
+ return int32(idx), nil
+}
+
// RemoveInterface implements inet.Stack.RemoveInterface.
func (s *Stack) RemoveInterface(idx int32) error {
nic := tcpip.NICID(idx)
@@ -146,6 +158,18 @@ func convertAddr(addr inet.InterfaceAddr) (tcpip.ProtocolAddress, error) {
return protocolAddress, nil
}
+func (s *Stack) nextInterfaceIndex() tcpip.NICID {
+ var maxIdx tcpip.NICID = 0
+
+ for id, _ := range s.Stack.NICInfo() {
+ if id > maxIdx {
+ maxIdx = id
+ }
+ }
+
+ return maxIdx + 1
+}
+
// AddInterfaceAddr implements inet.Stack.AddInterfaceAddr.
func (s *Stack) AddInterfaceAddr(idx int32, addr inet.InterfaceAddr) error {
protocolAddress, err := convertAddr(addr)