1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
|
// Copyright 2021 The gVisor Authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package istio_test
import (
"fmt"
"io"
"net"
"net/http"
"strconv"
"testing"
"github.com/google/go-cmp/cmp"
"gvisor.dev/gvisor/pkg/context"
"gvisor.dev/gvisor/pkg/rand"
"gvisor.dev/gvisor/pkg/sync"
"gvisor.dev/gvisor/pkg/tcpip"
"gvisor.dev/gvisor/pkg/tcpip/adapters/gonet"
"gvisor.dev/gvisor/pkg/tcpip/header"
"gvisor.dev/gvisor/pkg/tcpip/link/loopback"
"gvisor.dev/gvisor/pkg/tcpip/link/pipe"
"gvisor.dev/gvisor/pkg/tcpip/link/sniffer"
"gvisor.dev/gvisor/pkg/tcpip/network/ipv4"
"gvisor.dev/gvisor/pkg/tcpip/stack"
"gvisor.dev/gvisor/pkg/tcpip/testutil"
"gvisor.dev/gvisor/pkg/tcpip/transport/tcp"
)
// testContext encapsulates the state required to run tests that simulate
// an istio like environment.
//
// A diagram depicting the setup is shown below.
// +-----------------------------------------------------------------------+
// | +-------------------------------------------------+ |
// | + ----------+ | + -----------------+ PROXY +----------+ | |
// | | clientEP | | | serverListeningEP|--accepted-> | serverEP |-+ | |
// | + ----------+ | + -----------------+ +----------+ | | |
// | | -------|-------------+ +----------+ | | |
// | | | | | proxyEP |-+ | |
// | +-----redirect | +----------+ | |
// | + ------------+---|------+---+ |
// | | |
// | Local Stack. | |
// +-------------------------------------------------------|---------------+
// |
// +-----------------------------------------------------------------------+
// | remoteStack | |
// | +-------------SYN ---------------| |
// | | | |
// | +-------------------|--------------------------------|-_---+ |
// | | + -----------------+ + ----------+ | | |
// | | | remoteListeningEP|--accepted--->| remoteEP |<++ | |
// | | + -----------------+ + ----------+ | |
// | | Remote HTTP Server | |
// | +----------------------------------------------------------+ |
// +-----------------------------------------------------------------------+
//
type testContext struct {
// localServerListener is the listening port for the server which will proxy
// all traffic to the remote EP.
localServerListener *gonet.TCPListener
// remoteListenListener is the remote listening endpoint that will receive
// connections from server.
remoteServerListener *gonet.TCPListener
// localStack is the stack used to create client/server endpoints and
// also the stack on which we install NAT redirect rules.
localStack *stack.Stack
// remoteStack is the stack that represents a *remote* server.
remoteStack *stack.Stack
// defaultResponse is the response served by the HTTP server for all GET
defaultResponse []byte
// requests. wg is used to wait for HTTP server and Proxy to terminate before
// returning from cleanup.
wg sync.WaitGroup
}
func (ctx *testContext) cleanup() {
ctx.localServerListener.Close()
ctx.localStack.Close()
ctx.remoteServerListener.Close()
ctx.remoteStack.Close()
ctx.wg.Wait()
}
const (
localServerPort = 8080
remoteServerPort = 9090
)
var (
localIPv4Addr1 = testutil.MustParse4("10.0.0.1")
localIPv4Addr2 = testutil.MustParse4("10.0.0.2")
loopbackIPv4Addr = testutil.MustParse4("127.0.0.1")
remoteIPv4Addr1 = testutil.MustParse4("10.0.0.3")
)
func newTestContext(t *testing.T) *testContext {
t.Helper()
localNIC, remoteNIC := pipe.New("" /* linkAddr1 */, "" /* linkAddr2 */)
localStack := stack.New(stack.Options{
NetworkProtocols: []stack.NetworkProtocolFactory{ipv4.NewProtocol},
TransportProtocols: []stack.TransportProtocolFactory{tcp.NewProtocol},
HandleLocal: true,
})
remoteStack := stack.New(stack.Options{
NetworkProtocols: []stack.NetworkProtocolFactory{ipv4.NewProtocol},
TransportProtocols: []stack.TransportProtocolFactory{tcp.NewProtocol},
HandleLocal: true,
})
// Add loopback NIC. We need a loopback NIC as NAT redirect rule redirect to
// loopback address + specified port.
loopbackNIC := loopback.New()
const loopbackNICID = tcpip.NICID(1)
if err := localStack.CreateNIC(loopbackNICID, sniffer.New(loopbackNIC)); err != nil {
t.Fatalf("localStack.CreateNIC(%d, _): %s", loopbackNICID, err)
}
loopbackAddr := tcpip.ProtocolAddress{
Protocol: header.IPv4ProtocolNumber,
AddressWithPrefix: loopbackIPv4Addr.WithPrefix(),
}
if err := localStack.AddProtocolAddress(loopbackNICID, loopbackAddr, stack.AddressProperties{}); err != nil {
t.Fatalf("localStack.AddProtocolAddress(%d, %+v, {}): %s", loopbackNICID, loopbackAddr, err)
}
// Create linked NICs that connects the local and remote stack.
const localNICID = tcpip.NICID(2)
const remoteNICID = tcpip.NICID(3)
if err := localStack.CreateNIC(localNICID, sniffer.New(localNIC)); err != nil {
t.Fatalf("localStack.CreateNIC(%d, _): %s", localNICID, err)
}
if err := remoteStack.CreateNIC(remoteNICID, sniffer.New(remoteNIC)); err != nil {
t.Fatalf("remoteStack.CreateNIC(%d, _): %s", remoteNICID, err)
}
for _, addr := range []tcpip.Address{localIPv4Addr1, localIPv4Addr2} {
localProtocolAddr := tcpip.ProtocolAddress{
Protocol: header.IPv4ProtocolNumber,
AddressWithPrefix: addr.WithPrefix(),
}
if err := localStack.AddProtocolAddress(localNICID, localProtocolAddr, stack.AddressProperties{}); err != nil {
t.Fatalf("localStack.AddProtocolAddress(%d, %+v, {}): %s", localNICID, localProtocolAddr, err)
}
}
remoteProtocolAddr := tcpip.ProtocolAddress{
Protocol: header.IPv4ProtocolNumber,
AddressWithPrefix: remoteIPv4Addr1.WithPrefix(),
}
if err := remoteStack.AddProtocolAddress(remoteNICID, remoteProtocolAddr, stack.AddressProperties{}); err != nil {
t.Fatalf("remoteStack.AddProtocolAddress(%d, %+v, {}): %s", remoteNICID, remoteProtocolAddr, err)
}
// Setup route table for local and remote stacks.
localStack.SetRouteTable([]tcpip.Route{
{
Destination: header.IPv4LoopbackSubnet,
NIC: loopbackNICID,
},
{
Destination: header.IPv4EmptySubnet,
NIC: localNICID,
},
})
remoteStack.SetRouteTable([]tcpip.Route{
{
Destination: header.IPv4EmptySubnet,
NIC: remoteNICID,
},
})
const netProto = ipv4.ProtocolNumber
localServerAddress := tcpip.FullAddress{
Port: localServerPort,
}
localServerListener, err := gonet.ListenTCP(localStack, localServerAddress, netProto)
if err != nil {
t.Fatalf("gonet.ListenTCP(_, %+v, %d) = %s", localServerAddress, netProto, err)
}
remoteServerAddress := tcpip.FullAddress{
Port: remoteServerPort,
}
remoteServerListener, err := gonet.ListenTCP(remoteStack, remoteServerAddress, netProto)
if err != nil {
t.Fatalf("gonet.ListenTCP(_, %+v, %d) = %s", remoteServerAddress, netProto, err)
}
// Initialize a random default response served by the HTTP server.
defaultResponse := make([]byte, 512<<10)
if _, err := rand.Read(defaultResponse); err != nil {
t.Fatalf("rand.Read(buf) failed: %s", err)
}
tc := &testContext{
localServerListener: localServerListener,
remoteServerListener: remoteServerListener,
localStack: localStack,
remoteStack: remoteStack,
defaultResponse: defaultResponse,
}
tc.startServers(t)
return tc
}
func (ctx *testContext) startServers(t *testing.T) {
ctx.wg.Add(1)
go func() {
defer ctx.wg.Done()
ctx.startHTTPServer()
}()
ctx.wg.Add(1)
go func() {
defer ctx.wg.Done()
ctx.startTCPProxyServer(t)
}()
}
func (ctx *testContext) startTCPProxyServer(t *testing.T) {
t.Helper()
for {
conn, err := ctx.localServerListener.Accept()
if err != nil {
t.Logf("terminating local proxy server: %s", err)
return
}
// Start a goroutine to handle this inbound connection.
go func() {
remoteServerAddr := tcpip.FullAddress{
Addr: remoteIPv4Addr1,
Port: remoteServerPort,
}
localServerAddr := tcpip.FullAddress{
Addr: localIPv4Addr2,
}
serverConn, err := gonet.DialTCPWithBind(context.Background(), ctx.localStack, localServerAddr, remoteServerAddr, ipv4.ProtocolNumber)
if err != nil {
t.Logf("gonet.DialTCP(_, %+v, %d) = %s", remoteServerAddr, ipv4.ProtocolNumber, err)
return
}
proxy(conn, serverConn)
t.Logf("proxying completed")
}()
}
}
// proxy transparently proxies the TCP payload from conn1 to conn2
// and vice versa.
func proxy(conn1, conn2 net.Conn) {
var wg sync.WaitGroup
wg.Add(1)
go func() {
io.Copy(conn2, conn1)
conn1.Close()
conn2.Close()
}()
wg.Add(1)
go func() {
io.Copy(conn1, conn2)
conn1.Close()
conn2.Close()
}()
wg.Wait()
}
func (ctx *testContext) startHTTPServer() {
handlerFunc := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Write([]byte(ctx.defaultResponse))
})
s := &http.Server{
Handler: handlerFunc,
}
s.Serve(ctx.remoteServerListener)
}
func TestOutboundNATRedirect(t *testing.T) {
ctx := newTestContext(t)
defer ctx.cleanup()
// Install an IPTable rule to redirect all TCP traffic with the sourceIP of
// localIPv4Addr1 to the tcp proxy port.
ipt := ctx.localStack.IPTables()
tbl := ipt.GetTable(stack.NATID, false /* ipv6 */)
ruleIdx := tbl.BuiltinChains[stack.Output]
tbl.Rules[ruleIdx].Filter = stack.IPHeaderFilter{
Protocol: tcp.ProtocolNumber,
CheckProtocol: true,
Src: localIPv4Addr1,
SrcMask: tcpip.Address("\xff\xff\xff\xff"),
}
tbl.Rules[ruleIdx].Target = &stack.RedirectTarget{
Port: localServerPort,
NetworkProtocol: ipv4.ProtocolNumber,
}
tbl.Rules[ruleIdx+1].Target = &stack.AcceptTarget{}
if err := ipt.ReplaceTable(stack.NATID, tbl, false /* ipv6 */); err != nil {
t.Fatalf("ipt.ReplaceTable(%d, _, false): %s", stack.NATID, err)
}
dialFunc := func(protocol, address string) (net.Conn, error) {
host, port, err := net.SplitHostPort(address)
if err != nil {
return nil, fmt.Errorf("unable to parse address: %s, err: %s", address, err)
}
remoteServerIP := net.ParseIP(host)
remoteServerPort, err := strconv.Atoi(port)
if err != nil {
return nil, fmt.Errorf("unable to parse port from string %s, err: %s", port, err)
}
remoteAddress := tcpip.FullAddress{
Addr: tcpip.Address(remoteServerIP.To4()),
Port: uint16(remoteServerPort),
}
// Dial with an explicit source address bound so that the redirect rule will
// be able to correctly redirect these packets.
localAddr := tcpip.FullAddress{Addr: localIPv4Addr1}
return gonet.DialTCPWithBind(context.Background(), ctx.localStack, localAddr, remoteAddress, ipv4.ProtocolNumber)
}
httpClient := &http.Client{
Transport: &http.Transport{
Dial: dialFunc,
},
}
serverURL := fmt.Sprintf("http://[%s]:%d/", net.IP(remoteIPv4Addr1), remoteServerPort)
response, err := httpClient.Get(serverURL)
if err != nil {
t.Fatalf("httpClient.Get(\"/\") failed: %s", err)
}
if got, want := response.StatusCode, http.StatusOK; got != want {
t.Fatalf("unexpected status code got: %d, want: %d", got, want)
}
body, err := io.ReadAll(response.Body)
if err != nil {
t.Fatalf("io.ReadAll(response.Body) failed: %s", err)
}
response.Body.Close()
if diff := cmp.Diff(body, ctx.defaultResponse); diff != "" {
t.Fatalf("unexpected response (-want +got): \n %s", diff)
}
}
|