-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathlink.go
More file actions
402 lines (371 loc) · 10.6 KB
/
Copy pathlink.go
File metadata and controls
402 lines (371 loc) · 10.6 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
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
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
/*
Copyright 2019 Yoichi Hariguchi
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package iproute
import (
"fmt"
"github.com/vishvananda/netlink"
"github.com/vishvananda/netlink/nl"
"net"
)
// LinkDel deletes the specified link device (interface.)
// in: name Name of the link device (interface) to be removed
// return: nil if success
// non-nil otherwise
func LinkDel(name string) error {
if l, err := netlink.LinkByName(name); err == nil {
return netlink.LinkDel(l)
} else {
return err
}
}
// IfIndex returns the ifindex associated with interface `name'
// in: name Interface name
// return: 1. Ifindex (>0) of interface `name' if success
// -1 otherwise
// 2. nil if success
// non-nil otherwise
func IfIndex(name string) (int, error) {
if l, err := netlink.LinkByName(name); err == nil {
return l.Attrs().Index, nil
} else {
return -1, err
}
}
// IfName retuns the interface name whose ifindex is `ifIndex'
// in: ifIndex Ifindex for the interface
// return: 1. Interface name whose ifindex is `ifIndex' if success
// Empty string otherwise
// 2. nil if success
// non-nil otherwise
func IfName(ifIndex int) (string, error) {
if l, err := netlink.LinkByIndex(ifIndex); err == nil {
return l.Attrs().Name, nil
} else {
return "", err
}
}
// LinkByIndex returns Link instance whose ifindex is `ifIndex'
// in: ifIndex Ifindex for the interface
// return: 1. Link instance for the instance whose if success
// Undetermined Link instance otherwise
func LinkByIndex(ifIndex int) (Link, error) {
return netlink.LinkByIndex(ifIndex)
}
// LinkByIndex returns Link instance whose name is `name'
// in: ifIndex Ifindex for the interface
// return: 1. Link instance for the instance whose if success
// Undetermined Link instance otherwise
func LinkByName(name string) (Link, error) {
return netlink.LinkByName(name)
}
// IfUpByName brings up the specified interface
// in: name Interface name
// return: nil if success
// non-nil otherwise
func IfUpByName(name string) error {
if l, err := netlink.LinkByName(name); err == nil {
return netlink.LinkSetUp(l)
} else {
return err
}
}
// IfUpByName brings down the specified interface
// in: name Interface name
// return: nil if success
// non-nil otherwise
func IfDownByName(name string) error {
if l, err := netlink.LinkByName(name); err == nil {
return netlink.LinkSetDown(l)
} else {
return err
}
}
// IfIsUpByName returns true if the specified interface is up
// in: name Interface name
// return: 1. true if interface status is up
// false otherwise
// 2. nil if success
// non-nil otherwise
func IfIsUpByName(name string) (bool, error) {
if l, err := netlink.LinkByName(name); err == nil {
if l.Attrs().Flags&net.FlagUp != 0 {
return true, nil
} else {
return false, nil
}
} else {
return false, err
}
}
// IfRename changes the name of the specified interface.
// This function brings down the interface momentarily
// in: oldName original name of the interface
// newName new name of the interface
// return: nil if success
// non-nil otherwise
func IfRename(oldName, newName string) error {
errMsg := fmt.Sprintf("Error: IfRename(%s, %s): ", oldName, newName)
if l, err := netlink.LinkByName(oldName); err == nil {
var linkUp bool
if l.Attrs().Flags&net.FlagUp == 0 {
linkUp = false
} else {
//
// link is up. bring it down first.
//
linkUp = true
if err := netlink.LinkSetDown(l); err != nil {
return fmt.Errorf("IfRename(%s): LinkSetName(%s): %v",
oldName, newName, err)
}
}
//
// link is down now. Rename the interface
//
if err := netlink.LinkSetName(l, newName); err == nil {
if linkUp {
//
// Bring up the link again
//
if err := netlink.LinkSetUp(l); err != nil {
return fmt.Errorf(errMsg+"LinkSetUp(): %v", err)
}
}
return nil
} else {
if linkUp {
//
// Bring up the link again
//
if err := netlink.LinkSetUp(l); err != nil {
return fmt.Errorf(errMsg+"LinkSetUp(): %v", err)
}
}
return fmt.Errorf(errMsg+"LinkSetName(): %v", err)
}
} else {
return fmt.Errorf(errMsg+"LinkByName(): %v", err)
}
}
// IfUnbind unbinds an interface from the master device
// in: ifName Name of interface to be unbound
// return: nil if success
// non-nil otherwise
func IfUnbind(ifName string) error {
if l, err := netlink.LinkByName(ifName); err == nil {
return netlink.LinkSetNoMaster(l)
} else {
return err
}
}
// IfDelete deletes the interface whose name is `name'
// in: name Name of an interface
// return: nil if success
// non-nil otherwise
func IfDelete(name string) error {
return LinkDel(name)
}
func IfExists(name string) (bool, error) {
return ifExists(name, nil)
}
func ifExists(name string, kind netlink.Link) (bool, error) {
l, err := LinkByName(name)
if err == nil {
if kind == nil {
return true, nil
}
if l.Type() == kind.Type() {
return true, nil
}
return false, nil
} else {
if IsNotFound(err) {
return false, nil
}
return false, err
}
}
// IsTunnelByIndex returns true if theh specified interface is a
// tunnel interface.
// in: ifindex Ifindex of the interface to test
// return: 1. true if the interface is a tunnel interface
// false otherwise
// 2. nil if success
// non-nil otherwise
func IsTunnelByIndex(ifindex int) (bool, error) {
if link, err := netlink.LinkByIndex(ifindex); err == nil {
if link.Type() == "tun" {
return true, nil
} else {
return false, nil
}
} else {
return false, err
}
}
// IsTunnelByName returns true if theh specified interface is a
// tunnel interface.
// in: name Name of the interface to test
// return: 1. true if the interface is a tunnel interface
// false otherwise
// 2. nil if success
// non-nil otherwise
func IsTunnelByName(name string) (bool, error) {
if link, err := netlink.LinkByName(name); err == nil {
if link.Type() == "tun" {
return true, nil
} else {
return false, nil
}
} else {
return false, err
}
}
// IpAddrAdd adds an IP prefix to an interface
// in: name Interface name
// addr IP prefix (IPv4 or IPv6)
// up Bring up `name' if true
// Do nothing otherwise
// return: nil if success
// non-nil otherwise
func IpAddrAdd(name string, addr *net.IPNet, up bool) error {
if l, err := netlink.LinkByName(name); err == nil {
if err := netlink.AddrAdd(l, &netlink.Addr{IPNet: addr}); err != nil {
return err
}
if up {
return netlink.LinkSetUp(l)
}
return nil
} else {
return err
}
}
// IpAddrDelete deletes an IP prefix from an interface
// in: `name': interface name
// `addr': IP prefix (IPv4 or IPv6)
// return: nil if success
// non-nil otherwise
func IpAddrDelete(name string, addr *net.IPNet) error {
if l, err := netlink.LinkByName(name); err == nil {
return netlink.AddrDel(l, &netlink.Addr{IPNet: addr})
} else {
return err
}
}
// IpAddrReplace replaces (or adds unless present) an IP prefix
// on an interface
// in: name Interface name
// addr IP prefix (IPv4 or IPv6)
// up Bring up `name' if true
// Do nothing otherwise
// return: nil if success
// non-nil otherwise
func IpAddrReplace(name string, addr *net.IPNet, up bool) error {
if l, err := netlink.LinkByName(name); err == nil {
if err :=
netlink.AddrReplace(l, &netlink.Addr{IPNet: addr}); err != nil {
return err
}
if up {
return netlink.LinkSetUp(l)
}
return nil
} else {
return err
}
}
// IpAddrList returns a list of IP prefixes associated with the interface
// in: `name': interface name
// `family': FAMILY_ALL, FAMILY_V4, FAMILY_V6, or FAMILY_MPLS
// return: 1. slice of *net.IPNet if success
// nil otherwise
// 2. nil if success
// non-nil otherwise
func IpAddrList(name string, family int) ([]*net.IPNet, error) {
var rc []*net.IPNet
if l, err := netlink.LinkByName(name); err == nil {
if addr, err := netlink.AddrList(l, family); err == nil {
for _, a := range addr {
rc = append(rc, a.IPNet)
}
return rc, nil
} else {
return nil, err
}
} else {
return nil, err
}
}
// Ipv4AddrList returns a list of IPv4 prefixes associated with the interface
// in: `name': interface name
// return: 1. slice of *net.IPNet if success
// nil otherwise
// 2. nil if success
// non-nil otherwise
func IPv4AddrList(name string) ([]*net.IPNet, error) {
return IpAddrList(name, nl.FAMILY_V4)
}
// Ipv6AddrList returns a list of IPv6 prefixes associated with the interface
// in: `name': interface name
// return: 1. slice of *net.IPNet if success
// nil otherwise
// 2. nil if success
// non-nil otherwise
func IPv6AddrList(name string) ([]*net.IPNet, error) {
return IpAddrList(name, nl.FAMILY_V6)
}
// IsIfPrefix returns true if `ifPrefix' belongs to interface `ifName'
// in: ifName Interface name
// ifPrefix Pointer to net.IPNet representing the prefix to be tested
// return: 1. true if `ifPrefix' belongs to `ifName'
// false otherwise
// 2. nil if success
// non-nil otherwise
func IsIfPrefix(ifName string, ifPrefix *net.IPNet) (bool, error) {
if prefixes, err := IpAddrList(ifName, nl.FAMILY_ALL); err == nil {
for _, pfx := range prefixes {
if IPNetEqual(pfx, ifPrefix) == true {
return true, nil
}
}
return false, err
} else {
return false, err
}
}
// IsIfPrefixByName returns true if `ifPrefix' belongs to interface `ifName'
// in: ifName Interface name
// ifPrefix IP prefix to be tested
// return: 1. true if `ifPrefix' belongs to `ifName'
// false otherwise
// 2. nil if success
// non-nil otherwise
func IsIfPrefixByName(ifName, ifPrefix string) (bool, error) {
if _, addr, err := net.ParseCIDR(ifPrefix); err == nil {
return IsIfPrefix(ifName, addr)
} else {
return false, err
}
}
func IfList() ([]string, error) {
var ifs []string
ll, err := netlink.LinkList()
if err != nil {
return nil, fmt.Errorf("LinkList(): %v", err)
}
for _, l := range ll {
ifs = append(ifs, l.Attrs().Name)
}
return ifs, nil
}