-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtunnel.go
242 lines (187 loc) · 4.42 KB
/
tunnel.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
package l2tp
import (
"fmt"
"net"
"github.com/mdlayher/netlink"
)
type TunnelMessage struct {
command uint8
EncapType *uint16
ProtoVersion *uint8
ConnId *uint32
PeerConnId *uint32
IpSaddr *net.IP
IpDaddr *net.IP
UdpSport *uint16
UdpDport *uint16
Ip6Saddr *net.IP
Ip6Daddr *net.IP
}
func (t *TunnelMessage) Command() uint8 {
return t.command
}
func (t *TunnelMessage) MarshalBinary() ([]byte, error) {
ae := netlink.NewAttributeEncoder()
if t.EncapType != nil {
ae.Uint16(L2TP_ATTR_ENCAP_TYPE, *t.EncapType)
}
if t.ProtoVersion != nil {
ae.Uint8(L2TP_ATTR_PROTO_VERSION, *t.ProtoVersion)
} else {
// Unlikely any other version is desired
ae.Uint8(L2TP_ATTR_PROTO_VERSION, 3)
}
if t.ConnId != nil {
ae.Uint32(L2TP_ATTR_CONN_ID, *t.ConnId)
}
if t.PeerConnId != nil {
ae.Uint32(L2TP_ATTR_PEER_CONN_ID, *t.PeerConnId)
}
if t.IpSaddr != nil {
ip4 := t.IpSaddr.To4()
if ip4 == nil {
return nil, fmt.Errorf("dst addr (%s) is not an ipv4 address", t.IpSaddr)
}
ae.Bytes(L2TP_ATTR_IP_SADDR, ip4)
}
if t.IpDaddr != nil {
ip4 := t.IpSaddr.To4()
if ip4 == nil {
return nil, fmt.Errorf("src addr (%s) is not an ipv4 address", t.IpDaddr)
}
ae.Bytes(L2TP_ATTR_IP_DADDR, *t.IpDaddr)
}
if t.UdpSport != nil {
ae.Uint16(L2TP_ATTR_UDP_SPORT, *t.UdpSport)
}
if t.UdpDport != nil {
ae.Uint16(L2TP_ATTR_UDP_DPORT, *t.UdpDport)
}
if t.Ip6Saddr != nil {
ae.Bytes(L2TP_ATTR_IP6_SADDR, *t.Ip6Saddr)
}
if t.Ip6Daddr != nil {
ae.Bytes(L2TP_ATTR_IP6_DADDR, *t.Ip6Daddr)
}
return ae.Encode()
}
func (t *TunnelMessage) UnmarshalBinary(b []byte) error {
ad, err := netlink.NewAttributeDecoder(b)
if err != nil {
return err
}
for ad.Next() {
switch ad.Type() {
case L2TP_ATTR_ENCAP_TYPE:
v := ad.Uint16()
t.EncapType = &v
case L2TP_ATTR_PROTO_VERSION:
v := ad.Uint8()
t.ProtoVersion = &v
case L2TP_ATTR_CONN_ID:
v := ad.Uint32()
t.ConnId = &v
case L2TP_ATTR_PEER_CONN_ID:
v := ad.Uint32()
t.PeerConnId = &v
case L2TP_ATTR_IP6_SADDR:
v := net.IP(ad.Bytes())
t.Ip6Saddr = &v
case L2TP_ATTR_IP6_DADDR:
v := net.IP(ad.Bytes())
t.Ip6Daddr = &v
case L2TP_ATTR_IP_SADDR:
v := net.IP(ad.Bytes())
t.IpSaddr = &v
case L2TP_ATTR_IP_DADDR:
v := net.IP(ad.Bytes())
t.IpDaddr = &v
case L2TP_ATTR_UDP_SPORT:
v := ad.Uint16()
t.UdpSport = &v
case L2TP_ATTR_UDP_DPORT:
v := ad.Uint16()
t.UdpDport = &v
}
}
return nil
}
type TunnelService struct {
c *Conn
}
func (t *TunnelService) Add(tun *TunnelMessage) error {
tun.command = L2TP_CMD_TUNNEL_CREATE
_, err := t.c.Execute(tun, t.c.genFamily.ID, netlink.Request|netlink.Acknowledge)
if err != nil {
return err
}
return nil
}
func (t *TunnelService) Delete(tun *TunnelMessage) error {
tun.command = L2TP_CMD_TUNNEL_DELETE
_, err := t.c.Execute(tun, t.c.genFamily.ID, netlink.Request|netlink.Acknowledge)
if err != nil {
return err
}
return nil
}
func (t *TunnelService) List() ([]TunnelMessage, error) {
req := &TunnelMessage{
command: L2TP_CMD_TUNNEL_GET,
}
resp, err := t.c.Execute(req, t.c.genFamily.ID, netlink.Request|netlink.Dump)
if err != nil {
return []TunnelMessage{}, err
}
tunnels := make([]TunnelMessage, len(resp))
for i, t := range resp {
tunnels[i] = *(t).(*TunnelMessage)
}
return tunnels, nil
}
func (t *TunnelService) Get(tun *TunnelMessage) ([]TunnelMessage, error) {
tunnels, err := t.List()
if err != nil {
return nil, err
}
result := make([]TunnelMessage, 0, len(tunnels))
for _, t := range tunnels {
if tunnelFilterMatch(tun, &t) {
result = append(result, t)
}
}
return result, nil
}
func tunnelFilterMatch(f, t *TunnelMessage) bool {
if f.EncapType != nil && *f.EncapType != *t.EncapType {
return false
}
if f.ProtoVersion != nil && *f.ProtoVersion != *t.ProtoVersion {
return false
}
if f.ConnId != nil && *f.ConnId != *t.ConnId {
return false
}
if f.PeerConnId != nil && *f.PeerConnId != *t.PeerConnId {
return false
}
if f.IpSaddr != nil && !f.IpSaddr.Equal(*t.IpSaddr) {
return false
}
if f.IpDaddr != nil && !f.IpDaddr.Equal(*t.IpDaddr) {
return false
}
if f.UdpSport != nil && *f.UdpSport != *t.UdpSport {
return false
}
if f.UdpDport != nil && *f.UdpDport != *t.UdpDport {
return false
}
if f.Ip6Saddr != nil && !f.Ip6Saddr.Equal(*t.Ip6Saddr) {
return false
}
if f.Ip6Daddr != nil && !f.Ip6Daddr.Equal(*t.Ip6Daddr) {
return false
}
return true
}