xfrm_policy_linux.go 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. package netlink
  2. import (
  3. "syscall"
  4. "github.com/vishvananda/netlink/nl"
  5. )
  6. func selFromPolicy(sel *nl.XfrmSelector, policy *XfrmPolicy) {
  7. sel.Family = uint16(nl.GetIPFamily(policy.Dst.IP))
  8. sel.Daddr.FromIP(policy.Dst.IP)
  9. sel.Saddr.FromIP(policy.Src.IP)
  10. prefixlenD, _ := policy.Dst.Mask.Size()
  11. sel.PrefixlenD = uint8(prefixlenD)
  12. prefixlenS, _ := policy.Src.Mask.Size()
  13. sel.PrefixlenS = uint8(prefixlenS)
  14. }
  15. // XfrmPolicyAdd will add an xfrm policy to the system.
  16. // Equivalent to: `ip xfrm policy add $policy`
  17. func XfrmPolicyAdd(policy *XfrmPolicy) error {
  18. req := nl.NewNetlinkRequest(nl.XFRM_MSG_NEWPOLICY, syscall.NLM_F_CREATE|syscall.NLM_F_EXCL|syscall.NLM_F_ACK)
  19. msg := &nl.XfrmUserpolicyInfo{}
  20. selFromPolicy(&msg.Sel, policy)
  21. msg.Priority = uint32(policy.Priority)
  22. msg.Index = uint32(policy.Index)
  23. msg.Dir = uint8(policy.Dir)
  24. msg.Lft.SoftByteLimit = nl.XFRM_INF
  25. msg.Lft.HardByteLimit = nl.XFRM_INF
  26. msg.Lft.SoftPacketLimit = nl.XFRM_INF
  27. msg.Lft.HardPacketLimit = nl.XFRM_INF
  28. req.AddData(msg)
  29. tmplData := make([]byte, nl.SizeofXfrmUserTmpl*len(policy.Tmpls))
  30. for i, tmpl := range policy.Tmpls {
  31. start := i * nl.SizeofXfrmUserTmpl
  32. userTmpl := nl.DeserializeXfrmUserTmpl(tmplData[start : start+nl.SizeofXfrmUserTmpl])
  33. userTmpl.XfrmId.Daddr.FromIP(tmpl.Dst)
  34. userTmpl.Saddr.FromIP(tmpl.Src)
  35. userTmpl.XfrmId.Proto = uint8(tmpl.Proto)
  36. userTmpl.Mode = uint8(tmpl.Mode)
  37. userTmpl.Reqid = uint32(tmpl.Reqid)
  38. userTmpl.Aalgos = ^uint32(0)
  39. userTmpl.Ealgos = ^uint32(0)
  40. userTmpl.Calgos = ^uint32(0)
  41. }
  42. if len(tmplData) > 0 {
  43. tmpls := nl.NewRtAttr(nl.XFRMA_TMPL, tmplData)
  44. req.AddData(tmpls)
  45. }
  46. _, err := req.Execute(syscall.NETLINK_XFRM, 0)
  47. return err
  48. }
  49. // XfrmPolicyDel will delete an xfrm policy from the system. Note that
  50. // the Tmpls are ignored when matching the policy to delete.
  51. // Equivalent to: `ip xfrm policy del $policy`
  52. func XfrmPolicyDel(policy *XfrmPolicy) error {
  53. req := nl.NewNetlinkRequest(nl.XFRM_MSG_DELPOLICY, syscall.NLM_F_ACK)
  54. msg := &nl.XfrmUserpolicyId{}
  55. selFromPolicy(&msg.Sel, policy)
  56. msg.Index = uint32(policy.Index)
  57. msg.Dir = uint8(policy.Dir)
  58. req.AddData(msg)
  59. _, err := req.Execute(syscall.NETLINK_XFRM, 0)
  60. return err
  61. }
  62. // XfrmPolicyList gets a list of xfrm policies in the system.
  63. // Equivalent to: `ip xfrm policy show`.
  64. // The list can be filtered by ip family.
  65. func XfrmPolicyList(family int) ([]XfrmPolicy, error) {
  66. req := nl.NewNetlinkRequest(nl.XFRM_MSG_GETPOLICY, syscall.NLM_F_DUMP)
  67. msg := nl.NewIfInfomsg(family)
  68. req.AddData(msg)
  69. msgs, err := req.Execute(syscall.NETLINK_XFRM, nl.XFRM_MSG_NEWPOLICY)
  70. if err != nil {
  71. return nil, err
  72. }
  73. var res []XfrmPolicy
  74. for _, m := range msgs {
  75. msg := nl.DeserializeXfrmUserpolicyInfo(m)
  76. if family != FAMILY_ALL && family != int(msg.Sel.Family) {
  77. continue
  78. }
  79. var policy XfrmPolicy
  80. policy.Dst = msg.Sel.Daddr.ToIPNet(msg.Sel.PrefixlenD)
  81. policy.Src = msg.Sel.Saddr.ToIPNet(msg.Sel.PrefixlenS)
  82. policy.Priority = int(msg.Priority)
  83. policy.Index = int(msg.Index)
  84. policy.Dir = Dir(msg.Dir)
  85. attrs, err := nl.ParseRouteAttr(m[msg.Len():])
  86. if err != nil {
  87. return nil, err
  88. }
  89. for _, attr := range attrs {
  90. switch attr.Attr.Type {
  91. case nl.XFRMA_TMPL:
  92. max := len(attr.Value)
  93. for i := 0; i < max; i += nl.SizeofXfrmUserTmpl {
  94. var resTmpl XfrmPolicyTmpl
  95. tmpl := nl.DeserializeXfrmUserTmpl(attr.Value[i : i+nl.SizeofXfrmUserTmpl])
  96. resTmpl.Dst = tmpl.XfrmId.Daddr.ToIP()
  97. resTmpl.Src = tmpl.Saddr.ToIP()
  98. resTmpl.Proto = Proto(tmpl.XfrmId.Proto)
  99. resTmpl.Mode = Mode(tmpl.Mode)
  100. resTmpl.Reqid = int(tmpl.Reqid)
  101. policy.Tmpls = append(policy.Tmpls, resTmpl)
  102. }
  103. }
  104. }
  105. res = append(res, policy)
  106. }
  107. return res, nil
  108. }