Added support for route protocol and address label

This commit is contained in:
Neven Miculinic 2019-03-28 14:39:45 +01:00
parent 612737e036
commit 5f01824316
2 changed files with 28 additions and 3 deletions

View File

@ -35,6 +35,12 @@ type Config struct {
PreDown string PreDown string
PostDown string PostDown string
// RouteProtocol to set on the route. See linux/rtnetlink.h Use value > 4 or default 0
RouteProtocol int
// Address label to set on the link
AddressLabel string
// SaveConfig — if set to true, the configuration is saved from the current state of the interface upon shutdown. // SaveConfig — if set to true, the configuration is saved from the current state of the interface upon shutdown.
// Currently unsupported // Currently unsupported
SaveConfig bool SaveConfig bool

25
wg.go
View File

@ -200,6 +200,10 @@ func SyncAddress(cfg *Config, link netlink.Link, log logrus.FieldLogger) error {
// nil addr means I've used it // nil addr means I've used it
presentAddresses := make(map[string]*netlink.Addr, 0) presentAddresses := make(map[string]*netlink.Addr, 0)
for _, addr := range addrs { for _, addr := range addrs {
log.WithFields(map[string]interface{}{
"addr": addr.IPNet.String(),
"label": addr.Label,
}).Debugln("found existing address")
presentAddresses[addr.IPNet.String()] = &addr presentAddresses[addr.IPNet.String()] = &addr
} }
@ -213,6 +217,7 @@ func SyncAddress(cfg *Config, link netlink.Link, log logrus.FieldLogger) error {
} }
if err := netlink.AddrAdd(link, &netlink.Addr{ if err := netlink.AddrAdd(link, &netlink.Addr{
IPNet: &addr, IPNet: &addr,
Label: cfg.AddressLabel,
}); err != nil { }); err != nil {
log.WithError(err).Error("cannot add addr") log.WithError(err).Error("cannot add addr")
return err return err
@ -224,7 +229,10 @@ func SyncAddress(cfg *Config, link netlink.Link, log logrus.FieldLogger) error {
if addr == nil { if addr == nil {
continue continue
} }
log := log.WithField("addr", addr.IPNet.String()) log := log.WithFields(map[string]interface{}{
"addr": addr.IPNet.String(),
"label": addr.Label,
})
if err := netlink.AddrDel(link, addr); err != nil { if err := netlink.AddrDel(link, addr); err != nil {
log.WithError(err).Error("cannot delete addr") log.WithError(err).Error("cannot delete addr")
return err return err
@ -244,7 +252,12 @@ func SyncRoutes(cfg *Config, link netlink.Link, log logrus.FieldLogger) error {
presentRoutes := make(map[string]*netlink.Route, 0) presentRoutes := make(map[string]*netlink.Route, 0)
for _, r := range routes { for _, r := range routes {
log := log.WithField("route", r.Dst.String()) log := log.WithFields(map[string]interface{}{
"route": r.Dst.String(),
"protocol": r.Protocol,
"table": r.Table,
"type": r.Type,
})
if r.Table == cfg.Table || (cfg.Table == 0 && r.Table == defaultRoutingTable) { if r.Table == cfg.Table || (cfg.Table == 0 && r.Table == defaultRoutingTable) {
presentRoutes[r.Dst.String()] = &r presentRoutes[r.Dst.String()] = &r
log.WithField("table", r.Table).Debug("detected existing route") log.WithField("table", r.Table).Debug("detected existing route")
@ -266,6 +279,7 @@ func SyncRoutes(cfg *Config, link netlink.Link, log logrus.FieldLogger) error {
LinkIndex: link.Attrs().Index, LinkIndex: link.Attrs().Index,
Dst: &rt, Dst: &rt,
Table: cfg.Table, Table: cfg.Table,
Protocol: cfg.RouteProtocol,
}); err != nil { }); err != nil {
log.WithError(err).Error("cannot setup route") log.WithError(err).Error("cannot setup route")
return err return err
@ -279,7 +293,12 @@ func SyncRoutes(cfg *Config, link netlink.Link, log logrus.FieldLogger) error {
if rt == nil { // skip visited routes if rt == nil { // skip visited routes
continue continue
} }
log := log.WithField("route", rt.Dst.String()) log := log.WithFields(map[string]interface{}{
"route": rt.Dst.String(),
"protocol": rt.Protocol,
"table": rt.Table,
"type": rt.Type,
})
log.Info("extra manual route found") log.Info("extra manual route found")
if err := netlink.RouteDel(rt); err != nil { if err := netlink.RouteDel(rt); err != nil {
log.WithError(err).Error("cannot setup route") log.WithError(err).Error("cannot setup route")