| 1 | diff -ruN a/pkg/services/forwarder/tcp.go b/pkg/services/forwarder/tcp.go |
| 2 | |
| 3 | |
| 4 | @@ -4,8 +4,10 @@ |
| 5 | "context" |
| 6 | "fmt" |
| 7 | "net" |
| 8 | + "net/netip" |
| 9 | "sync" |
| 10 | |
| 11 | + "github.com/containers/gvisor-tap-vsock/pkg/types" |
| 12 | "github.com/inetaf/tcpproxy" |
| 13 | log "github.com/sirupsen/logrus" |
| 14 | "gvisor.dev/gvisor/pkg/tcpip" |
| 15 | @@ -17,7 +19,7 @@ |
| 16 | |
| 17 | const linkLocalSubnet = "169.254.0.0/16" |
| 18 | |
| 19 | -func TCP(s *stack.Stack, nat map[tcpip.Address]tcpip.Address, natLock *sync.Mutex, ec2MetadataAccess bool) *tcp.Forwarder { |
| 20 | +func TCP(s *stack.Stack, nat map[tcpip.Address]tcpip.Address, natLock *sync.Mutex, ec2MetadataAccess bool, outbound types.Outbound) *tcp.Forwarder { |
| 21 | return tcp.NewForwarder(s, 0, 10, func(r *tcp.ForwarderRequest) { |
| 22 | localAddress := r.ID().LocalAddress |
| 23 | |
| 24 | @@ -31,6 +33,10 @@ |
| 25 | localAddress = replaced |
| 26 | } |
| 27 | natLock.Unlock() |
| 28 | + if dst, ok := netip.AddrFromSlice(localAddress.AsSlice()); !ok || !outbound.Allows("tcp", netip.AddrPortFrom(dst, r.ID().LocalPort)) { |
| 29 | + r.Complete(true) |
| 30 | + return |
| 31 | + } |
| 32 | outbound, err := net.Dial("tcp", net.JoinHostPort(localAddress.String(), fmt.Sprint(r.ID().LocalPort))) |
| 33 | if err != nil { |
| 34 | log.Tracef("net.Dial() = %v", err) |
| 35 | diff -ruN a/pkg/services/forwarder/udp.go b/pkg/services/forwarder/udp.go |
| 36 | |
| 37 | |
| 38 | @@ -2,9 +2,11 @@ |
| 39 | |
| 40 | import ( |
| 41 | "net" |
| 42 | + "net/netip" |
| 43 | "strconv" |
| 44 | "sync" |
| 45 | |
| 46 | + "github.com/containers/gvisor-tap-vsock/pkg/types" |
| 47 | log "github.com/sirupsen/logrus" |
| 48 | "gvisor.dev/gvisor/pkg/tcpip" |
| 49 | "gvisor.dev/gvisor/pkg/tcpip/adapters/gonet" |
| 50 | @@ -14,7 +16,7 @@ |
| 51 | "gvisor.dev/gvisor/pkg/waiter" |
| 52 | ) |
| 53 | |
| 54 | -func UDP(s *stack.Stack, nat map[tcpip.Address]tcpip.Address, natLock *sync.Mutex, ec2MetadataAccess bool) *udp.Forwarder { |
| 55 | +func UDP(s *stack.Stack, nat map[tcpip.Address]tcpip.Address, natLock *sync.Mutex, ec2MetadataAccess bool, outbound types.Outbound) *udp.Forwarder { |
| 56 | return udp.NewForwarder(s, func(r *udp.ForwarderRequest) { |
| 57 | localAddress := r.ID().LocalAddress |
| 58 | |
| 59 | @@ -27,6 +29,9 @@ |
| 60 | localAddress = replaced |
| 61 | } |
| 62 | natLock.Unlock() |
| 63 | + if dst, ok := netip.AddrFromSlice(localAddress.AsSlice()); !ok || !outbound.Allows("udp", netip.AddrPortFrom(dst, r.ID().LocalPort)) { |
| 64 | + return |
| 65 | + } |
| 66 | |
| 67 | var wq waiter.Queue |
| 68 | ep, tcpErr := r.CreateEndpoint(&wq) |
| 69 | diff -ruN a/pkg/types/configuration.go b/pkg/types/configuration.go |
| 70 | |
| 71 | |
| 72 | @@ -2,9 +2,19 @@ |
| 73 | |
| 74 | import ( |
| 75 | "net" |
| 76 | + "net/netip" |
| 77 | "regexp" |
| 78 | ) |
| 79 | |
| 80 | +// Outbound decides whether the guest may reach dst over network ("tcp" or |
| 81 | +// "udp"): see Configuration.Outbound. |
| 82 | +type Outbound func(network string, dst netip.AddrPort) bool |
| 83 | + |
| 84 | +// Allows reports whether o lets the guest reach dst: all, when nil. |
| 85 | +func (o Outbound) Allows(network string, dst netip.AddrPort) bool { |
| 86 | + return o == nil || o(network, dst) |
| 87 | +} |
| 88 | + |
| 89 | type Configuration struct { |
| 90 | // Print packets on stderr |
| 91 | Debug bool `yaml:"debug,omitempty"` |
| 92 | @@ -53,6 +63,12 @@ |
| 93 | |
| 94 | // EC2 Metadata Service Access |
| 95 | Ec2MetadataAccess bool `yaml:"ec2MetadataAccess,omitempty"` |
| 96 | + |
| 97 | + // Outbound, when set, decides whether the virtual network opens a |
| 98 | + // connection the guest asks for: network is "tcp" or "udp", dst the |
| 99 | + // destination once NAT applied. A refused TCP connection is reset, a |
| 100 | + // refused UDP datagram dropped. |
| 101 | + Outbound Outbound `yaml:"-"` |
| 102 | } |
| 103 | |
| 104 | type Protocol string |
| 105 | diff -ruN a/pkg/virtualnetwork/services.go b/pkg/virtualnetwork/services.go |
| 106 | |
| 107 | |
| 108 | @@ -24,9 +24,9 @@ |
| 109 | var natLock sync.Mutex |
| 110 | translation := parseNATTable(configuration) |
| 111 | |
| 112 | - tcpForwarder := forwarder.TCP(s, translation, &natLock, configuration.Ec2MetadataAccess) |
| 113 | + tcpForwarder := forwarder.TCP(s, translation, &natLock, configuration.Ec2MetadataAccess, configuration.Outbound) |
| 114 | s.SetTransportProtocolHandler(tcp.ProtocolNumber, tcpForwarder.HandlePacket) |
| 115 | - udpForwarder := forwarder.UDP(s, translation, &natLock, configuration.Ec2MetadataAccess) |
| 116 | + udpForwarder := forwarder.UDP(s, translation, &natLock, configuration.Ec2MetadataAccess, configuration.Outbound) |
| 117 | s.SetTransportProtocolHandler(udp.ProtocolNumber, udpForwarder.HandlePacket) |
| 118 | |
| 119 | dnsMux, err := dnsServer(configuration, s) |