forked from naggie/dsnet
-
Notifications
You must be signed in to change notification settings - Fork 0
/
up.go
75 lines (61 loc) · 1.61 KB
/
up.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
package dsnet
import (
"net"
"github.com/vishvananda/netlink"
)
func Up() {
conf := MustLoadDsnetConfig()
CreateLink(conf)
MustConfigureDevice(conf)
RunPostUp(conf)
}
func RunPostUp(conf *DsnetConfig) {
ShellOut(conf.PostUp, "PostUp")
}
// CreateLink sets up the WG interface and link with the correct
// address
func CreateLink(conf *DsnetConfig) {
if len(conf.IP) == 0 && len(conf.IP6) == 0 {
ExitFail("No IPv4 or IPv6 network defined in config")
}
linkAttrs := netlink.NewLinkAttrs()
linkAttrs.Name = conf.InterfaceName
link := &netlink.GenericLink{
LinkAttrs: linkAttrs,
LinkType: "wireguard",
}
err := netlink.LinkAdd(link)
if err != nil {
FailWithoutExit("Could not add interface '%s' (%v), falling back to the userspace implementation", conf.InterfaceName, err)
ShellOut(WG_USERSPACE_IMPLEMENTATION + " " + conf.InterfaceName, "Userspace implementation")
}
if len(conf.IP) != 0 {
addr := &netlink.Addr{
IPNet: &net.IPNet{
IP: conf.IP,
Mask: conf.Network.IPNet.Mask,
},
}
err = netlink.AddrAdd(link, addr)
if err != nil {
ExitFail("Could not add ipv4 addr %s to interface %s", addr.IP, err)
}
}
if len(conf.IP6) != 0 {
addr6 := &netlink.Addr{
IPNet: &net.IPNet{
IP: conf.IP6,
Mask: conf.Network6.IPNet.Mask,
},
}
err = netlink.AddrAdd(link, addr6)
if err != nil {
ExitFail("Could not add ipv6 addr %s to interface %s", addr6.IP, err)
}
}
// bring up interface (UNKNOWN state instead of UP, a wireguard quirk)
err = netlink.LinkSetUp(link)
if err != nil {
ExitFail("Could not bring up device '%s' (%v)", conf.InterfaceName, err)
}
}