-
Notifications
You must be signed in to change notification settings - Fork 107
/
iface_linux.go
97 lines (78 loc) · 1.95 KB
/
iface_linux.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
// +build linux
package main
import (
"log"
"net"
"github.com/kanocz/lcvpn/netlink"
"github.com/milosgajdos83/tenus"
"github.com/songgao/water"
)
const (
// MTU used for tunneled packets
MTU = 1300
)
// ifaceSetup returns new interface OR PANIC!
func ifaceSetup(localCIDR string) *water.Interface {
lIP, lNet, err := net.ParseCIDR(localCIDR)
if nil != err {
log.Fatalln("\nlocal ip is not in ip/cidr format")
panic("invalid local ip")
}
iface, err := water.New(water.Config{DeviceType: water.TUN})
if nil != err {
log.Println("Unable to allocate TUN interface:", err)
panic(err)
}
log.Println("Interface allocated:", iface.Name())
link, err := tenus.NewLinkFrom(iface.Name())
if nil != err {
log.Fatalln("Unable to get interface info", err)
}
err = link.SetLinkMTU(MTU)
if nil != err {
log.Fatalln("Unable to set MTU to 1300 on interface")
}
err = link.SetLinkIp(lIP, lNet)
if nil != err {
log.Fatalln("Unable to set IP to ", lIP, "/", lNet, " on interface")
}
err = link.SetLinkUp()
if nil != err {
log.Fatalln("Unable to UP interface")
}
return iface
}
func routesThread(ifaceName string, refresh chan bool) {
currentRoutes := map[string]bool{}
for {
<-refresh
log.Println("Reloading routes...")
conf := config.Load().(VPNState)
routes2Del := map[string]bool{}
for r := range currentRoutes {
routes2Del[r] = true
}
for r := range conf.routes {
rs := r.String()
if _, exist := routes2Del[rs]; exist {
delete(routes2Del, rs)
} else {
// real add route
currentRoutes[rs] = true
log.Println("Adding route:", rs)
err := netlink.AddRoute(rs, "", "", ifaceName)
if nil != err {
log.Println("Adding route", rs, "failed:", err)
}
}
}
for r := range routes2Del {
delete(currentRoutes, r)
log.Println("Removing route:", r)
err := netlink.DelRoute(r, "", "", ifaceName)
if nil != err {
log.Printf("Error removeing route \"%s\": %s", r, err.Error())
}
}
}
}