xfrm_state.go 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. package netlink
  2. import (
  3. "fmt"
  4. "net"
  5. )
  6. // XfrmStateAlgo represents the algorithm to use for the ipsec encryption.
  7. type XfrmStateAlgo struct {
  8. Name string
  9. Key []byte
  10. TruncateLen int // Auth only
  11. ICVLen int // AEAD only
  12. }
  13. func (a XfrmStateAlgo) String() string {
  14. base := fmt.Sprintf("{Name: %s, Key: 0x%x", a.Name, a.Key)
  15. if a.TruncateLen != 0 {
  16. base = fmt.Sprintf("%s, Truncate length: %d", base, a.TruncateLen)
  17. }
  18. if a.ICVLen != 0 {
  19. base = fmt.Sprintf("%s, ICV length: %d", base, a.ICVLen)
  20. }
  21. return fmt.Sprintf("%s}", base)
  22. }
  23. // EncapType is an enum representing the optional packet encapsulation.
  24. type EncapType uint8
  25. const (
  26. XFRM_ENCAP_ESPINUDP_NONIKE EncapType = iota + 1
  27. XFRM_ENCAP_ESPINUDP
  28. )
  29. func (e EncapType) String() string {
  30. switch e {
  31. case XFRM_ENCAP_ESPINUDP_NONIKE:
  32. return "espinudp-non-ike"
  33. case XFRM_ENCAP_ESPINUDP:
  34. return "espinudp"
  35. }
  36. return "unknown"
  37. }
  38. // XfrmStateEncap represents the encapsulation to use for the ipsec encryption.
  39. type XfrmStateEncap struct {
  40. Type EncapType
  41. SrcPort int
  42. DstPort int
  43. OriginalAddress net.IP
  44. }
  45. func (e XfrmStateEncap) String() string {
  46. return fmt.Sprintf("{Type: %s, Srcport: %d, DstPort: %d, OriginalAddress: %v}",
  47. e.Type, e.SrcPort, e.DstPort, e.OriginalAddress)
  48. }
  49. // XfrmStateLimits represents the configured limits for the state.
  50. type XfrmStateLimits struct {
  51. ByteSoft uint64
  52. ByteHard uint64
  53. PacketSoft uint64
  54. PacketHard uint64
  55. TimeSoft uint64
  56. TimeHard uint64
  57. TimeUseSoft uint64
  58. TimeUseHard uint64
  59. }
  60. // XfrmState represents the state of an ipsec policy. It optionally
  61. // contains an XfrmStateAlgo for encryption and one for authentication.
  62. type XfrmState struct {
  63. Dst net.IP
  64. Src net.IP
  65. Proto Proto
  66. Mode Mode
  67. Spi int
  68. Reqid int
  69. ReplayWindow int
  70. Limits XfrmStateLimits
  71. Mark *XfrmMark
  72. Auth *XfrmStateAlgo
  73. Crypt *XfrmStateAlgo
  74. Aead *XfrmStateAlgo
  75. Encap *XfrmStateEncap
  76. ESN bool
  77. }
  78. func (sa XfrmState) String() string {
  79. return fmt.Sprintf("Dst: %v, Src: %v, Proto: %s, Mode: %s, SPI: 0x%x, ReqID: 0x%x, ReplayWindow: %d, Mark: %v, Auth: %v, Crypt: %v, Aead: %v, Encap: %v, ESN: %t",
  80. sa.Dst, sa.Src, sa.Proto, sa.Mode, sa.Spi, sa.Reqid, sa.ReplayWindow, sa.Mark, sa.Auth, sa.Crypt, sa.Aead, sa.Encap, sa.ESN)
  81. }
  82. func (sa XfrmState) Print(stats bool) string {
  83. if !stats {
  84. return sa.String()
  85. }
  86. return fmt.Sprintf("%s, ByteSoft: %s, ByteHard: %s, PacketSoft: %s, PacketHard: %s, TimeSoft: %d, TimeHard: %d, TimeUseSoft: %d, TimeUseHard: %d",
  87. sa.String(), printLimit(sa.Limits.ByteSoft), printLimit(sa.Limits.ByteHard), printLimit(sa.Limits.PacketSoft), printLimit(sa.Limits.PacketHard),
  88. sa.Limits.TimeSoft, sa.Limits.TimeHard, sa.Limits.TimeUseSoft, sa.Limits.TimeUseHard)
  89. }
  90. func printLimit(lmt uint64) string {
  91. if lmt == ^uint64(0) {
  92. return "(INF)"
  93. }
  94. return fmt.Sprintf("%d", lmt)
  95. }