xfrm_state_linux.go 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  1. package nl
  2. import (
  3. "unsafe"
  4. )
  5. const (
  6. SizeofXfrmUsersaId = 0x18
  7. SizeofXfrmStats = 0x0c
  8. SizeofXfrmUsersaInfo = 0xe0
  9. SizeofXfrmAlgo = 0x44
  10. SizeofXfrmAlgoAuth = 0x48
  11. SizeofXfrmEncapTmpl = 0x18
  12. )
  13. // struct xfrm_usersa_id {
  14. // xfrm_address_t daddr;
  15. // __be32 spi;
  16. // __u16 family;
  17. // __u8 proto;
  18. // };
  19. type XfrmUsersaId struct {
  20. Daddr XfrmAddress
  21. Spi uint32 // big endian
  22. Family uint16
  23. Proto uint8
  24. Pad byte
  25. }
  26. func (msg *XfrmUsersaId) Len() int {
  27. return SizeofXfrmUsersaId
  28. }
  29. func DeserializeXfrmUsersaId(b []byte) *XfrmUsersaId {
  30. return (*XfrmUsersaId)(unsafe.Pointer(&b[0:SizeofXfrmUsersaId][0]))
  31. }
  32. func (msg *XfrmUsersaId) Serialize() []byte {
  33. return (*(*[SizeofXfrmUsersaId]byte)(unsafe.Pointer(msg)))[:]
  34. }
  35. // struct xfrm_stats {
  36. // __u32 replay_window;
  37. // __u32 replay;
  38. // __u32 integrity_failed;
  39. // };
  40. type XfrmStats struct {
  41. ReplayWindow uint32
  42. Replay uint32
  43. IntegrityFailed uint32
  44. }
  45. func (msg *XfrmStats) Len() int {
  46. return SizeofXfrmStats
  47. }
  48. func DeserializeXfrmStats(b []byte) *XfrmStats {
  49. return (*XfrmStats)(unsafe.Pointer(&b[0:SizeofXfrmStats][0]))
  50. }
  51. func (msg *XfrmStats) Serialize() []byte {
  52. return (*(*[SizeofXfrmStats]byte)(unsafe.Pointer(msg)))[:]
  53. }
  54. // struct xfrm_usersa_info {
  55. // struct xfrm_selector sel;
  56. // struct xfrm_id id;
  57. // xfrm_address_t saddr;
  58. // struct xfrm_lifetime_cfg lft;
  59. // struct xfrm_lifetime_cur curlft;
  60. // struct xfrm_stats stats;
  61. // __u32 seq;
  62. // __u32 reqid;
  63. // __u16 family;
  64. // __u8 mode; /* XFRM_MODE_xxx */
  65. // __u8 replay_window;
  66. // __u8 flags;
  67. // #define XFRM_STATE_NOECN 1
  68. // #define XFRM_STATE_DECAP_DSCP 2
  69. // #define XFRM_STATE_NOPMTUDISC 4
  70. // #define XFRM_STATE_WILDRECV 8
  71. // #define XFRM_STATE_ICMP 16
  72. // #define XFRM_STATE_AF_UNSPEC 32
  73. // #define XFRM_STATE_ALIGN4 64
  74. // #define XFRM_STATE_ESN 128
  75. // };
  76. //
  77. // #define XFRM_SA_XFLAG_DONT_ENCAP_DSCP 1
  78. //
  79. type XfrmUsersaInfo struct {
  80. Sel XfrmSelector
  81. Id XfrmId
  82. Saddr XfrmAddress
  83. Lft XfrmLifetimeCfg
  84. Curlft XfrmLifetimeCur
  85. Stats XfrmStats
  86. Seq uint32
  87. Reqid uint32
  88. Family uint16
  89. Mode uint8
  90. ReplayWindow uint8
  91. Flags uint8
  92. Pad [7]byte
  93. }
  94. func (msg *XfrmUsersaInfo) Len() int {
  95. return SizeofXfrmUsersaInfo
  96. }
  97. func DeserializeXfrmUsersaInfo(b []byte) *XfrmUsersaInfo {
  98. return (*XfrmUsersaInfo)(unsafe.Pointer(&b[0:SizeofXfrmUsersaInfo][0]))
  99. }
  100. func (msg *XfrmUsersaInfo) Serialize() []byte {
  101. return (*(*[SizeofXfrmUsersaInfo]byte)(unsafe.Pointer(msg)))[:]
  102. }
  103. // struct xfrm_algo {
  104. // char alg_name[64];
  105. // unsigned int alg_key_len; /* in bits */
  106. // char alg_key[0];
  107. // };
  108. type XfrmAlgo struct {
  109. AlgName [64]byte
  110. AlgKeyLen uint32
  111. AlgKey []byte
  112. }
  113. func (msg *XfrmAlgo) Len() int {
  114. return SizeofXfrmAlgo + int(msg.AlgKeyLen/8)
  115. }
  116. func DeserializeXfrmAlgo(b []byte) *XfrmAlgo {
  117. ret := XfrmAlgo{}
  118. copy(ret.AlgName[:], b[0:64])
  119. ret.AlgKeyLen = *(*uint32)(unsafe.Pointer(&b[64]))
  120. ret.AlgKey = b[68:ret.Len()]
  121. return &ret
  122. }
  123. func (msg *XfrmAlgo) Serialize() []byte {
  124. b := make([]byte, msg.Len())
  125. copy(b[0:64], msg.AlgName[:])
  126. copy(b[64:68], (*(*[4]byte)(unsafe.Pointer(&msg.AlgKeyLen)))[:])
  127. copy(b[68:msg.Len()], msg.AlgKey[:])
  128. return b
  129. }
  130. // struct xfrm_algo_auth {
  131. // char alg_name[64];
  132. // unsigned int alg_key_len; /* in bits */
  133. // unsigned int alg_trunc_len; /* in bits */
  134. // char alg_key[0];
  135. // };
  136. type XfrmAlgoAuth struct {
  137. AlgName [64]byte
  138. AlgKeyLen uint32
  139. AlgTruncLen uint32
  140. AlgKey []byte
  141. }
  142. func (msg *XfrmAlgoAuth) Len() int {
  143. return SizeofXfrmAlgoAuth + int(msg.AlgKeyLen/8)
  144. }
  145. func DeserializeXfrmAlgoAuth(b []byte) *XfrmAlgoAuth {
  146. ret := XfrmAlgoAuth{}
  147. copy(ret.AlgName[:], b[0:64])
  148. ret.AlgKeyLen = *(*uint32)(unsafe.Pointer(&b[64]))
  149. ret.AlgTruncLen = *(*uint32)(unsafe.Pointer(&b[68]))
  150. ret.AlgKey = b[72:ret.Len()]
  151. return &ret
  152. }
  153. func (msg *XfrmAlgoAuth) Serialize() []byte {
  154. b := make([]byte, msg.Len())
  155. copy(b[0:64], msg.AlgName[:])
  156. copy(b[64:68], (*(*[4]byte)(unsafe.Pointer(&msg.AlgKeyLen)))[:])
  157. copy(b[68:72], (*(*[4]byte)(unsafe.Pointer(&msg.AlgTruncLen)))[:])
  158. copy(b[72:msg.Len()], msg.AlgKey[:])
  159. return b
  160. }
  161. // struct xfrm_algo_aead {
  162. // char alg_name[64];
  163. // unsigned int alg_key_len; /* in bits */
  164. // unsigned int alg_icv_len; /* in bits */
  165. // char alg_key[0];
  166. // }
  167. // struct xfrm_encap_tmpl {
  168. // __u16 encap_type;
  169. // __be16 encap_sport;
  170. // __be16 encap_dport;
  171. // xfrm_address_t encap_oa;
  172. // };
  173. type XfrmEncapTmpl struct {
  174. EncapType uint16
  175. EncapSport uint16 // big endian
  176. EncapDport uint16 // big endian
  177. Pad [2]byte
  178. EncapOa XfrmAddress
  179. }
  180. func (msg *XfrmEncapTmpl) Len() int {
  181. return SizeofXfrmEncapTmpl
  182. }
  183. func DeserializeXfrmEncapTmpl(b []byte) *XfrmEncapTmpl {
  184. return (*XfrmEncapTmpl)(unsafe.Pointer(&b[0:SizeofXfrmEncapTmpl][0]))
  185. }
  186. func (msg *XfrmEncapTmpl) Serialize() []byte {
  187. return (*(*[SizeofXfrmEncapTmpl]byte)(unsafe.Pointer(msg)))[:]
  188. }