-
Notifications
You must be signed in to change notification settings - Fork 28
Expand file tree
/
Copy pathnetstack.go
More file actions
101 lines (88 loc) · 2.29 KB
/
netstack.go
File metadata and controls
101 lines (88 loc) · 2.29 KB
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
// Copyright (c) 2022 RethinkDNS and its authors.
//
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at http://mozilla.org/MPL/2.0/.
package netstack
import (
"gvisor.dev/gvisor/pkg/tcpip"
"gvisor.dev/gvisor/pkg/tcpip/header"
"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"
)
type GConnHandler interface {
TCP() GTCPConnHandler
UDP() GUDPConnHandler
}
type gconnhandler struct {
GConnHandler
tcp GTCPConnHandler
udp GUDPConnHandler
}
func NewGConnHandler(tcp GTCPConnHandler, udp GUDPConnHandler) GConnHandler {
return &gconnhandler{
tcp: tcp,
udp: udp,
}
}
func (g *gconnhandler) TCP() GTCPConnHandler {
return g.tcp
}
func (g *gconnhandler) UDP() GUDPConnHandler {
return g.udp
}
func NewEndpoint(dev int, mtu uint32) (stack.LinkEndpoint, error) {
var endpoint stack.LinkEndpoint
var fd_array []int
fd_array[0] = int(dev)
opt := Options{
FDs: fd_array,
MTU: mtu,
}
endpoint, _ = NewFdbasedInjectableEndpoint(&opt)
return endpoint, nil
}
const nic tcpip.NICID = 0x01
func NewStack(handler GConnHandler, endpoint stack.LinkEndpoint) (*stack.Stack, error) {
var o stack.Options
o = stack.Options{
NetworkProtocols: []stack.NetworkProtocolFactory{
ipv4.NewProtocol,
},
TransportProtocols: []stack.TransportProtocolFactory{
tcp.NewProtocol,
udp.NewProtocol,
icmp.NewProtocol4,
},
}
s := stack.New(o)
s.SetRouteTable([]tcpip.Route{
{
Destination: header.IPv4EmptySubnet,
NIC: nic,
},
{
Destination: header.IPv6EmptySubnet,
NIC: nic,
},
})
// creates a fake nic and attaches netstack to it
assertNoErr(s.CreateNIC(nic, endpoint))
// allow spoofing packets tuples
assertNoErr(s.SetSpoofing(nic, true))
// allow all packets sent to our fake nic through to netstack
assertNoErr(s.SetPromiscuousMode(nic, true))
setupTcpHandler(s, handler.TCP())
setupUdpHandler(s, handler.UDP())
//setupUdpHandler(s, handler)
// setupIcmpHandler(s, endpoint, handler)
return s, nil
}
func assertNoErr(err tcpip.Error) {
if err != nil {
panic(err.String())
}
}