xfrm_state_linux.go 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335
  1. package nl
  2. import (
  3. "unsafe"
  4. )
  5. const (
  6. SizeofXfrmUsersaId = 0x18
  7. SizeofXfrmStats = 0x0c
  8. SizeofXfrmUsersaInfo = 0xe0
  9. SizeofXfrmUserSpiInfo = 0xe8
  10. SizeofXfrmAlgo = 0x44
  11. SizeofXfrmAlgoAuth = 0x48
  12. SizeofXfrmAlgoAEAD = 0x48
  13. SizeofXfrmEncapTmpl = 0x18
  14. SizeofXfrmUsersaFlush = 0x8
  15. SizeofXfrmReplayStateEsn = 0x18
  16. )
  17. const (
  18. XFRM_STATE_NOECN = 1
  19. XFRM_STATE_DECAP_DSCP = 2
  20. XFRM_STATE_NOPMTUDISC = 4
  21. XFRM_STATE_WILDRECV = 8
  22. XFRM_STATE_ICMP = 16
  23. XFRM_STATE_AF_UNSPEC = 32
  24. XFRM_STATE_ALIGN4 = 64
  25. XFRM_STATE_ESN = 128
  26. )
  27. // struct xfrm_usersa_id {
  28. // xfrm_address_t daddr;
  29. // __be32 spi;
  30. // __u16 family;
  31. // __u8 proto;
  32. // };
  33. type XfrmUsersaId struct {
  34. Daddr XfrmAddress
  35. Spi uint32 // big endian
  36. Family uint16
  37. Proto uint8
  38. Pad byte
  39. }
  40. func (msg *XfrmUsersaId) Len() int {
  41. return SizeofXfrmUsersaId
  42. }
  43. func DeserializeXfrmUsersaId(b []byte) *XfrmUsersaId {
  44. return (*XfrmUsersaId)(unsafe.Pointer(&b[0:SizeofXfrmUsersaId][0]))
  45. }
  46. func (msg *XfrmUsersaId) Serialize() []byte {
  47. return (*(*[SizeofXfrmUsersaId]byte)(unsafe.Pointer(msg)))[:]
  48. }
  49. // struct xfrm_stats {
  50. // __u32 replay_window;
  51. // __u32 replay;
  52. // __u32 integrity_failed;
  53. // };
  54. type XfrmStats struct {
  55. ReplayWindow uint32
  56. Replay uint32
  57. IntegrityFailed uint32
  58. }
  59. func (msg *XfrmStats) Len() int {
  60. return SizeofXfrmStats
  61. }
  62. func DeserializeXfrmStats(b []byte) *XfrmStats {
  63. return (*XfrmStats)(unsafe.Pointer(&b[0:SizeofXfrmStats][0]))
  64. }
  65. func (msg *XfrmStats) Serialize() []byte {
  66. return (*(*[SizeofXfrmStats]byte)(unsafe.Pointer(msg)))[:]
  67. }
  68. // struct xfrm_usersa_info {
  69. // struct xfrm_selector sel;
  70. // struct xfrm_id id;
  71. // xfrm_address_t saddr;
  72. // struct xfrm_lifetime_cfg lft;
  73. // struct xfrm_lifetime_cur curlft;
  74. // struct xfrm_stats stats;
  75. // __u32 seq;
  76. // __u32 reqid;
  77. // __u16 family;
  78. // __u8 mode; /* XFRM_MODE_xxx */
  79. // __u8 replay_window;
  80. // __u8 flags;
  81. // #define XFRM_STATE_NOECN 1
  82. // #define XFRM_STATE_DECAP_DSCP 2
  83. // #define XFRM_STATE_NOPMTUDISC 4
  84. // #define XFRM_STATE_WILDRECV 8
  85. // #define XFRM_STATE_ICMP 16
  86. // #define XFRM_STATE_AF_UNSPEC 32
  87. // #define XFRM_STATE_ALIGN4 64
  88. // #define XFRM_STATE_ESN 128
  89. // };
  90. //
  91. // #define XFRM_SA_XFLAG_DONT_ENCAP_DSCP 1
  92. //
  93. type XfrmUsersaInfo struct {
  94. Sel XfrmSelector
  95. Id XfrmId
  96. Saddr XfrmAddress
  97. Lft XfrmLifetimeCfg
  98. Curlft XfrmLifetimeCur
  99. Stats XfrmStats
  100. Seq uint32
  101. Reqid uint32
  102. Family uint16
  103. Mode uint8
  104. ReplayWindow uint8
  105. Flags uint8
  106. Pad [7]byte
  107. }
  108. func (msg *XfrmUsersaInfo) Len() int {
  109. return SizeofXfrmUsersaInfo
  110. }
  111. func DeserializeXfrmUsersaInfo(b []byte) *XfrmUsersaInfo {
  112. return (*XfrmUsersaInfo)(unsafe.Pointer(&b[0:SizeofXfrmUsersaInfo][0]))
  113. }
  114. func (msg *XfrmUsersaInfo) Serialize() []byte {
  115. return (*(*[SizeofXfrmUsersaInfo]byte)(unsafe.Pointer(msg)))[:]
  116. }
  117. // struct xfrm_userspi_info {
  118. // struct xfrm_usersa_info info;
  119. // __u32 min;
  120. // __u32 max;
  121. // };
  122. type XfrmUserSpiInfo struct {
  123. XfrmUsersaInfo XfrmUsersaInfo
  124. Min uint32
  125. Max uint32
  126. }
  127. func (msg *XfrmUserSpiInfo) Len() int {
  128. return SizeofXfrmUserSpiInfo
  129. }
  130. func DeserializeXfrmUserSpiInfo(b []byte) *XfrmUserSpiInfo {
  131. return (*XfrmUserSpiInfo)(unsafe.Pointer(&b[0:SizeofXfrmUserSpiInfo][0]))
  132. }
  133. func (msg *XfrmUserSpiInfo) Serialize() []byte {
  134. return (*(*[SizeofXfrmUserSpiInfo]byte)(unsafe.Pointer(msg)))[:]
  135. }
  136. // struct xfrm_algo {
  137. // char alg_name[64];
  138. // unsigned int alg_key_len; /* in bits */
  139. // char alg_key[0];
  140. // };
  141. type XfrmAlgo struct {
  142. AlgName [64]byte
  143. AlgKeyLen uint32
  144. AlgKey []byte
  145. }
  146. func (msg *XfrmAlgo) Len() int {
  147. return SizeofXfrmAlgo + int(msg.AlgKeyLen/8)
  148. }
  149. func DeserializeXfrmAlgo(b []byte) *XfrmAlgo {
  150. ret := XfrmAlgo{}
  151. copy(ret.AlgName[:], b[0:64])
  152. ret.AlgKeyLen = *(*uint32)(unsafe.Pointer(&b[64]))
  153. ret.AlgKey = b[68:ret.Len()]
  154. return &ret
  155. }
  156. func (msg *XfrmAlgo) Serialize() []byte {
  157. b := make([]byte, msg.Len())
  158. copy(b[0:64], msg.AlgName[:])
  159. copy(b[64:68], (*(*[4]byte)(unsafe.Pointer(&msg.AlgKeyLen)))[:])
  160. copy(b[68:msg.Len()], msg.AlgKey[:])
  161. return b
  162. }
  163. // struct xfrm_algo_auth {
  164. // char alg_name[64];
  165. // unsigned int alg_key_len; /* in bits */
  166. // unsigned int alg_trunc_len; /* in bits */
  167. // char alg_key[0];
  168. // };
  169. type XfrmAlgoAuth struct {
  170. AlgName [64]byte
  171. AlgKeyLen uint32
  172. AlgTruncLen uint32
  173. AlgKey []byte
  174. }
  175. func (msg *XfrmAlgoAuth) Len() int {
  176. return SizeofXfrmAlgoAuth + int(msg.AlgKeyLen/8)
  177. }
  178. func DeserializeXfrmAlgoAuth(b []byte) *XfrmAlgoAuth {
  179. ret := XfrmAlgoAuth{}
  180. copy(ret.AlgName[:], b[0:64])
  181. ret.AlgKeyLen = *(*uint32)(unsafe.Pointer(&b[64]))
  182. ret.AlgTruncLen = *(*uint32)(unsafe.Pointer(&b[68]))
  183. ret.AlgKey = b[72:ret.Len()]
  184. return &ret
  185. }
  186. func (msg *XfrmAlgoAuth) Serialize() []byte {
  187. b := make([]byte, msg.Len())
  188. copy(b[0:64], msg.AlgName[:])
  189. copy(b[64:68], (*(*[4]byte)(unsafe.Pointer(&msg.AlgKeyLen)))[:])
  190. copy(b[68:72], (*(*[4]byte)(unsafe.Pointer(&msg.AlgTruncLen)))[:])
  191. copy(b[72:msg.Len()], msg.AlgKey[:])
  192. return b
  193. }
  194. // struct xfrm_algo_aead {
  195. // char alg_name[64];
  196. // unsigned int alg_key_len; /* in bits */
  197. // unsigned int alg_icv_len; /* in bits */
  198. // char alg_key[0];
  199. // }
  200. type XfrmAlgoAEAD struct {
  201. AlgName [64]byte
  202. AlgKeyLen uint32
  203. AlgICVLen uint32
  204. AlgKey []byte
  205. }
  206. func (msg *XfrmAlgoAEAD) Len() int {
  207. return SizeofXfrmAlgoAEAD + int(msg.AlgKeyLen/8)
  208. }
  209. func DeserializeXfrmAlgoAEAD(b []byte) *XfrmAlgoAEAD {
  210. ret := XfrmAlgoAEAD{}
  211. copy(ret.AlgName[:], b[0:64])
  212. ret.AlgKeyLen = *(*uint32)(unsafe.Pointer(&b[64]))
  213. ret.AlgICVLen = *(*uint32)(unsafe.Pointer(&b[68]))
  214. ret.AlgKey = b[72:ret.Len()]
  215. return &ret
  216. }
  217. func (msg *XfrmAlgoAEAD) Serialize() []byte {
  218. b := make([]byte, msg.Len())
  219. copy(b[0:64], msg.AlgName[:])
  220. copy(b[64:68], (*(*[4]byte)(unsafe.Pointer(&msg.AlgKeyLen)))[:])
  221. copy(b[68:72], (*(*[4]byte)(unsafe.Pointer(&msg.AlgICVLen)))[:])
  222. copy(b[72:msg.Len()], msg.AlgKey[:])
  223. return b
  224. }
  225. // struct xfrm_encap_tmpl {
  226. // __u16 encap_type;
  227. // __be16 encap_sport;
  228. // __be16 encap_dport;
  229. // xfrm_address_t encap_oa;
  230. // };
  231. type XfrmEncapTmpl struct {
  232. EncapType uint16
  233. EncapSport uint16 // big endian
  234. EncapDport uint16 // big endian
  235. Pad [2]byte
  236. EncapOa XfrmAddress
  237. }
  238. func (msg *XfrmEncapTmpl) Len() int {
  239. return SizeofXfrmEncapTmpl
  240. }
  241. func DeserializeXfrmEncapTmpl(b []byte) *XfrmEncapTmpl {
  242. return (*XfrmEncapTmpl)(unsafe.Pointer(&b[0:SizeofXfrmEncapTmpl][0]))
  243. }
  244. func (msg *XfrmEncapTmpl) Serialize() []byte {
  245. return (*(*[SizeofXfrmEncapTmpl]byte)(unsafe.Pointer(msg)))[:]
  246. }
  247. // struct xfrm_usersa_flush {
  248. // __u8 proto;
  249. // };
  250. type XfrmUsersaFlush struct {
  251. Proto uint8
  252. }
  253. func (msg *XfrmUsersaFlush) Len() int {
  254. return SizeofXfrmUsersaFlush
  255. }
  256. func DeserializeXfrmUsersaFlush(b []byte) *XfrmUsersaFlush {
  257. return (*XfrmUsersaFlush)(unsafe.Pointer(&b[0:SizeofXfrmUsersaFlush][0]))
  258. }
  259. func (msg *XfrmUsersaFlush) Serialize() []byte {
  260. return (*(*[SizeofXfrmUsersaFlush]byte)(unsafe.Pointer(msg)))[:]
  261. }
  262. // struct xfrm_replay_state_esn {
  263. // unsigned int bmp_len;
  264. // __u32 oseq;
  265. // __u32 seq;
  266. // __u32 oseq_hi;
  267. // __u32 seq_hi;
  268. // __u32 replay_window;
  269. // __u32 bmp[0];
  270. // };
  271. type XfrmReplayStateEsn struct {
  272. BmpLen uint32
  273. OSeq uint32
  274. Seq uint32
  275. OSeqHi uint32
  276. SeqHi uint32
  277. ReplayWindow uint32
  278. Bmp []uint32
  279. }
  280. func (msg *XfrmReplayStateEsn) Serialize() []byte {
  281. // We deliberately do not pass Bmp, as it gets set by the kernel.
  282. return (*(*[SizeofXfrmReplayStateEsn]byte)(unsafe.Pointer(msg)))[:]
  283. }