2020-03-04 20:38:48 +01:00
|
|
|
package dsnet
|
|
|
|
|
|
|
|
import (
|
|
|
|
"net"
|
|
|
|
|
|
|
|
"github.com/vishvananda/netlink"
|
|
|
|
)
|
|
|
|
|
|
|
|
func Up() {
|
|
|
|
conf := MustLoadDsnetConfig()
|
2020-03-04 23:01:30 +01:00
|
|
|
CreateLink(conf)
|
|
|
|
ConfigureDevice(conf)
|
2020-11-18 23:37:32 +01:00
|
|
|
RunPostUp(conf)
|
|
|
|
}
|
|
|
|
|
|
|
|
func RunPostUp(conf *DsnetConfig) {
|
|
|
|
ShellOut(conf.PostUp, "PostUp")
|
2020-03-04 20:38:48 +01:00
|
|
|
}
|
|
|
|
|
2020-12-20 16:27:02 +01:00
|
|
|
// CreateLink sets up the WG interface and link with the correct
|
|
|
|
// address
|
2020-03-04 23:01:30 +01:00
|
|
|
func CreateLink(conf *DsnetConfig) {
|
2020-03-04 20:38:48 +01:00
|
|
|
linkAttrs := netlink.NewLinkAttrs()
|
|
|
|
linkAttrs.Name = conf.InterfaceName
|
|
|
|
|
2020-03-04 20:56:57 +01:00
|
|
|
link := &netlink.GenericLink{
|
2020-03-04 20:38:48 +01:00
|
|
|
LinkAttrs: linkAttrs,
|
|
|
|
LinkType: "wireguard",
|
|
|
|
}
|
|
|
|
|
2020-03-04 20:56:57 +01:00
|
|
|
err := netlink.LinkAdd(link)
|
2020-03-04 20:38:48 +01:00
|
|
|
if err != nil {
|
2020-03-04 22:09:12 +01:00
|
|
|
ExitFail("Could not add interface '%s' (%v)", conf.InterfaceName, err)
|
2020-03-04 20:38:48 +01:00
|
|
|
}
|
|
|
|
|
2020-12-20 16:27:02 +01:00
|
|
|
if conf.IP != nil {
|
|
|
|
addr := &netlink.Addr{
|
|
|
|
IPNet: &net.IPNet{
|
|
|
|
IP: conf.IP,
|
|
|
|
Mask: conf.Network.IPNet.Mask,
|
|
|
|
},
|
|
|
|
}
|
2020-03-04 20:38:48 +01:00
|
|
|
|
2020-12-20 16:27:02 +01:00
|
|
|
err = netlink.AddrAdd(link, addr)
|
|
|
|
if err != nil {
|
|
|
|
ExitFail("Could not add ipv4 addr %s to interface %s", addr.IP, err)
|
|
|
|
}
|
2020-03-04 20:38:48 +01:00
|
|
|
}
|
|
|
|
|
2020-12-20 16:27:02 +01:00
|
|
|
if conf.IP6 != nil {
|
|
|
|
addr6 := &netlink.Addr{
|
|
|
|
IPNet: &net.IPNet{
|
|
|
|
IP: conf.IP6,
|
|
|
|
Mask: conf.Network6.IPNet.Mask,
|
|
|
|
},
|
|
|
|
}
|
2020-10-30 13:34:35 +01:00
|
|
|
|
2020-12-20 16:27:02 +01:00
|
|
|
err = netlink.AddrAdd(link, addr6)
|
|
|
|
if err != nil {
|
|
|
|
ExitFail("Could not add ipv6 addr %s to interface %s", addr6.IP, err)
|
|
|
|
}
|
2020-10-30 13:34:35 +01:00
|
|
|
}
|
|
|
|
|
2020-03-04 23:01:30 +01:00
|
|
|
// bring up interface (UNKNOWN state instead of UP, a wireguard quirk)
|
|
|
|
err = netlink.LinkSetUp(link)
|
2020-03-04 23:04:13 +01:00
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
ExitFail("Could not bring up device '%s' (%v)", conf.InterfaceName, err)
|
|
|
|
}
|
2020-03-04 23:01:30 +01:00
|
|
|
}
|