@@ -2,9 +2,10 @@ package tunnel
22
33import (
44 "fmt"
5- "strconv "
5+ "os "
66
77 "github.com/sirupsen/logrus"
8+ "github.com/vishvananda/netlink"
89)
910
1011// Create and open tunnel interface.
@@ -14,7 +15,7 @@ import (
1415// Accept external IP address as a string.
1516// Return nil if interface opened successfully, error otherwise.
1617func (conf * TunnelConfig ) openInterface (extIP string ) error {
17- // Cast sunnel name, ip and CIDR to string
18+ // Cast tunnel name, ip and CIDR to string
1819 tunnelName := conf .Tunnel .Name ()
1920 tunnelString := conf .IP .String ()
2021 tunnelCIDR , _ := conf .Network .Mask .Size ()
@@ -27,28 +28,38 @@ func (conf *TunnelConfig) openInterface(extIP string) error {
2728 }
2829 conf .mtu = int32 (tunnelInterface .MTU )
2930 }
30- tunnelMTU := strconv .FormatInt (int64 (conf .mtu ), 10 )
3131
32- // Setup tunnel interface MTU
33- _ , err := runCommand ( "ip" , "link" , "set" , "dev" , tunnelName , "mtu" , tunnelMTU )
32+ // Lookup tunnel link by name
33+ link , err := netlink . LinkByName ( tunnelName )
3434 if err != nil {
35+ return fmt .Errorf ("could not get link %s: %v" , tunnelName , err )
36+ }
37+
38+ // Setup tunnel interface MTU
39+ if err := netlink .LinkSetMTU (link , int (conf .mtu )); err != nil {
3540 return fmt .Errorf ("error setting tunnel MTU: %v" , err )
3641 }
3742
38- // Setup IP address for tunnel interface
39- _ , err = runCommand ( "ip" , "addr" , "add" , fmt .Sprintf ("%s/%d" , tunnelString , tunnelCIDR ), "dev" , tunnelName )
43+ // Parse tunnel IP and CIDR
44+ addr , err := netlink . ParseAddr ( fmt .Sprintf ("%s/%d" , tunnelString , tunnelCIDR ))
4045 if err != nil {
41- return fmt .Errorf ("error setting tunnel IP address: %v" , err )
46+ return fmt .Errorf ("invalid tunnel address: %v" , err )
4247 }
4348
44- // Enable tunnel interfaces
45- _ , err = runCommand ("ip" , "link" , "set" , "dev" , tunnelName , "up" )
46- if err != nil {
49+ // Setup IP address for tunnel interface
50+ if err := netlink .AddrAdd (link , addr ); err != nil {
51+ if ! os .IsExist (err ) {
52+ return fmt .Errorf ("error adding tunnel address: %v" , err )
53+ }
54+ }
55+
56+ // Enable tunnel interface
57+ if err := netlink .LinkSetUp (link ); err != nil {
4758 return fmt .Errorf ("error setting tunnel UP: %v" , err )
4859 }
4960
5061 // Log and return no error
51- logrus .Infof ("Interface %s opened (IP: %s, MTU: %s )" , tunnelName , tunnelString , tunnelMTU )
62+ logrus .Infof ("Interface %s opened (IP: %s, MTU: %d )" , tunnelName , tunnelString , conf . mtu )
5263 return nil
5364}
5465
@@ -59,14 +70,18 @@ func (conf *TunnelConfig) closeInterface() error {
5970 // Receive tunnel name
6071 tunnelName := conf .Tunnel .Name ()
6172
62- // Disable and remove tunnel
63- _ , err := runCommand ( "ip" , "link" , "set" , "dev" , tunnelName , "down" )
73+ // Lookup tunnel link by name
74+ link , err := netlink . LinkByName ( tunnelName )
6475 if err != nil {
76+ return fmt .Errorf ("could not get link %s: %v" , tunnelName , err )
77+ }
78+
79+ // Disable and remove tunnel
80+ if err := netlink .LinkSetDown (link ); err != nil {
6581 return fmt .Errorf ("error shutting down tunnel interface: %v" , err )
6682 }
6783
68- _ , err = runCommand ("ip" , "link" , "del" , "dev" , tunnelName )
69- if err != nil {
84+ if err := netlink .LinkDel (link ); err != nil {
7085 return fmt .Errorf ("error deleting tunnel interface: %v" , err )
7186 }
7287
0 commit comments