Skip to content

Commit

Permalink
Accept existing XMRF policies and update them intead of raising errors
Browse files Browse the repository at this point in the history
  • Loading branch information
txomon committed Aug 21, 2020
1 parent d5c4faf commit e7682f1
Showing 1 changed file with 17 additions and 6 deletions.
23 changes: 17 additions & 6 deletions backend/ipsec/handle_xfrm.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,10 @@
package ipsec

import (
"errors"
"fmt"
"net"
"syscall"

log "github.com/golang/glog"
"github.com/vishvananda/netlink"
Expand All @@ -30,7 +32,7 @@ func AddXFRMPolicy(myLease, remoteLease *subnet.Lease, dir netlink.Dir, reqID in

dst := remoteLease.Subnet.ToIPNet()

policy := netlink.XfrmPolicy{
policy := &netlink.XfrmPolicy{
Src: src,
Dst: dst,
Dir: dir,
Expand All @@ -47,14 +49,23 @@ func AddXFRMPolicy(myLease, remoteLease *subnet.Lease, dir netlink.Dir, reqID in
Reqid: reqID,
}

log.Infof("Adding ipsec policy: %+v", tmpl)

policy.Tmpls = append(policy.Tmpls, tmpl)

if err := netlink.XfrmPolicyAdd(&policy); err != nil {
return fmt.Errorf("error adding policy: %+v err: %v", policy, err)
if existingPolicy, err := netlink.XfrmPolicyGet(policy); err != nil {
if errors.Is(err, syscall.ENOENT) {
log.Infof("Adding ipsec policy: %+v", tmpl)
if err := netlink.XfrmPolicyAdd(policy); err != nil {
return fmt.Errorf("error adding policy: %+v err: %v", policy, err)
}
} else {
return fmt.Errorf("error getting policy: %+v err: %v", policy, err)
}
} else {
log.Info("Updating ipsec policy %+v with %+v", existingPolicy, policy)
if err := netlink.XfrmPolicyUpdate(policy); err != nil {
return fmt.Errorf("error updating policy: %+v err: %v", policy, err)
}
}

return nil
}

Expand Down

0 comments on commit e7682f1

Please sign in to comment.