Set ip if configured, otherwise just bring up link

This commit is contained in:
Martin Eskdale Moen 2020-12-20 15:27:02 +00:00
parent e719f111f1
commit 7aa7548df6

44
up.go
View File

@ -17,6 +17,8 @@ func RunPostUp(conf *DsnetConfig) {
ShellOut(conf.PostUp, "PostUp")
}
// CreateLink sets up the WG interface and link with the correct
// address
func CreateLink(conf *DsnetConfig) {
linkAttrs := netlink.NewLinkAttrs()
linkAttrs.Name = conf.InterfaceName
@ -31,28 +33,32 @@ func CreateLink(conf *DsnetConfig) {
ExitFail("Could not add interface '%s' (%v)", conf.InterfaceName, err)
}
addr := &netlink.Addr{
IPNet: &net.IPNet{
IP: conf.IP,
Mask: conf.Network.IPNet.Mask,
},
if conf.IP != nil {
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)
}
}
err = netlink.AddrAdd(link, addr)
if err != nil {
ExitFail("Could not add addr %s to interface %s", addr.IP, err)
}
if conf.IP6 != nil {
addr6 := &netlink.Addr{
IPNet: &net.IPNet{
IP: conf.IP6,
Mask: conf.Network6.IPNet.Mask,
},
}
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 addr %s to interface %s", addr.IP, err)
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)