-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathtcp.go
211 lines (191 loc) · 6 KB
/
tcp.go
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
package tunat
import (
"errors"
"net"
"net/netip"
"gvisor.dev/gvisor/pkg/tcpip"
"gvisor.dev/gvisor/pkg/tcpip/header"
)
type tcpMapValue struct {
natAddr netip.AddrPort // fakeSAddr or originSAddr
daddr netip.AddrPort
}
type tcpConn struct {
net.Conn
tunat *Tunat
saddr netip.AddrPort
daddr netip.AddrPort
saddrInterface net.Addr
daddrInterface net.Addr
}
// Close delete nat map at the same time
func (tc *tcpConn) Close() error {
if value, ok := tc.tunat.tcpMap.Load(tc.saddr); ok {
tc.tunat.tcpMap.Delete(value.(*tcpMapValue).natAddr)
tc.tunat.tcpMap.Delete(tc.saddr)
}
return tc.Conn.Close()
}
// LocalAddr original destination address
func (tc *tcpConn) LocalAddr() net.Addr {
return tc.daddrInterface
}
// RemoteAddr original source address
func (tc *tcpConn) RemoteAddr() net.Addr {
return tc.saddrInterface
}
// Accept like net package
func (t *Tunat) Accept() (conn net.Conn, err error) {
acceptConn, err := t.tcpListener.Accept()
if err != nil {
return
}
connRemoteAddr := acceptConn.RemoteAddr().(*net.TCPAddr).AddrPort()
if connRemoteAddr.Addr().Is4In6() {
connRemoteAddr = netip.AddrPortFrom(connRemoteAddr.Addr().Unmap(), connRemoteAddr.Port())
}
value, ok := t.tcpMap.Load(connRemoteAddr)
if !ok {
return nil, errors.New("tcp nat map not exist")
}
saddr := value.(*tcpMapValue).natAddr
daddr := value.(*tcpMapValue).daddr
conn = &tcpConn{
Conn: acceptConn,
tunat: t,
saddr: saddr,
daddr: daddr,
saddrInterface: net.TCPAddrFromAddrPort(saddr),
daddrInterface: net.TCPAddrFromAddrPort(daddr),
}
return
}
func (t *Tunat) handleIPv4TCP(ipHeader header.IPv4, tcpHeader header.TCP) {
/*
tcpListener 10.0.0.1:100
raw 10.0.0.1:1234 -> 1.2.3.4:4321 SYN
modified 10.0.0.2:1234 -> 10.0.0.1:100 SYN
raw 10.0.0.1:100 -> 10.0.0.2:1234 ACK SYN
modified 1.2.3.4:4321 -> 10.0.0.1:1234 ACK SYN
raw 10.0.0.1:1234 -> 1.2.3.4:4321 ACK
modified 10.0.0.2:1234 -> 10.0.0.1:100 ACK
*/
ip, ok := netip.AddrFromSlice([]byte(ipHeader.SourceAddress()))
if !ok {
return
}
saddr := netip.AddrPortFrom(ip, tcpHeader.SourcePort())
ip, ok = netip.AddrFromSlice([]byte(ipHeader.DestinationAddress()))
if !ok {
return
}
daddr := netip.AddrPortFrom(ip, tcpHeader.DestinationPort())
if tcpHeader.Flags()&header.TCPFlagSyn == 0 || tcpHeader.Flags()&header.TCPFlagAck != 0 {
goto next
}
if _, ok := t.tcpMap.Load(saddr); ok {
goto next
}
for port, endPort := tcpHeader.SourcePort(), tcpHeader.SourcePort()-1; port != endPort; port++ {
if port == 0 {
continue
}
fakeAddr := netip.AddrPortFrom(t.fakeIPv4Addr, port)
if _, ok := t.tcpMap.Load(fakeAddr); !ok {
t.tcpMap.Store(fakeAddr, &tcpMapValue{natAddr: saddr, daddr: daddr})
t.tcpMap.Store(saddr, &tcpMapValue{natAddr: fakeAddr, daddr: daddr})
goto next
}
}
return
next:
if value, ok := t.tcpMap.Load(saddr); ok {
ipHeader.SetSourceAddress(tcpip.Address(value.(*tcpMapValue).natAddr.Addr().AsSlice()))
ipHeader.SetDestinationAddress(tcpip.Address(t.ipv4TCPListenerAddrPort.Addr().AsSlice()))
tcpHeader.SetSourcePort(uint16(value.(*tcpMapValue).natAddr.Port()))
tcpHeader.SetDestinationPort(uint16(t.ipv4TCPListenerAddrPort.Port()))
} else if value, ok := t.tcpMap.Load(daddr); ok {
ipHeader.SetSourceAddress(tcpip.Address(value.(*tcpMapValue).daddr.Addr().AsSlice()))
ipHeader.SetDestinationAddress(tcpip.Address(value.(*tcpMapValue).natAddr.Addr().AsSlice()))
tcpHeader.SetSourcePort(uint16(value.(*tcpMapValue).daddr.Port()))
tcpHeader.SetDestinationPort(uint16(value.(*tcpMapValue).natAddr.Port()))
} else {
return
}
ipHeader.SetChecksum(0)
ipHeader.SetChecksum(^ipHeader.CalculateChecksum())
tcpHeader.SetChecksum(0)
tcpHeader.SetChecksum(
^tcpHeader.CalculateChecksum(
header.Checksum(
tcpHeader.Payload(),
header.PseudoHeaderChecksum(
header.TCPProtocolNumber,
ipHeader.SourceAddress(),
ipHeader.DestinationAddress(),
uint16(len(tcpHeader)),
),
),
),
)
_, _ = t.file.Write(ipHeader)
}
func (t *Tunat) handleIPv6TCP(ipHeader header.IPv6, tcpHeader header.TCP) {
ip, ok := netip.AddrFromSlice([]byte(ipHeader.SourceAddress()))
if !ok {
return
}
saddr := netip.AddrPortFrom(ip, tcpHeader.SourcePort())
ip, ok = netip.AddrFromSlice([]byte(ipHeader.DestinationAddress()))
if !ok {
return
}
daddr := netip.AddrPortFrom(ip, tcpHeader.DestinationPort())
if tcpHeader.Flags()&header.TCPFlagSyn == 0 || tcpHeader.Flags()&header.TCPFlagAck != 0 {
goto next
}
if _, ok := t.tcpMap.Load(saddr); ok {
goto next
}
for port, endPort := tcpHeader.SourcePort(), tcpHeader.SourcePort()-1; port != endPort; port++ {
if port == 0 {
continue
}
fakeAddr := netip.AddrPortFrom(t.fakeIPv6Addr, port)
if _, ok := t.tcpMap.Load(fakeAddr); !ok {
t.tcpMap.Store(fakeAddr, &tcpMapValue{natAddr: saddr, daddr: daddr})
t.tcpMap.Store(saddr, &tcpMapValue{natAddr: fakeAddr, daddr: daddr})
goto next
}
}
return
next:
if value, ok := t.tcpMap.Load(saddr); ok {
ipHeader.SetSourceAddress(tcpip.Address(value.(*tcpMapValue).natAddr.Addr().AsSlice()))
ipHeader.SetDestinationAddress(tcpip.Address(t.ipv6TCPListenerAddrPort.Addr().AsSlice()))
tcpHeader.SetSourcePort(uint16(value.(*tcpMapValue).natAddr.Port()))
tcpHeader.SetDestinationPort(uint16(t.ipv6TCPListenerAddrPort.Port()))
} else if value, ok := t.tcpMap.Load(daddr); ok {
ipHeader.SetSourceAddress(tcpip.Address(value.(*tcpMapValue).daddr.Addr().AsSlice()))
ipHeader.SetDestinationAddress(tcpip.Address(value.(*tcpMapValue).natAddr.Addr().AsSlice()))
tcpHeader.SetSourcePort(uint16(value.(*tcpMapValue).daddr.Port()))
tcpHeader.SetDestinationPort(uint16(value.(*tcpMapValue).natAddr.Port()))
} else {
return
}
tcpHeader.SetChecksum(0)
tcpHeader.SetChecksum(
^tcpHeader.CalculateChecksum(
header.Checksum(
tcpHeader.Payload(),
header.PseudoHeaderChecksum(
header.TCPProtocolNumber,
ipHeader.SourceAddress(),
ipHeader.DestinationAddress(),
uint16(len(tcpHeader)),
),
),
),
)
_, _ = t.file.Write(ipHeader)
}