xfrm_state_linux.go 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481
  1. package netlink
  2. import (
  3. "fmt"
  4. "unsafe"
  5. "github.com/vishvananda/netlink/nl"
  6. "golang.org/x/sys/unix"
  7. )
  8. func writeStateAlgo(a *XfrmStateAlgo) []byte {
  9. algo := nl.XfrmAlgo{
  10. AlgKeyLen: uint32(len(a.Key) * 8),
  11. AlgKey: a.Key,
  12. }
  13. end := len(a.Name)
  14. if end > 64 {
  15. end = 64
  16. }
  17. copy(algo.AlgName[:end], a.Name)
  18. return algo.Serialize()
  19. }
  20. func writeStateAlgoAuth(a *XfrmStateAlgo) []byte {
  21. algo := nl.XfrmAlgoAuth{
  22. AlgKeyLen: uint32(len(a.Key) * 8),
  23. AlgTruncLen: uint32(a.TruncateLen),
  24. AlgKey: a.Key,
  25. }
  26. end := len(a.Name)
  27. if end > 64 {
  28. end = 64
  29. }
  30. copy(algo.AlgName[:end], a.Name)
  31. return algo.Serialize()
  32. }
  33. func writeStateAlgoAead(a *XfrmStateAlgo) []byte {
  34. algo := nl.XfrmAlgoAEAD{
  35. AlgKeyLen: uint32(len(a.Key) * 8),
  36. AlgICVLen: uint32(a.ICVLen),
  37. AlgKey: a.Key,
  38. }
  39. end := len(a.Name)
  40. if end > 64 {
  41. end = 64
  42. }
  43. copy(algo.AlgName[:end], a.Name)
  44. return algo.Serialize()
  45. }
  46. func writeMark(m *XfrmMark) []byte {
  47. mark := &nl.XfrmMark{
  48. Value: m.Value,
  49. Mask: m.Mask,
  50. }
  51. if mark.Mask == 0 {
  52. mark.Mask = ^uint32(0)
  53. }
  54. return mark.Serialize()
  55. }
  56. func writeReplayEsn(replayWindow int) []byte {
  57. replayEsn := &nl.XfrmReplayStateEsn{
  58. OSeq: 0,
  59. Seq: 0,
  60. OSeqHi: 0,
  61. SeqHi: 0,
  62. ReplayWindow: uint32(replayWindow),
  63. }
  64. // Linux stores the bitmap to identify the already received sequence packets in blocks of uint32 elements.
  65. // Therefore bitmap length is the minimum number of uint32 elements needed. The following is a ceiling operation.
  66. bytesPerElem := int(unsafe.Sizeof(replayEsn.BmpLen)) // Any uint32 variable is good for this
  67. replayEsn.BmpLen = uint32((replayWindow + (bytesPerElem * 8) - 1) / (bytesPerElem * 8))
  68. return replayEsn.Serialize()
  69. }
  70. // XfrmStateAdd will add an xfrm state to the system.
  71. // Equivalent to: `ip xfrm state add $state`
  72. func XfrmStateAdd(state *XfrmState) error {
  73. return pkgHandle.XfrmStateAdd(state)
  74. }
  75. // XfrmStateAdd will add an xfrm state to the system.
  76. // Equivalent to: `ip xfrm state add $state`
  77. func (h *Handle) XfrmStateAdd(state *XfrmState) error {
  78. return h.xfrmStateAddOrUpdate(state, nl.XFRM_MSG_NEWSA)
  79. }
  80. // XfrmStateAllocSpi will allocate an xfrm state in the system.
  81. // Equivalent to: `ip xfrm state allocspi`
  82. func XfrmStateAllocSpi(state *XfrmState) (*XfrmState, error) {
  83. return pkgHandle.xfrmStateAllocSpi(state)
  84. }
  85. // XfrmStateUpdate will update an xfrm state to the system.
  86. // Equivalent to: `ip xfrm state update $state`
  87. func XfrmStateUpdate(state *XfrmState) error {
  88. return pkgHandle.XfrmStateUpdate(state)
  89. }
  90. // XfrmStateUpdate will update an xfrm state to the system.
  91. // Equivalent to: `ip xfrm state update $state`
  92. func (h *Handle) XfrmStateUpdate(state *XfrmState) error {
  93. return h.xfrmStateAddOrUpdate(state, nl.XFRM_MSG_UPDSA)
  94. }
  95. func (h *Handle) xfrmStateAddOrUpdate(state *XfrmState, nlProto int) error {
  96. // A state with spi 0 can't be deleted so don't allow it to be set
  97. if state.Spi == 0 {
  98. return fmt.Errorf("Spi must be set when adding xfrm state")
  99. }
  100. req := h.newNetlinkRequest(nlProto, unix.NLM_F_CREATE|unix.NLM_F_EXCL|unix.NLM_F_ACK)
  101. msg := xfrmUsersaInfoFromXfrmState(state)
  102. if state.ESN {
  103. if state.ReplayWindow == 0 {
  104. return fmt.Errorf("ESN flag set without ReplayWindow")
  105. }
  106. msg.Flags |= nl.XFRM_STATE_ESN
  107. msg.ReplayWindow = 0
  108. }
  109. limitsToLft(state.Limits, &msg.Lft)
  110. req.AddData(msg)
  111. if state.Auth != nil {
  112. out := nl.NewRtAttr(nl.XFRMA_ALG_AUTH_TRUNC, writeStateAlgoAuth(state.Auth))
  113. req.AddData(out)
  114. }
  115. if state.Crypt != nil {
  116. out := nl.NewRtAttr(nl.XFRMA_ALG_CRYPT, writeStateAlgo(state.Crypt))
  117. req.AddData(out)
  118. }
  119. if state.Aead != nil {
  120. out := nl.NewRtAttr(nl.XFRMA_ALG_AEAD, writeStateAlgoAead(state.Aead))
  121. req.AddData(out)
  122. }
  123. if state.Encap != nil {
  124. encapData := make([]byte, nl.SizeofXfrmEncapTmpl)
  125. encap := nl.DeserializeXfrmEncapTmpl(encapData)
  126. encap.EncapType = uint16(state.Encap.Type)
  127. encap.EncapSport = nl.Swap16(uint16(state.Encap.SrcPort))
  128. encap.EncapDport = nl.Swap16(uint16(state.Encap.DstPort))
  129. encap.EncapOa.FromIP(state.Encap.OriginalAddress)
  130. out := nl.NewRtAttr(nl.XFRMA_ENCAP, encapData)
  131. req.AddData(out)
  132. }
  133. if state.Mark != nil {
  134. out := nl.NewRtAttr(nl.XFRMA_MARK, writeMark(state.Mark))
  135. req.AddData(out)
  136. }
  137. if state.ESN {
  138. out := nl.NewRtAttr(nl.XFRMA_REPLAY_ESN_VAL, writeReplayEsn(state.ReplayWindow))
  139. req.AddData(out)
  140. }
  141. if state.OutputMark != nil {
  142. out := nl.NewRtAttr(nl.XFRMA_SET_MARK, nl.Uint32Attr(state.OutputMark.Value))
  143. req.AddData(out)
  144. if state.OutputMark.Mask != 0 {
  145. out = nl.NewRtAttr(nl.XFRMA_SET_MARK_MASK, nl.Uint32Attr(state.OutputMark.Mask))
  146. req.AddData(out)
  147. }
  148. }
  149. if state.Ifid != 0 {
  150. ifId := nl.NewRtAttr(nl.XFRMA_IF_ID, nl.Uint32Attr(uint32(state.Ifid)))
  151. req.AddData(ifId)
  152. }
  153. _, err := req.Execute(unix.NETLINK_XFRM, 0)
  154. return err
  155. }
  156. func (h *Handle) xfrmStateAllocSpi(state *XfrmState) (*XfrmState, error) {
  157. req := h.newNetlinkRequest(nl.XFRM_MSG_ALLOCSPI,
  158. unix.NLM_F_CREATE|unix.NLM_F_EXCL|unix.NLM_F_ACK)
  159. msg := &nl.XfrmUserSpiInfo{}
  160. msg.XfrmUsersaInfo = *(xfrmUsersaInfoFromXfrmState(state))
  161. // 1-255 is reserved by IANA for future use
  162. msg.Min = 0x100
  163. msg.Max = 0xffffffff
  164. req.AddData(msg)
  165. if state.Mark != nil {
  166. out := nl.NewRtAttr(nl.XFRMA_MARK, writeMark(state.Mark))
  167. req.AddData(out)
  168. }
  169. msgs, err := req.Execute(unix.NETLINK_XFRM, 0)
  170. if err != nil {
  171. return nil, err
  172. }
  173. return parseXfrmState(msgs[0], FAMILY_ALL)
  174. }
  175. // XfrmStateDel will delete an xfrm state from the system. Note that
  176. // the Algos are ignored when matching the state to delete.
  177. // Equivalent to: `ip xfrm state del $state`
  178. func XfrmStateDel(state *XfrmState) error {
  179. return pkgHandle.XfrmStateDel(state)
  180. }
  181. // XfrmStateDel will delete an xfrm state from the system. Note that
  182. // the Algos are ignored when matching the state to delete.
  183. // Equivalent to: `ip xfrm state del $state`
  184. func (h *Handle) XfrmStateDel(state *XfrmState) error {
  185. _, err := h.xfrmStateGetOrDelete(state, nl.XFRM_MSG_DELSA)
  186. return err
  187. }
  188. // XfrmStateList gets a list of xfrm states in the system.
  189. // Equivalent to: `ip [-4|-6] xfrm state show`.
  190. // The list can be filtered by ip family.
  191. func XfrmStateList(family int) ([]XfrmState, error) {
  192. return pkgHandle.XfrmStateList(family)
  193. }
  194. // XfrmStateList gets a list of xfrm states in the system.
  195. // Equivalent to: `ip xfrm state show`.
  196. // The list can be filtered by ip family.
  197. func (h *Handle) XfrmStateList(family int) ([]XfrmState, error) {
  198. req := h.newNetlinkRequest(nl.XFRM_MSG_GETSA, unix.NLM_F_DUMP)
  199. msgs, err := req.Execute(unix.NETLINK_XFRM, nl.XFRM_MSG_NEWSA)
  200. if err != nil {
  201. return nil, err
  202. }
  203. var res []XfrmState
  204. for _, m := range msgs {
  205. if state, err := parseXfrmState(m, family); err == nil {
  206. res = append(res, *state)
  207. } else if err == familyError {
  208. continue
  209. } else {
  210. return nil, err
  211. }
  212. }
  213. return res, nil
  214. }
  215. // XfrmStateGet gets the xfrm state described by the ID, if found.
  216. // Equivalent to: `ip xfrm state get ID [ mark MARK [ mask MASK ] ]`.
  217. // Only the fields which constitue the SA ID must be filled in:
  218. // ID := [ src ADDR ] [ dst ADDR ] [ proto XFRM-PROTO ] [ spi SPI ]
  219. // mark is optional
  220. func XfrmStateGet(state *XfrmState) (*XfrmState, error) {
  221. return pkgHandle.XfrmStateGet(state)
  222. }
  223. // XfrmStateGet gets the xfrm state described by the ID, if found.
  224. // Equivalent to: `ip xfrm state get ID [ mark MARK [ mask MASK ] ]`.
  225. // Only the fields which constitue the SA ID must be filled in:
  226. // ID := [ src ADDR ] [ dst ADDR ] [ proto XFRM-PROTO ] [ spi SPI ]
  227. // mark is optional
  228. func (h *Handle) XfrmStateGet(state *XfrmState) (*XfrmState, error) {
  229. return h.xfrmStateGetOrDelete(state, nl.XFRM_MSG_GETSA)
  230. }
  231. func (h *Handle) xfrmStateGetOrDelete(state *XfrmState, nlProto int) (*XfrmState, error) {
  232. req := h.newNetlinkRequest(nlProto, unix.NLM_F_ACK)
  233. msg := &nl.XfrmUsersaId{}
  234. msg.Family = uint16(nl.GetIPFamily(state.Dst))
  235. msg.Daddr.FromIP(state.Dst)
  236. msg.Proto = uint8(state.Proto)
  237. msg.Spi = nl.Swap32(uint32(state.Spi))
  238. req.AddData(msg)
  239. if state.Mark != nil {
  240. out := nl.NewRtAttr(nl.XFRMA_MARK, writeMark(state.Mark))
  241. req.AddData(out)
  242. }
  243. if state.Src != nil {
  244. out := nl.NewRtAttr(nl.XFRMA_SRCADDR, state.Src.To16())
  245. req.AddData(out)
  246. }
  247. if state.Ifid != 0 {
  248. ifId := nl.NewRtAttr(nl.XFRMA_IF_ID, nl.Uint32Attr(uint32(state.Ifid)))
  249. req.AddData(ifId)
  250. }
  251. resType := nl.XFRM_MSG_NEWSA
  252. if nlProto == nl.XFRM_MSG_DELSA {
  253. resType = 0
  254. }
  255. msgs, err := req.Execute(unix.NETLINK_XFRM, uint16(resType))
  256. if err != nil {
  257. return nil, err
  258. }
  259. if nlProto == nl.XFRM_MSG_DELSA {
  260. return nil, nil
  261. }
  262. s, err := parseXfrmState(msgs[0], FAMILY_ALL)
  263. if err != nil {
  264. return nil, err
  265. }
  266. return s, nil
  267. }
  268. var familyError = fmt.Errorf("family error")
  269. func xfrmStateFromXfrmUsersaInfo(msg *nl.XfrmUsersaInfo) *XfrmState {
  270. var state XfrmState
  271. state.Dst = msg.Id.Daddr.ToIP()
  272. state.Src = msg.Saddr.ToIP()
  273. state.Proto = Proto(msg.Id.Proto)
  274. state.Mode = Mode(msg.Mode)
  275. state.Spi = int(nl.Swap32(msg.Id.Spi))
  276. state.Reqid = int(msg.Reqid)
  277. state.ReplayWindow = int(msg.ReplayWindow)
  278. lftToLimits(&msg.Lft, &state.Limits)
  279. curToStats(&msg.Curlft, &msg.Stats, &state.Statistics)
  280. return &state
  281. }
  282. func parseXfrmState(m []byte, family int) (*XfrmState, error) {
  283. msg := nl.DeserializeXfrmUsersaInfo(m)
  284. // This is mainly for the state dump
  285. if family != FAMILY_ALL && family != int(msg.Family) {
  286. return nil, familyError
  287. }
  288. state := xfrmStateFromXfrmUsersaInfo(msg)
  289. attrs, err := nl.ParseRouteAttr(m[nl.SizeofXfrmUsersaInfo:])
  290. if err != nil {
  291. return nil, err
  292. }
  293. for _, attr := range attrs {
  294. switch attr.Attr.Type {
  295. case nl.XFRMA_ALG_AUTH, nl.XFRMA_ALG_CRYPT:
  296. var resAlgo *XfrmStateAlgo
  297. if attr.Attr.Type == nl.XFRMA_ALG_AUTH {
  298. if state.Auth == nil {
  299. state.Auth = new(XfrmStateAlgo)
  300. }
  301. resAlgo = state.Auth
  302. } else {
  303. state.Crypt = new(XfrmStateAlgo)
  304. resAlgo = state.Crypt
  305. }
  306. algo := nl.DeserializeXfrmAlgo(attr.Value[:])
  307. (*resAlgo).Name = nl.BytesToString(algo.AlgName[:])
  308. (*resAlgo).Key = algo.AlgKey
  309. case nl.XFRMA_ALG_AUTH_TRUNC:
  310. if state.Auth == nil {
  311. state.Auth = new(XfrmStateAlgo)
  312. }
  313. algo := nl.DeserializeXfrmAlgoAuth(attr.Value[:])
  314. state.Auth.Name = nl.BytesToString(algo.AlgName[:])
  315. state.Auth.Key = algo.AlgKey
  316. state.Auth.TruncateLen = int(algo.AlgTruncLen)
  317. case nl.XFRMA_ALG_AEAD:
  318. state.Aead = new(XfrmStateAlgo)
  319. algo := nl.DeserializeXfrmAlgoAEAD(attr.Value[:])
  320. state.Aead.Name = nl.BytesToString(algo.AlgName[:])
  321. state.Aead.Key = algo.AlgKey
  322. state.Aead.ICVLen = int(algo.AlgICVLen)
  323. case nl.XFRMA_ENCAP:
  324. encap := nl.DeserializeXfrmEncapTmpl(attr.Value[:])
  325. state.Encap = new(XfrmStateEncap)
  326. state.Encap.Type = EncapType(encap.EncapType)
  327. state.Encap.SrcPort = int(nl.Swap16(encap.EncapSport))
  328. state.Encap.DstPort = int(nl.Swap16(encap.EncapDport))
  329. state.Encap.OriginalAddress = encap.EncapOa.ToIP()
  330. case nl.XFRMA_MARK:
  331. mark := nl.DeserializeXfrmMark(attr.Value[:])
  332. state.Mark = new(XfrmMark)
  333. state.Mark.Value = mark.Value
  334. state.Mark.Mask = mark.Mask
  335. case nl.XFRMA_SET_MARK:
  336. if state.OutputMark == nil {
  337. state.OutputMark = new(XfrmMark)
  338. }
  339. state.OutputMark.Value = native.Uint32(attr.Value)
  340. case nl.XFRMA_SET_MARK_MASK:
  341. if state.OutputMark == nil {
  342. state.OutputMark = new(XfrmMark)
  343. }
  344. state.OutputMark.Mask = native.Uint32(attr.Value)
  345. if state.OutputMark.Mask == 0xffffffff {
  346. state.OutputMark.Mask = 0
  347. }
  348. case nl.XFRMA_IF_ID:
  349. state.Ifid = int(native.Uint32(attr.Value))
  350. }
  351. }
  352. return state, nil
  353. }
  354. // XfrmStateFlush will flush the xfrm state on the system.
  355. // proto = 0 means any transformation protocols
  356. // Equivalent to: `ip xfrm state flush [ proto XFRM-PROTO ]`
  357. func XfrmStateFlush(proto Proto) error {
  358. return pkgHandle.XfrmStateFlush(proto)
  359. }
  360. // XfrmStateFlush will flush the xfrm state on the system.
  361. // proto = 0 means any transformation protocols
  362. // Equivalent to: `ip xfrm state flush [ proto XFRM-PROTO ]`
  363. func (h *Handle) XfrmStateFlush(proto Proto) error {
  364. req := h.newNetlinkRequest(nl.XFRM_MSG_FLUSHSA, unix.NLM_F_ACK)
  365. req.AddData(&nl.XfrmUsersaFlush{Proto: uint8(proto)})
  366. _, err := req.Execute(unix.NETLINK_XFRM, 0)
  367. return err
  368. }
  369. func limitsToLft(lmts XfrmStateLimits, lft *nl.XfrmLifetimeCfg) {
  370. if lmts.ByteSoft != 0 {
  371. lft.SoftByteLimit = lmts.ByteSoft
  372. } else {
  373. lft.SoftByteLimit = nl.XFRM_INF
  374. }
  375. if lmts.ByteHard != 0 {
  376. lft.HardByteLimit = lmts.ByteHard
  377. } else {
  378. lft.HardByteLimit = nl.XFRM_INF
  379. }
  380. if lmts.PacketSoft != 0 {
  381. lft.SoftPacketLimit = lmts.PacketSoft
  382. } else {
  383. lft.SoftPacketLimit = nl.XFRM_INF
  384. }
  385. if lmts.PacketHard != 0 {
  386. lft.HardPacketLimit = lmts.PacketHard
  387. } else {
  388. lft.HardPacketLimit = nl.XFRM_INF
  389. }
  390. lft.SoftAddExpiresSeconds = lmts.TimeSoft
  391. lft.HardAddExpiresSeconds = lmts.TimeHard
  392. lft.SoftUseExpiresSeconds = lmts.TimeUseSoft
  393. lft.HardUseExpiresSeconds = lmts.TimeUseHard
  394. }
  395. func lftToLimits(lft *nl.XfrmLifetimeCfg, lmts *XfrmStateLimits) {
  396. *lmts = *(*XfrmStateLimits)(unsafe.Pointer(lft))
  397. }
  398. func curToStats(cur *nl.XfrmLifetimeCur, wstats *nl.XfrmStats, stats *XfrmStateStats) {
  399. stats.Bytes = cur.Bytes
  400. stats.Packets = cur.Packets
  401. stats.AddTime = cur.AddTime
  402. stats.UseTime = cur.UseTime
  403. stats.ReplayWindow = wstats.ReplayWindow
  404. stats.Replay = wstats.Replay
  405. stats.Failed = wstats.IntegrityFailed
  406. }
  407. func xfrmUsersaInfoFromXfrmState(state *XfrmState) *nl.XfrmUsersaInfo {
  408. msg := &nl.XfrmUsersaInfo{}
  409. msg.Family = uint16(nl.GetIPFamily(state.Dst))
  410. msg.Id.Daddr.FromIP(state.Dst)
  411. msg.Saddr.FromIP(state.Src)
  412. msg.Id.Proto = uint8(state.Proto)
  413. msg.Mode = uint8(state.Mode)
  414. msg.Id.Spi = nl.Swap32(uint32(state.Spi))
  415. msg.Reqid = uint32(state.Reqid)
  416. msg.ReplayWindow = uint8(state.ReplayWindow)
  417. return msg
  418. }