-
Notifications
You must be signed in to change notification settings - Fork 1
/
net.go
264 lines (204 loc) · 6.61 KB
/
net.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
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
// i.MX Ethernet (ENET) driver
//
// Copyright (c) WithSecure Corporation
// https://foundry.withsecure.com
//
// Use of this source code is governed by the license
// that can be found in the LICENSE file.
// Package enet implements TCP/IP connectivity through Ethernet (ENET)
// controllers on i.MX6 SoCs.
//
// The TCP/IP stack is implemented using gVisor pure Go implementation.
//
// This package is only meant to be used with `GOOS=tamago GOARCH=arm` as
// supported by the TamaGo framework for bare metal Go on ARM SoCs, see
// https://github.com/usbarmory/tamago.
package enet
import (
"context"
"fmt"
"net"
"strconv"
"github.com/usbarmory/tamago/soc/nxp/enet"
"gvisor.dev/gvisor/pkg/tcpip"
"gvisor.dev/gvisor/pkg/tcpip/adapters/gonet"
"gvisor.dev/gvisor/pkg/tcpip/header"
"gvisor.dev/gvisor/pkg/tcpip/link/channel"
"gvisor.dev/gvisor/pkg/tcpip/network/arp"
"gvisor.dev/gvisor/pkg/tcpip/network/ipv4"
"gvisor.dev/gvisor/pkg/tcpip/stack"
"gvisor.dev/gvisor/pkg/tcpip/transport/icmp"
"gvisor.dev/gvisor/pkg/tcpip/transport/tcp"
"gvisor.dev/gvisor/pkg/tcpip/transport/udp"
"gvisor.dev/gvisor/pkg/waiter"
)
var (
// MTU represents the Ethernet Maximum Transmission Unit.
MTU uint32 = enet.MTU
// NICID represents the default gVisor NIC identifier
NICID = tcpip.NICID(1)
// DefaultStackOptions represents the default gVisor Stack configuration
DefaultStackOptions = stack.Options{
NetworkProtocols: []stack.NetworkProtocolFactory{
ipv4.NewProtocol,
arp.NewProtocol},
TransportProtocols: []stack.TransportProtocolFactory{
tcp.NewProtocol,
icmp.NewProtocol4,
udp.NewProtocol},
}
)
// Interface represents an Ethernet interface instance.
type Interface struct {
NICID tcpip.NICID
NIC *NIC
Stack *stack.Stack
Link *channel.Endpoint
}
func (iface *Interface) configure(mac string, ip tcpip.AddressWithPrefix, gw tcpip.Address) (err error) {
if iface.Stack == nil {
iface.Stack = stack.New(DefaultStackOptions)
}
linkAddr, err := tcpip.ParseMACAddress(mac)
if err != nil {
return
}
iface.Link = channel.New(256, MTU, linkAddr)
iface.Link.LinkEPCapabilities |= stack.CapabilityResolutionRequired
linkEP := stack.LinkEndpoint(iface.Link)
if err := iface.Stack.CreateNIC(iface.NICID, linkEP); err != nil {
return fmt.Errorf("%v", err)
}
protocolAddr := tcpip.ProtocolAddress{
Protocol: ipv4.ProtocolNumber,
AddressWithPrefix: ip,
}
if err := iface.Stack.AddProtocolAddress(iface.NICID, protocolAddr, stack.AddressProperties{}); err != nil {
return fmt.Errorf("%v", err)
}
rt := iface.Stack.GetRouteTable()
rt = append(rt, tcpip.Route{
Destination: protocolAddr.AddressWithPrefix.Subnet(),
NIC: iface.NICID,
})
rt = append(rt, tcpip.Route{
Destination: header.IPv4EmptySubnet,
Gateway: gw,
NIC: iface.NICID,
})
iface.Stack.SetRouteTable(rt)
return
}
// EnableICMP adds an ICMP endpoint to the interface, it is useful to enable
// ping requests.
func (iface *Interface) EnableICMP() error {
var wq waiter.Queue
ep, err := iface.Stack.NewEndpoint(icmp.ProtocolNumber4, ipv4.ProtocolNumber, &wq)
if err != nil {
return fmt.Errorf("endpoint error (icmp): %v", err)
}
addr, tcpErr := iface.Stack.GetMainNICAddress(iface.NICID, ipv4.ProtocolNumber)
if tcpErr != nil {
return fmt.Errorf("couldn't get NIC IP address: %v", tcpErr)
}
fullAddr := tcpip.FullAddress{Addr: addr.Address, Port: 0, NIC: iface.NICID}
if err := ep.Bind(fullAddr); err != nil {
return fmt.Errorf("bind error (icmp endpoint): ", err)
}
return nil
}
// ListenerTCP4 returns a net.Listener capable of accepting IPv4 TCP
// connections for the argument port.
func (iface *Interface) ListenerTCP4(port uint16) (net.Listener, error) {
addr, tcpErr := iface.Stack.GetMainNICAddress(iface.NICID, ipv4.ProtocolNumber)
if tcpErr != nil {
return nil, fmt.Errorf("couldn't get NIC IP address: %v", tcpErr)
}
fullAddr := tcpip.FullAddress{Addr: addr.Address, Port: port, NIC: iface.NICID}
listener, err := gonet.ListenTCP(iface.Stack, fullAddr, ipv4.ProtocolNumber)
if err != nil {
return nil, err
}
return (net.Listener)(listener), nil
}
// DialTCP4 connects to an IPv4 TCP address.
func (iface *Interface) DialTCP4(address string) (net.Conn, error) {
return iface.DialContextTCP4(context.Background(), address)
}
// DialContextTCP4 connects to an IPv4 TCP address with support for timeout
// supplied by ctx.
func (iface *Interface) DialContextTCP4(ctx context.Context, address string) (net.Conn, error) {
fullAddr, err := fullAddr(address)
if err != nil {
return nil, err
}
conn, err := gonet.DialContextTCP(ctx, iface.Stack, fullAddr, ipv4.ProtocolNumber)
if err != nil {
return nil, err
}
return (net.Conn)(conn), nil
}
// DialUDP4 creates a UDP connection to the ip:port specified by rAddr, optionally setting
// the local ip:port to lAddr.
func (iface *Interface) DialUDP4(lAddr, rAddr string) (net.Conn, error) {
var lFullAddr tcpip.FullAddress
var rFullAddr tcpip.FullAddress
var err error
if lAddr != "" {
if lFullAddr, err = fullAddr(lAddr); err != nil {
return nil, fmt.Errorf("failed to parse lAddr %q: %v", lAddr, err)
}
}
if rAddr != "" {
if rFullAddr, err = fullAddr(rAddr); err != nil {
return nil, fmt.Errorf("failed to parse rAddr %q: %v", rAddr, err)
}
}
conn, err := gonet.DialUDP(iface.Stack, &lFullAddr, &rFullAddr, ipv4.ProtocolNumber)
if err != nil {
return nil, err
}
return (net.Conn)(conn), nil
}
// fullAddr attempts to convert the ip:port to a FullAddress struct.
func fullAddr(a string) (tcpip.FullAddress, error) {
var p int
host, port, err := net.SplitHostPort(a)
if err == nil {
if p, err = strconv.Atoi(port); err != nil {
return tcpip.FullAddress{}, err
}
} else {
host = a
}
addr := net.ParseIP(host)
return tcpip.FullAddress{Addr: tcpip.AddrFromSlice(addr.To4()), Port: uint16(p)}, nil
}
// Init initializes an ENET Ethernet interface associating it to a gVisor link,
// a default NICID and TCP/IP gVisor Stack are set if not previously assigned.
func (iface *Interface) Init(nic *enet.ENET, ip string, netmask string, mac string, gateway string) (err error) {
address, err := net.ParseMAC(mac)
if err != nil {
return
}
if iface.NICID == 0 {
iface.NICID = NICID
}
ipAddr := tcpip.AddressWithPrefix{
Address: tcpip.AddrFromSlice(net.ParseIP(ip).To4()),
PrefixLen: tcpip.MaskFromBytes(net.ParseIP(netmask).To4()).Prefix(),
}
gwAddr := tcpip.AddrFromSlice(net.ParseIP(gateway)).To4()
if err = iface.configure(mac, ipAddr, gwAddr); err != nil {
return
}
if iface.NIC == nil {
iface.NIC = &NIC{
MAC: address,
Link: iface.Link,
Device: nic,
}
err = iface.NIC.Init()
}
return
}