xfrm_policy.go 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. package netlink
  2. import (
  3. "fmt"
  4. "net"
  5. )
  6. // Dir is an enum representing an ipsec template direction.
  7. type Dir uint8
  8. const (
  9. XFRM_DIR_IN Dir = iota
  10. XFRM_DIR_OUT
  11. XFRM_DIR_FWD
  12. XFRM_SOCKET_IN
  13. XFRM_SOCKET_OUT
  14. XFRM_SOCKET_FWD
  15. )
  16. func (d Dir) String() string {
  17. switch d {
  18. case XFRM_DIR_IN:
  19. return "dir in"
  20. case XFRM_DIR_OUT:
  21. return "dir out"
  22. case XFRM_DIR_FWD:
  23. return "dir fwd"
  24. case XFRM_SOCKET_IN:
  25. return "socket in"
  26. case XFRM_SOCKET_OUT:
  27. return "socket out"
  28. case XFRM_SOCKET_FWD:
  29. return "socket fwd"
  30. }
  31. return fmt.Sprintf("socket %d", d-XFRM_SOCKET_IN)
  32. }
  33. // PolicyAction is an enum representing an ipsec policy action.
  34. type PolicyAction uint8
  35. const (
  36. XFRM_POLICY_ALLOW PolicyAction = 0
  37. XFRM_POLICY_BLOCK PolicyAction = 1
  38. )
  39. func (a PolicyAction) String() string {
  40. switch a {
  41. case XFRM_POLICY_ALLOW:
  42. return "allow"
  43. case XFRM_POLICY_BLOCK:
  44. return "block"
  45. default:
  46. return fmt.Sprintf("action %d", a)
  47. }
  48. }
  49. // XfrmPolicyTmpl encapsulates a rule for the base addresses of an ipsec
  50. // policy. These rules are matched with XfrmState to determine encryption
  51. // and authentication algorithms.
  52. type XfrmPolicyTmpl struct {
  53. Dst net.IP
  54. Src net.IP
  55. Proto Proto
  56. Mode Mode
  57. Spi int
  58. Reqid int
  59. Optional int
  60. }
  61. func (t XfrmPolicyTmpl) String() string {
  62. return fmt.Sprintf("{Dst: %v, Src: %v, Proto: %s, Mode: %s, Spi: 0x%x, Reqid: 0x%x}",
  63. t.Dst, t.Src, t.Proto, t.Mode, t.Spi, t.Reqid)
  64. }
  65. // XfrmPolicy represents an ipsec policy. It represents the overlay network
  66. // and has a list of XfrmPolicyTmpls representing the base addresses of
  67. // the policy.
  68. type XfrmPolicy struct {
  69. Dst *net.IPNet
  70. Src *net.IPNet
  71. Proto Proto
  72. DstPort int
  73. SrcPort int
  74. Dir Dir
  75. Priority int
  76. Index int
  77. Action PolicyAction
  78. Ifindex int
  79. Ifid int
  80. Mark *XfrmMark
  81. Tmpls []XfrmPolicyTmpl
  82. }
  83. func (p XfrmPolicy) String() string {
  84. return fmt.Sprintf("{Dst: %v, Src: %v, Proto: %s, DstPort: %d, SrcPort: %d, Dir: %s, Priority: %d, Index: %d, Action: %s, Ifindex: %d, Ifid: %d, Mark: %s, Tmpls: %s}",
  85. p.Dst, p.Src, p.Proto, p.DstPort, p.SrcPort, p.Dir, p.Priority, p.Index, p.Action, p.Ifindex, p.Ifid, p.Mark, p.Tmpls)
  86. }