handle_xfrm.go 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. // Copyright 2017 flannel authors
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. // +build !windows
  15. package ipsec
  16. import (
  17. "errors"
  18. "fmt"
  19. "net"
  20. "syscall"
  21. "github.com/coreos/flannel/subnet"
  22. "github.com/vishvananda/netlink"
  23. log "k8s.io/klog"
  24. )
  25. func AddXFRMPolicy(myLease, remoteLease *subnet.Lease, dir netlink.Dir, reqID int) error {
  26. src := myLease.Subnet.ToIPNet()
  27. dst := remoteLease.Subnet.ToIPNet()
  28. policy := &netlink.XfrmPolicy{
  29. Src: src,
  30. Dst: dst,
  31. Dir: dir,
  32. }
  33. tunnelLeft := myLease.Attrs.PublicIP.ToIP()
  34. tunnelRight := remoteLease.Attrs.PublicIP.ToIP()
  35. tmpl := netlink.XfrmPolicyTmpl{
  36. Src: tunnelLeft,
  37. Dst: tunnelRight,
  38. Proto: netlink.XFRM_PROTO_ESP,
  39. Mode: netlink.XFRM_MODE_TUNNEL,
  40. Reqid: reqID,
  41. }
  42. policy.Tmpls = append(policy.Tmpls, tmpl)
  43. if existingPolicy, err := netlink.XfrmPolicyGet(policy); err != nil {
  44. if errors.Is(err, syscall.ENOENT) {
  45. log.Infof("Adding ipsec policy: %+v", tmpl)
  46. if err := netlink.XfrmPolicyAdd(policy); err != nil {
  47. return fmt.Errorf("error adding policy: %+v err: %v", policy, err)
  48. }
  49. } else {
  50. return fmt.Errorf("error getting policy: %+v err: %v", policy, err)
  51. }
  52. } else {
  53. log.Info("Updating ipsec policy %+v with %+v", existingPolicy, policy)
  54. if err := netlink.XfrmPolicyUpdate(policy); err != nil {
  55. return fmt.Errorf("error updating policy: %+v err: %v", policy, err)
  56. }
  57. }
  58. return nil
  59. }
  60. func DeleteXFRMPolicy(localSubnet, remoteSubnet *net.IPNet, localPublicIP, remotePublicIP net.IP, dir netlink.Dir, reqID int) error {
  61. src := localSubnet
  62. dst := remoteSubnet
  63. policy := netlink.XfrmPolicy{
  64. Src: src,
  65. Dst: dst,
  66. Dir: dir,
  67. }
  68. tunnelLeft := localPublicIP
  69. tunnelRight := remotePublicIP
  70. tmpl := netlink.XfrmPolicyTmpl{
  71. Src: tunnelLeft,
  72. Dst: tunnelRight,
  73. Proto: netlink.XFRM_PROTO_ESP,
  74. Mode: netlink.XFRM_MODE_TUNNEL,
  75. Reqid: reqID,
  76. }
  77. log.Infof("Deleting ipsec policy: %+v", tmpl)
  78. policy.Tmpls = append(policy.Tmpls, tmpl)
  79. if err := netlink.XfrmPolicyDel(&policy); err != nil {
  80. return fmt.Errorf("error deleting policy: %+v err: %v", policy, err)
  81. }
  82. return nil
  83. }