|
| 1 | +package forwarder |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "io" |
| 6 | + "net" |
| 7 | + "testing" |
| 8 | + "time" |
| 9 | + |
| 10 | + "github.com/containers/gvisor-tap-vsock/pkg/types" |
| 11 | + "github.com/onsi/ginkgo/v2" |
| 12 | + "github.com/onsi/gomega" |
| 13 | + "gvisor.dev/gvisor/pkg/tcpip" |
| 14 | + "gvisor.dev/gvisor/pkg/tcpip/adapters/gonet" |
| 15 | + "gvisor.dev/gvisor/pkg/tcpip/header" |
| 16 | + "gvisor.dev/gvisor/pkg/tcpip/link/loopback" |
| 17 | + "gvisor.dev/gvisor/pkg/tcpip/network/ipv4" |
| 18 | + "gvisor.dev/gvisor/pkg/tcpip/stack" |
| 19 | + "gvisor.dev/gvisor/pkg/tcpip/transport/tcp" |
| 20 | + "gvisor.dev/gvisor/pkg/tcpip/transport/udp" |
| 21 | +) |
| 22 | + |
| 23 | +func TestSuite(t *testing.T) { |
| 24 | + gomega.RegisterFailHandler(ginkgo.Fail) |
| 25 | + ginkgo.RunSpecs(t, "forwarder suite") |
| 26 | +} |
| 27 | + |
| 28 | +func hostIP() net.IP { |
| 29 | + addrs, err := net.InterfaceAddrs() |
| 30 | + if err != nil { |
| 31 | + return nil |
| 32 | + } |
| 33 | + for _, addr := range addrs { |
| 34 | + if ipnet, ok := addr.(*net.IPNet); ok && !ipnet.IP.IsLoopback() && ipnet.IP.To4() != nil { |
| 35 | + return ipnet.IP |
| 36 | + } |
| 37 | + } |
| 38 | + return nil |
| 39 | +} |
| 40 | + |
| 41 | +var ( |
| 42 | + gatewayIP = tcpip.AddrFrom4([4]byte{10, 0, 2, 1}) |
| 43 | + childIP = tcpip.AddrFrom4([4]byte{10, 0, 2, 100}) |
| 44 | +) |
| 45 | + |
| 46 | +// newTestStack creates a gvisor stack with spoofing and promiscuous mode |
| 47 | +// enabled, matching the configuration used by virtualnetwork.New. |
| 48 | +func newTestStack() *stack.Stack { |
| 49 | + s := stack.New(stack.Options{ |
| 50 | + NetworkProtocols: []stack.NetworkProtocolFactory{ipv4.NewProtocol}, |
| 51 | + TransportProtocols: []stack.TransportProtocolFactory{tcp.NewProtocol, udp.NewProtocol}, |
| 52 | + }) |
| 53 | + gomega.Expect(s.CreateNIC(1, loopback.New())).To(gomega.BeNil()) |
| 54 | + for _, addr := range []tcpip.Address{gatewayIP, childIP} { |
| 55 | + gomega.Expect(s.AddProtocolAddress(1, tcpip.ProtocolAddress{ |
| 56 | + Protocol: ipv4.ProtocolNumber, |
| 57 | + AddressWithPrefix: addr.WithPrefix(), |
| 58 | + }, stack.AddressProperties{})).To(gomega.BeNil()) |
| 59 | + } |
| 60 | + s.SetSpoofing(1, true) |
| 61 | + s.SetPromiscuousMode(1, true) |
| 62 | + s.SetRouteTable([]tcpip.Route{{Destination: header.IPv4EmptySubnet, NIC: 1}}) |
| 63 | + return s |
| 64 | +} |
| 65 | + |
| 66 | +// freeHostAddr returns a free "hostIP:port" address for the given network. |
| 67 | +func freeHostAddr(network string, ip net.IP) string { |
| 68 | + switch network { |
| 69 | + case "tcp": |
| 70 | + ln, err := net.Listen("tcp", ip.String()+":0") |
| 71 | + gomega.Expect(err).ShouldNot(gomega.HaveOccurred()) |
| 72 | + addr := ln.Addr().String() |
| 73 | + ln.Close() |
| 74 | + return addr |
| 75 | + case "udp": |
| 76 | + conn, err := net.ListenPacket("udp", ip.String()+":0") |
| 77 | + gomega.Expect(err).ShouldNot(gomega.HaveOccurred()) |
| 78 | + addr := conn.LocalAddr().String() |
| 79 | + conn.Close() |
| 80 | + return addr |
| 81 | + default: |
| 82 | + panic("unsupported network: " + network) |
| 83 | + } |
| 84 | +} |
| 85 | + |
| 86 | +var _ = ginkgo.Describe("source IP propagation", func() { |
| 87 | + ginkgo.It("should propagate routable IPv4 source addresses", func() { |
| 88 | + bindAddr := sourceBindAddr(net.ParseIP("10.0.2.50"), 1234) |
| 89 | + gomega.Expect(bindAddr).ToNot(gomega.BeNil()) |
| 90 | + gomega.Expect(bindAddr.NIC).To(gomega.Equal(tcpip.NICID(1))) |
| 91 | + gomega.Expect(bindAddr.Addr).To(gomega.Equal(tcpip.AddrFrom4([4]byte{10, 0, 2, 50}))) |
| 92 | + gomega.Expect(bindAddr.Port).To(gomega.Equal(uint16(1234))) |
| 93 | + }) |
| 94 | + |
| 95 | + // Loopback and unspecified sources must fall back to the default gateway |
| 96 | + // source: the guest replies to the source IP via the gateway, and gvisor |
| 97 | + // drops the reply as a martian packet when its destination is a loopback |
| 98 | + // address on the (non-loopback) tap NIC. This covers host-local forwards |
| 99 | + // such as 127.0.0.1:2222 -> 192.168.127.2:22. |
| 100 | + ginkgo.It("should not propagate loopback or unspecified source addresses", func() { |
| 101 | + gomega.Expect(sourceBindAddr(net.ParseIP("127.0.0.1"), 2222)).To(gomega.BeNil()) |
| 102 | + gomega.Expect(sourceBindAddr(net.IPv4zero, 0)).To(gomega.BeNil()) |
| 103 | + gomega.Expect(sourceBindAddr(net.ParseIP("::1"), 2222)).To(gomega.BeNil()) |
| 104 | + gomega.Expect(sourceBindAddr(net.ParseIP("2001:db8::1"), 2222)).To(gomega.BeNil()) |
| 105 | + }) |
| 106 | +}) |
| 107 | + |
| 108 | +var _ = ginkgo.Describe("port forwarding", func() { |
| 109 | + ginkgo.It("should preserve the client source IP for TCP", func() { |
| 110 | + ip := hostIP() |
| 111 | + if ip == nil { |
| 112 | + ginkgo.Skip("no non-loopback IPv4 address found") |
| 113 | + } |
| 114 | + |
| 115 | + s := newTestStack() |
| 116 | + |
| 117 | + childLn, err := gonet.ListenTCP(s, tcpip.FullAddress{NIC: 1, Addr: childIP, Port: 8080}, ipv4.ProtocolNumber) |
| 118 | + gomega.Expect(err).ShouldNot(gomega.HaveOccurred()) |
| 119 | + defer childLn.Close() |
| 120 | + |
| 121 | + sourceAddrCh := make(chan string, 1) |
| 122 | + go func() { |
| 123 | + conn, err := childLn.Accept() |
| 124 | + if err != nil { |
| 125 | + return |
| 126 | + } |
| 127 | + defer conn.Close() |
| 128 | + sourceAddrCh <- conn.RemoteAddr().String() |
| 129 | + _, _ = io.Copy(io.Discard, conn) |
| 130 | + }() |
| 131 | + |
| 132 | + listenAddr := freeHostAddr("tcp", ip) |
| 133 | + fw := NewPortsForwarder(s) |
| 134 | + gomega.Expect(fw.Expose(types.TCP, listenAddr, "10.0.2.100:8080")).Should(gomega.Succeed()) |
| 135 | + defer func() { _ = fw.Unexpose(types.TCP, listenAddr) }() |
| 136 | + |
| 137 | + conn, err := net.Dial("tcp", listenAddr) |
| 138 | + gomega.Expect(err).ShouldNot(gomega.HaveOccurred()) |
| 139 | + clientIP := conn.LocalAddr().(*net.TCPAddr).IP.String() |
| 140 | + conn.Close() |
| 141 | + |
| 142 | + var addr string |
| 143 | + gomega.Eventually(sourceAddrCh).Should(gomega.Receive(&addr)) |
| 144 | + host, _, err := net.SplitHostPort(addr) |
| 145 | + gomega.Expect(err).ShouldNot(gomega.HaveOccurred()) |
| 146 | + gomega.Expect(host).To(gomega.Equal(clientIP), |
| 147 | + fmt.Sprintf("child saw %s, expected client IP %s (gateway is 10.0.2.1)", host, clientIP)) |
| 148 | + }) |
| 149 | + |
| 150 | + ginkgo.It("should preserve the client source IP for UDP", func() { |
| 151 | + ip := hostIP() |
| 152 | + if ip == nil { |
| 153 | + ginkgo.Skip("no non-loopback IPv4 address found") |
| 154 | + } |
| 155 | + |
| 156 | + s := newTestStack() |
| 157 | + |
| 158 | + childAddr := tcpip.FullAddress{NIC: 1, Addr: childIP, Port: 8081} |
| 159 | + childConn, err := gonet.DialUDP(s, &childAddr, nil, ipv4.ProtocolNumber) |
| 160 | + gomega.Expect(err).ShouldNot(gomega.HaveOccurred()) |
| 161 | + defer childConn.Close() |
| 162 | + |
| 163 | + sourceAddrCh := make(chan string, 1) |
| 164 | + go func() { |
| 165 | + buf := make([]byte, 1024) |
| 166 | + n, from, err := childConn.ReadFrom(buf) |
| 167 | + if err != nil { |
| 168 | + return |
| 169 | + } |
| 170 | + sourceAddrCh <- from.String() |
| 171 | + // Echo back |
| 172 | + _, _ = childConn.WriteTo(buf[:n], from) |
| 173 | + }() |
| 174 | + |
| 175 | + listenAddr := freeHostAddr("udp", ip) |
| 176 | + fw := NewPortsForwarder(s) |
| 177 | + gomega.Expect(fw.Expose(types.UDP, listenAddr, "10.0.2.100:8081")).Should(gomega.Succeed()) |
| 178 | + defer func() { _ = fw.Unexpose(types.UDP, listenAddr) }() |
| 179 | + |
| 180 | + clientConn, err := net.Dial("udp", listenAddr) |
| 181 | + gomega.Expect(err).ShouldNot(gomega.HaveOccurred()) |
| 182 | + defer clientConn.Close() |
| 183 | + clientIP := clientConn.LocalAddr().(*net.UDPAddr).IP.String() |
| 184 | + |
| 185 | + _, err = clientConn.Write([]byte("hello")) |
| 186 | + gomega.Expect(err).ShouldNot(gomega.HaveOccurred()) |
| 187 | + |
| 188 | + // Read echo to ensure round-trip completes. |
| 189 | + gomega.Expect(clientConn.SetReadDeadline(time.Now().Add(5 * time.Second))).ShouldNot(gomega.HaveOccurred()) |
| 190 | + buf := make([]byte, 1024) |
| 191 | + _, err = clientConn.Read(buf) |
| 192 | + gomega.Expect(err).ShouldNot(gomega.HaveOccurred()) |
| 193 | + |
| 194 | + var addr string |
| 195 | + gomega.Eventually(sourceAddrCh).Should(gomega.Receive(&addr)) |
| 196 | + host, _, err := net.SplitHostPort(addr) |
| 197 | + gomega.Expect(err).ShouldNot(gomega.HaveOccurred()) |
| 198 | + gomega.Expect(host).To(gomega.Equal(clientIP), |
| 199 | + fmt.Sprintf("child saw %s, expected client IP %s (gateway is 10.0.2.1)", host, clientIP)) |
| 200 | + }) |
| 201 | +}) |
0 commit comments