-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlinkaction.go
307 lines (283 loc) · 7.47 KB
/
linkaction.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
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
package neslink
// TODO: Document all link actions
// TODO: Link actions for adding/deleting routes
import (
"errors"
"fmt"
"net"
"github.com/vishvananda/netlink"
)
// LinkAction is a singular operation that can be performed on a generic netlink
// link. Actions have a name as to identify individual actions when passed as a
// set to a LinkDo call, providing more contextual errors. They also have a
// function that take a link as a parameter. When called, the function will
// perform the operation on the provided link, returning an error if any
// occurred. These do support being executed outside of LinkDo calls, but
// using LinkDo is still recommended.
type LinkAction struct {
actionName string
f func() error
}
// ActionName returns the name associated with the given link action.
func (la LinkAction) ActionName() string {
return la.actionName
}
// name simply returns the name of the link action.
func (la LinkAction) name() string {
return la.actionName
}
// act will perform the link operation immediately.
func (la LinkAction) act() error {
return la.f()
}
// LAGeneric allows for a custom LinkAction to be created and then used in a
// LinkDo call.
func LAGeneric(actionName string, provider LinkProvider, function func() error) LinkAction {
if actionName == "" {
actionName = "unnamed-link-action"
}
return LinkAction{
actionName: actionName,
f: function,
}
}
// LANewBridge creates a new bridge with the given name.
func LANewBridge(name string) LinkAction {
return LinkAction{
actionName: "new-bridge",
f: func() error {
bridge := netlink.Bridge{
LinkAttrs: netlink.NewLinkAttrs(),
}
bridge.LinkAttrs.Name = name
return netlink.LinkAdd(&bridge)
},
}
}
// LANewVeth will create a new veth pair. The names for both the new interfaces
// (main link and peer) should be provided.
func LANewVeth(name, peerName string) LinkAction {
return LinkAction{
actionName: "new-veth",
f: func() error {
veth := netlink.Veth{
LinkAttrs: netlink.NewLinkAttrs(),
PeerName: peerName,
}
veth.LinkAttrs.Name = name
return netlink.LinkAdd(&veth)
},
}
}
// LANewDummy creates a new dummy link with the given name.
func LANewDummy(name string) LinkAction {
return LinkAction{
actionName: "new-dummy",
f: func() error {
dummy := netlink.Dummy{
LinkAttrs: netlink.NewLinkAttrs(),
}
dummy.LinkAttrs.Name = name
return netlink.LinkAdd(&dummy)
},
}
}
// LANewGRETap creates a new gretap device with the given name, local IP, and
// remoteIP.
func LANewGRETap(name, localIP, remoteIP string) LinkAction {
return LinkAction{
actionName: "new-gretap",
f: func() error {
local := net.ParseIP(localIP)
if local == nil {
return fmt.Errorf("failed to parse the local ip address of the gretap")
}
remote := net.ParseIP(remoteIP)
if remote == nil {
return fmt.Errorf("failed to parse the remote ip address of the gretap")
}
gre := netlink.Gretap{
LinkAttrs: netlink.NewLinkAttrs(),
Local: local,
Remote: remote,
}
gre.LinkAttrs.Name = name
return netlink.LinkAdd(&gre)
},
}
}
// LANewWireguard creates a new wireguard link with the given name. Further
// setup of this link should be done in custom LinkActions with wireguard
// specifc code.
func LANewWireguard(name string) LinkAction {
return LinkAction{
actionName: "new-wireguard",
f: func() error {
wg := netlink.Wireguard{
LinkAttrs: netlink.NewLinkAttrs(),
}
wg.LinkAttrs.Name = name
return netlink.LinkAdd(&wg)
},
}
}
// LANewVxlan creates a new vxlan link with the given configuration.
func LANewVxlan(name, localIP, groupIP string, id, port int) LinkAction {
return LinkAction{
actionName: "new-vxlan",
f: func() error {
local := net.ParseIP(localIP)
if local == nil {
return fmt.Errorf("failed to parse the local ip address of the vxlan")
}
group := net.ParseIP(groupIP)
if group == nil {
return fmt.Errorf("failed to parse the group ip address of the vxlan")
}
vx := netlink.Vxlan{
LinkAttrs: netlink.NewLinkAttrs(),
VxlanId: id,
SrcAddr: local,
Group: group,
Port: port,
}
vx.LinkAttrs.Name = name
return netlink.LinkAdd(&vx)
},
}
}
// LADelete will simply delete the link when the action is executed. For obvious
// reasons this should be at the end of any LinkDo call (since the link will be
// deleted, further actions will error).
func LADelete(provider LinkProvider) LinkAction {
return LinkAction{
actionName: "delete-link",
f: func() error {
if l, err := provider.Provide(); err != nil {
return errors.Join(errNoLink, err)
} else {
return netlink.LinkDel(l)
}
},
}
}
func LASetName(provider LinkProvider, name string) LinkAction {
return LinkAction{
actionName: "set-name",
f: func() error {
if l, err := provider.Provide(); err != nil {
return errors.Join(errNoLink, err)
} else {
return netlink.LinkSetName(l, name)
}
},
}
}
func LASetAlias(provider LinkProvider, alias string) LinkAction {
return LinkAction{
actionName: "set-alias",
f: func() error {
if l, err := provider.Provide(); err != nil {
return errors.Join(errNoLink, err)
} else {
return netlink.LinkSetAlias(l, alias)
}
},
}
}
func LASetHw(provider LinkProvider, addr string) LinkAction {
return LinkAction{
actionName: "set-hw",
f: func() error {
if l, err := provider.Provide(); err != nil {
return errors.Join(errNoLink, err)
} else {
hwAddr, err := net.ParseMAC(addr)
if err != nil {
return err
}
return netlink.LinkSetHardwareAddr(l, hwAddr)
}
},
}
}
func LASetUp(provider LinkProvider) LinkAction {
return LinkAction{
actionName: "set-state-up",
f: func() error {
if l, err := provider.Provide(); err != nil {
return errors.Join(errNoLink, err)
} else {
return netlink.LinkSetUp(l)
}
},
}
}
func LASetDown(provider LinkProvider) LinkAction {
return LinkAction{
actionName: "set-state-down",
f: func() error {
if l, err := provider.Provide(); err != nil {
return errors.Join(errNoLink, err)
} else {
return netlink.LinkSetDown(l)
}
},
}
}
func LASetPromiscOn(provider LinkProvider) LinkAction {
return LinkAction{
actionName: "set-promisc-on",
f: func() error {
if l, err := provider.Provide(); err != nil {
return errors.Join(errNoLink, err)
} else {
return netlink.SetPromiscOn(l)
}
},
}
}
func LASetPromiscOff(provider LinkProvider) LinkAction {
return LinkAction{
actionName: "set-promisc-off",
f: func() error {
if l, err := provider.Provide(); err != nil {
return errors.Join(errNoLink, err)
} else {
return netlink.SetPromiscOff(l)
}
},
}
}
func LAAddAddr(provider LinkProvider, cidr string) LinkAction {
return LinkAction{
actionName: "add-address",
f: func() error {
if l, err := provider.Provide(); err != nil {
return errors.Join(errNoLink, err)
} else {
addr, err := netlink.ParseAddr(cidr)
if err != nil {
return fmt.Errorf("failed to parse cidr to network address: %w", err)
}
return netlink.AddrAdd(l, addr)
}
},
}
}
func LADelAddr(provider LinkProvider, cidr string) LinkAction {
return LinkAction{
actionName: "del-address",
f: func() error {
if l, err := provider.Provide(); err != nil {
return errors.Join(errNoLink, err)
} else {
addr, err := netlink.ParseAddr(cidr)
if err != nil {
return fmt.Errorf("failed to parse cidr to network address: %w", err)
}
return netlink.AddrDel(l, addr)
}
},
}
}