xfrm_state_linux.go 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. package netlink
  2. import (
  3. "fmt"
  4. "syscall"
  5. "github.com/vishvananda/netlink/nl"
  6. )
  7. func writeStateAlgo(a *XfrmStateAlgo) []byte {
  8. algo := nl.XfrmAlgo{
  9. AlgKeyLen: uint32(len(a.Key) * 8),
  10. AlgKey: a.Key,
  11. }
  12. end := len(a.Name)
  13. if end > 64 {
  14. end = 64
  15. }
  16. copy(algo.AlgName[:end], a.Name)
  17. return algo.Serialize()
  18. }
  19. func writeStateAlgoAuth(a *XfrmStateAlgo) []byte {
  20. algo := nl.XfrmAlgoAuth{
  21. AlgKeyLen: uint32(len(a.Key) * 8),
  22. AlgTruncLen: uint32(a.TruncateLen),
  23. AlgKey: a.Key,
  24. }
  25. end := len(a.Name)
  26. if end > 64 {
  27. end = 64
  28. }
  29. copy(algo.AlgName[:end], a.Name)
  30. return algo.Serialize()
  31. }
  32. // XfrmStateAdd will add an xfrm state to the system.
  33. // Equivalent to: `ip xfrm state add $state`
  34. func XfrmStateAdd(state *XfrmState) error {
  35. // A state with spi 0 can't be deleted so don't allow it to be set
  36. if state.Spi == 0 {
  37. return fmt.Errorf("Spi must be set when adding xfrm state.")
  38. }
  39. req := nl.NewNetlinkRequest(nl.XFRM_MSG_NEWSA, syscall.NLM_F_CREATE|syscall.NLM_F_EXCL|syscall.NLM_F_ACK)
  40. msg := &nl.XfrmUsersaInfo{}
  41. msg.Family = uint16(nl.GetIPFamily(state.Dst))
  42. msg.Id.Daddr.FromIP(state.Dst)
  43. msg.Saddr.FromIP(state.Src)
  44. msg.Id.Proto = uint8(state.Proto)
  45. msg.Mode = uint8(state.Mode)
  46. msg.Id.Spi = nl.Swap32(uint32(state.Spi))
  47. msg.Reqid = uint32(state.Reqid)
  48. msg.ReplayWindow = uint8(state.ReplayWindow)
  49. msg.Lft.SoftByteLimit = nl.XFRM_INF
  50. msg.Lft.HardByteLimit = nl.XFRM_INF
  51. msg.Lft.SoftPacketLimit = nl.XFRM_INF
  52. msg.Lft.HardPacketLimit = nl.XFRM_INF
  53. req.AddData(msg)
  54. if state.Auth != nil {
  55. out := nl.NewRtAttr(nl.XFRMA_ALG_AUTH_TRUNC, writeStateAlgoAuth(state.Auth))
  56. req.AddData(out)
  57. }
  58. if state.Crypt != nil {
  59. out := nl.NewRtAttr(nl.XFRMA_ALG_CRYPT, writeStateAlgo(state.Crypt))
  60. req.AddData(out)
  61. }
  62. if state.Encap != nil {
  63. encapData := make([]byte, nl.SizeofXfrmEncapTmpl)
  64. encap := nl.DeserializeXfrmEncapTmpl(encapData)
  65. encap.EncapType = uint16(state.Encap.Type)
  66. encap.EncapSport = nl.Swap16(uint16(state.Encap.SrcPort))
  67. encap.EncapDport = nl.Swap16(uint16(state.Encap.DstPort))
  68. encap.EncapOa.FromIP(state.Encap.OriginalAddress)
  69. out := nl.NewRtAttr(nl.XFRMA_ENCAP, encapData)
  70. req.AddData(out)
  71. }
  72. _, err := req.Execute(syscall.NETLINK_XFRM, 0)
  73. return err
  74. }
  75. // XfrmStateDel will delete an xfrm state from the system. Note that
  76. // the Algos are ignored when matching the state to delete.
  77. // Equivalent to: `ip xfrm state del $state`
  78. func XfrmStateDel(state *XfrmState) error {
  79. req := nl.NewNetlinkRequest(nl.XFRM_MSG_DELSA, syscall.NLM_F_ACK)
  80. msg := &nl.XfrmUsersaId{}
  81. msg.Daddr.FromIP(state.Dst)
  82. msg.Family = uint16(nl.GetIPFamily(state.Dst))
  83. msg.Proto = uint8(state.Proto)
  84. msg.Spi = nl.Swap32(uint32(state.Spi))
  85. req.AddData(msg)
  86. saddr := nl.XfrmAddress{}
  87. saddr.FromIP(state.Src)
  88. srcdata := nl.NewRtAttr(nl.XFRMA_SRCADDR, saddr.Serialize())
  89. req.AddData(srcdata)
  90. _, err := req.Execute(syscall.NETLINK_XFRM, 0)
  91. return err
  92. }
  93. // XfrmStateList gets a list of xfrm states in the system.
  94. // Equivalent to: `ip xfrm state show`.
  95. // The list can be filtered by ip family.
  96. func XfrmStateList(family int) ([]XfrmState, error) {
  97. req := nl.NewNetlinkRequest(nl.XFRM_MSG_GETSA, syscall.NLM_F_DUMP)
  98. msgs, err := req.Execute(syscall.NETLINK_XFRM, nl.XFRM_MSG_NEWSA)
  99. if err != nil {
  100. return nil, err
  101. }
  102. var res []XfrmState
  103. for _, m := range msgs {
  104. msg := nl.DeserializeXfrmUsersaInfo(m)
  105. if family != FAMILY_ALL && family != int(msg.Family) {
  106. continue
  107. }
  108. var state XfrmState
  109. state.Dst = msg.Id.Daddr.ToIP()
  110. state.Src = msg.Saddr.ToIP()
  111. state.Proto = Proto(msg.Id.Proto)
  112. state.Mode = Mode(msg.Mode)
  113. state.Spi = int(nl.Swap32(msg.Id.Spi))
  114. state.Reqid = int(msg.Reqid)
  115. state.ReplayWindow = int(msg.ReplayWindow)
  116. attrs, err := nl.ParseRouteAttr(m[msg.Len():])
  117. if err != nil {
  118. return nil, err
  119. }
  120. for _, attr := range attrs {
  121. switch attr.Attr.Type {
  122. case nl.XFRMA_ALG_AUTH, nl.XFRMA_ALG_CRYPT:
  123. var resAlgo *XfrmStateAlgo
  124. if attr.Attr.Type == nl.XFRMA_ALG_AUTH {
  125. if state.Auth == nil {
  126. state.Auth = new(XfrmStateAlgo)
  127. }
  128. resAlgo = state.Auth
  129. } else {
  130. state.Crypt = new(XfrmStateAlgo)
  131. resAlgo = state.Crypt
  132. }
  133. algo := nl.DeserializeXfrmAlgo(attr.Value[:])
  134. (*resAlgo).Name = nl.BytesToString(algo.AlgName[:])
  135. (*resAlgo).Key = algo.AlgKey
  136. case nl.XFRMA_ALG_AUTH_TRUNC:
  137. if state.Auth == nil {
  138. state.Auth = new(XfrmStateAlgo)
  139. }
  140. algo := nl.DeserializeXfrmAlgoAuth(attr.Value[:])
  141. state.Auth.Name = nl.BytesToString(algo.AlgName[:])
  142. state.Auth.Key = algo.AlgKey
  143. state.Auth.TruncateLen = int(algo.AlgTruncLen)
  144. case nl.XFRMA_ENCAP:
  145. encap := nl.DeserializeXfrmEncapTmpl(attr.Value[:])
  146. state.Encap = new(XfrmStateEncap)
  147. state.Encap.Type = EncapType(encap.EncapType)
  148. state.Encap.SrcPort = int(nl.Swap16(encap.EncapSport))
  149. state.Encap.DstPort = int(nl.Swap16(encap.EncapDport))
  150. state.Encap.OriginalAddress = encap.EncapOa.ToIP()
  151. }
  152. }
  153. res = append(res, state)
  154. }
  155. return res, nil
  156. }