-
Notifications
You must be signed in to change notification settings - Fork 3
/
udp.go
149 lines (134 loc) · 3.89 KB
/
udp.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
package tunat
import (
"net/netip"
"gvisor.dev/gvisor/pkg/tcpip"
"gvisor.dev/gvisor/pkg/tcpip/header"
)
type udpData struct {
payload []byte
saddr netip.AddrPort
daddr netip.AddrPort
}
// ReadFromUDPAddrPort like net package
func (t *Tunat) ReadFromUDPAddrPort(payload []byte) (nread int, saddr, daddr netip.AddrPort, err error) {
udpData := <-t.udpChan
nread = copy(payload, udpData.payload)
return nread, udpData.saddr, udpData.daddr, nil
}
// WriteToUDPAddrPort like net package
func (t *Tunat) WriteToUDPAddrPort(payload []byte, saddr, daddr netip.AddrPort) (nwrite int, err error) {
if saddr.Addr().Is4() {
return t.ipv4WriteTo(payload, saddr, daddr)
}
return t.ipv6WriteTo(payload, saddr, daddr)
}
func (t *Tunat) ipv4WriteTo(payload []byte, saddr, daddr netip.AddrPort) (nwrite int, err error) {
totalLen := header.IPv4MinimumSize + header.UDPMinimumSize + len(payload)
ipHeader := header.IPv4(
append(
make([]byte, totalLen-len(payload)),
payload...,
),
)
ipHeader.Encode(&header.IPv4Fields{
TotalLength: uint16(totalLen),
TTL: 64,
Protocol: uint8(header.UDPProtocolNumber),
SrcAddr: tcpip.Address(saddr.Addr().AsSlice()),
DstAddr: tcpip.Address(daddr.Addr().AsSlice()),
})
ipHeader.SetChecksum(0)
ipHeader.SetChecksum(^ipHeader.CalculateChecksum())
udpHeader := header.UDP(ipHeader.Payload())
udpHeader.Encode(&header.UDPFields{
SrcPort: uint16(saddr.Port()),
DstPort: uint16(daddr.Port()),
Length: uint16(header.UDPMinimumSize + len(payload)),
})
udpHeader.SetChecksum(0)
udpHeader.SetChecksum(
^udpHeader.CalculateChecksum(
header.Checksum(
udpHeader.Payload(),
header.PseudoHeaderChecksum(
header.UDPProtocolNumber,
ipHeader.SourceAddress(),
ipHeader.DestinationAddress(),
udpHeader.Length(),
),
),
),
)
return t.file.Write(ipHeader)
}
func (t *Tunat) ipv6WriteTo(payload []byte, saddr, daddr netip.AddrPort) (nwrite int, err error) {
totalLen := header.IPv6MinimumSize + header.UDPMinimumSize + len(payload)
ipHeader := header.IPv6(
append(
make([]byte, totalLen-len(payload)),
payload...,
),
)
ipHeader.Encode(&header.IPv6Fields{
PayloadLength: uint16(header.UDPMinimumSize + len(payload)),
TransportProtocol: header.UDPProtocolNumber,
HopLimit: 64,
SrcAddr: tcpip.Address(saddr.Addr().AsSlice()),
DstAddr: tcpip.Address(daddr.Addr().AsSlice()),
})
udpHeader := header.UDP(ipHeader.Payload())
udpHeader.Encode(&header.UDPFields{
SrcPort: uint16(saddr.Port()),
DstPort: uint16(daddr.Port()),
Length: uint16(header.UDPMinimumSize + len(payload)),
})
udpHeader.SetChecksum(0)
udpHeader.SetChecksum(
^udpHeader.CalculateChecksum(
header.Checksum(
udpHeader.Payload(),
header.PseudoHeaderChecksum(
header.UDPProtocolNumber,
ipHeader.SourceAddress(),
ipHeader.DestinationAddress(),
udpHeader.Length(),
),
),
),
)
return t.file.Write(ipHeader)
}
func (t *Tunat) handleIPv4UDP(ipHeader header.IPv4, udpHeader header.UDP) {
ip, ok := netip.AddrFromSlice([]byte(ipHeader.SourceAddress()))
if !ok {
return
}
saddr := netip.AddrPortFrom(ip, udpHeader.SourcePort())
ip, ok = netip.AddrFromSlice([]byte(ipHeader.DestinationAddress()))
if !ok {
return
}
daddr := netip.AddrPortFrom(ip, udpHeader.DestinationPort())
t.udpChan <- udpData{
payload: append([]byte(nil), udpHeader.Payload()...),
saddr: saddr,
daddr: daddr,
}
}
func (t *Tunat) handleIPv6UDP(ipHeader header.IPv6, udpHeader header.UDP) {
ip, ok := netip.AddrFromSlice([]byte(ipHeader.SourceAddress()))
if !ok {
return
}
saddr := netip.AddrPortFrom(ip, udpHeader.SourcePort())
ip, ok = netip.AddrFromSlice([]byte(ipHeader.DestinationAddress()))
if !ok {
return
}
daddr := netip.AddrPortFrom(ip, udpHeader.DestinationPort())
t.udpChan <- udpData{
payload: append([]byte(nil), udpHeader.Payload()...),
saddr: saddr,
daddr: daddr,
}
}