xfrm_linux.go 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306
  1. package nl
  2. import (
  3. "bytes"
  4. "net"
  5. "unsafe"
  6. )
  7. // Infinity for packet and byte counts
  8. const (
  9. XFRM_INF = ^uint64(0)
  10. )
  11. type XfrmMsgType uint8
  12. type XfrmMsg interface {
  13. Type() XfrmMsgType
  14. }
  15. // Message Types
  16. const (
  17. XFRM_MSG_BASE XfrmMsgType = 0x10
  18. XFRM_MSG_NEWSA = 0x10
  19. XFRM_MSG_DELSA = 0x11
  20. XFRM_MSG_GETSA = 0x12
  21. XFRM_MSG_NEWPOLICY = 0x13
  22. XFRM_MSG_DELPOLICY = 0x14
  23. XFRM_MSG_GETPOLICY = 0x15
  24. XFRM_MSG_ALLOCSPI = 0x16
  25. XFRM_MSG_ACQUIRE = 0x17
  26. XFRM_MSG_EXPIRE = 0x18
  27. XFRM_MSG_UPDPOLICY = 0x19
  28. XFRM_MSG_UPDSA = 0x1a
  29. XFRM_MSG_POLEXPIRE = 0x1b
  30. XFRM_MSG_FLUSHSA = 0x1c
  31. XFRM_MSG_FLUSHPOLICY = 0x1d
  32. XFRM_MSG_NEWAE = 0x1e
  33. XFRM_MSG_GETAE = 0x1f
  34. XFRM_MSG_REPORT = 0x20
  35. XFRM_MSG_MIGRATE = 0x21
  36. XFRM_MSG_NEWSADINFO = 0x22
  37. XFRM_MSG_GETSADINFO = 0x23
  38. XFRM_MSG_NEWSPDINFO = 0x24
  39. XFRM_MSG_GETSPDINFO = 0x25
  40. XFRM_MSG_MAPPING = 0x26
  41. XFRM_MSG_MAX = 0x26
  42. XFRM_NR_MSGTYPES = 0x17
  43. )
  44. // Attribute types
  45. const (
  46. /* Netlink message attributes. */
  47. XFRMA_UNSPEC = iota
  48. XFRMA_ALG_AUTH /* struct xfrm_algo */
  49. XFRMA_ALG_CRYPT /* struct xfrm_algo */
  50. XFRMA_ALG_COMP /* struct xfrm_algo */
  51. XFRMA_ENCAP /* struct xfrm_algo + struct xfrm_encap_tmpl */
  52. XFRMA_TMPL /* 1 or more struct xfrm_user_tmpl */
  53. XFRMA_SA /* struct xfrm_usersa_info */
  54. XFRMA_POLICY /* struct xfrm_userpolicy_info */
  55. XFRMA_SEC_CTX /* struct xfrm_sec_ctx */
  56. XFRMA_LTIME_VAL
  57. XFRMA_REPLAY_VAL
  58. XFRMA_REPLAY_THRESH
  59. XFRMA_ETIMER_THRESH
  60. XFRMA_SRCADDR /* xfrm_address_t */
  61. XFRMA_COADDR /* xfrm_address_t */
  62. XFRMA_LASTUSED /* unsigned long */
  63. XFRMA_POLICY_TYPE /* struct xfrm_userpolicy_type */
  64. XFRMA_MIGRATE
  65. XFRMA_ALG_AEAD /* struct xfrm_algo_aead */
  66. XFRMA_KMADDRESS /* struct xfrm_user_kmaddress */
  67. XFRMA_ALG_AUTH_TRUNC /* struct xfrm_algo_auth */
  68. XFRMA_MARK /* struct xfrm_mark */
  69. XFRMA_TFCPAD /* __u32 */
  70. XFRMA_REPLAY_ESN_VAL /* struct xfrm_replay_esn */
  71. XFRMA_SA_EXTRA_FLAGS /* __u32 */
  72. XFRMA_PROTO /* __u8 */
  73. XFRMA_ADDRESS_FILTER /* struct xfrm_address_filter */
  74. XFRMA_PAD
  75. XFRMA_OFFLOAD_DEV /* struct xfrm_state_offload */
  76. XFRMA_SET_MARK /* __u32 */
  77. XFRMA_SET_MARK_MASK /* __u32 */
  78. XFRMA_IF_ID /* __u32 */
  79. XFRMA_MAX = iota - 1
  80. )
  81. const XFRMA_OUTPUT_MARK = XFRMA_SET_MARK
  82. const (
  83. SizeofXfrmAddress = 0x10
  84. SizeofXfrmSelector = 0x38
  85. SizeofXfrmLifetimeCfg = 0x40
  86. SizeofXfrmLifetimeCur = 0x20
  87. SizeofXfrmId = 0x18
  88. SizeofXfrmMark = 0x08
  89. )
  90. // Netlink groups
  91. const (
  92. XFRMNLGRP_NONE = 0x0
  93. XFRMNLGRP_ACQUIRE = 0x1
  94. XFRMNLGRP_EXPIRE = 0x2
  95. XFRMNLGRP_SA = 0x3
  96. XFRMNLGRP_POLICY = 0x4
  97. XFRMNLGRP_AEVENTS = 0x5
  98. XFRMNLGRP_REPORT = 0x6
  99. XFRMNLGRP_MIGRATE = 0x7
  100. XFRMNLGRP_MAPPING = 0x8
  101. __XFRMNLGRP_MAX = 0x9
  102. )
  103. // typedef union {
  104. // __be32 a4;
  105. // __be32 a6[4];
  106. // } xfrm_address_t;
  107. type XfrmAddress [SizeofXfrmAddress]byte
  108. func (x *XfrmAddress) ToIP() net.IP {
  109. var empty = [12]byte{}
  110. ip := make(net.IP, net.IPv6len)
  111. if bytes.Equal(x[4:16], empty[:]) {
  112. ip[10] = 0xff
  113. ip[11] = 0xff
  114. copy(ip[12:16], x[0:4])
  115. } else {
  116. copy(ip[:], x[:])
  117. }
  118. return ip
  119. }
  120. func (x *XfrmAddress) ToIPNet(prefixlen uint8) *net.IPNet {
  121. ip := x.ToIP()
  122. if GetIPFamily(ip) == FAMILY_V4 {
  123. return &net.IPNet{IP: ip, Mask: net.CIDRMask(int(prefixlen), 32)}
  124. }
  125. return &net.IPNet{IP: ip, Mask: net.CIDRMask(int(prefixlen), 128)}
  126. }
  127. func (x *XfrmAddress) FromIP(ip net.IP) {
  128. var empty = [16]byte{}
  129. if len(ip) < net.IPv4len {
  130. copy(x[4:16], empty[:])
  131. } else if GetIPFamily(ip) == FAMILY_V4 {
  132. copy(x[0:4], ip.To4()[0:4])
  133. copy(x[4:16], empty[:12])
  134. } else {
  135. copy(x[0:16], ip.To16()[0:16])
  136. }
  137. }
  138. func DeserializeXfrmAddress(b []byte) *XfrmAddress {
  139. return (*XfrmAddress)(unsafe.Pointer(&b[0:SizeofXfrmAddress][0]))
  140. }
  141. func (x *XfrmAddress) Serialize() []byte {
  142. return (*(*[SizeofXfrmAddress]byte)(unsafe.Pointer(x)))[:]
  143. }
  144. // struct xfrm_selector {
  145. // xfrm_address_t daddr;
  146. // xfrm_address_t saddr;
  147. // __be16 dport;
  148. // __be16 dport_mask;
  149. // __be16 sport;
  150. // __be16 sport_mask;
  151. // __u16 family;
  152. // __u8 prefixlen_d;
  153. // __u8 prefixlen_s;
  154. // __u8 proto;
  155. // int ifindex;
  156. // __kernel_uid32_t user;
  157. // };
  158. type XfrmSelector struct {
  159. Daddr XfrmAddress
  160. Saddr XfrmAddress
  161. Dport uint16 // big endian
  162. DportMask uint16 // big endian
  163. Sport uint16 // big endian
  164. SportMask uint16 // big endian
  165. Family uint16
  166. PrefixlenD uint8
  167. PrefixlenS uint8
  168. Proto uint8
  169. Pad [3]byte
  170. Ifindex int32
  171. User uint32
  172. }
  173. func (msg *XfrmSelector) Len() int {
  174. return SizeofXfrmSelector
  175. }
  176. func DeserializeXfrmSelector(b []byte) *XfrmSelector {
  177. return (*XfrmSelector)(unsafe.Pointer(&b[0:SizeofXfrmSelector][0]))
  178. }
  179. func (msg *XfrmSelector) Serialize() []byte {
  180. return (*(*[SizeofXfrmSelector]byte)(unsafe.Pointer(msg)))[:]
  181. }
  182. // struct xfrm_lifetime_cfg {
  183. // __u64 soft_byte_limit;
  184. // __u64 hard_byte_limit;
  185. // __u64 soft_packet_limit;
  186. // __u64 hard_packet_limit;
  187. // __u64 soft_add_expires_seconds;
  188. // __u64 hard_add_expires_seconds;
  189. // __u64 soft_use_expires_seconds;
  190. // __u64 hard_use_expires_seconds;
  191. // };
  192. //
  193. type XfrmLifetimeCfg struct {
  194. SoftByteLimit uint64
  195. HardByteLimit uint64
  196. SoftPacketLimit uint64
  197. HardPacketLimit uint64
  198. SoftAddExpiresSeconds uint64
  199. HardAddExpiresSeconds uint64
  200. SoftUseExpiresSeconds uint64
  201. HardUseExpiresSeconds uint64
  202. }
  203. func (msg *XfrmLifetimeCfg) Len() int {
  204. return SizeofXfrmLifetimeCfg
  205. }
  206. func DeserializeXfrmLifetimeCfg(b []byte) *XfrmLifetimeCfg {
  207. return (*XfrmLifetimeCfg)(unsafe.Pointer(&b[0:SizeofXfrmLifetimeCfg][0]))
  208. }
  209. func (msg *XfrmLifetimeCfg) Serialize() []byte {
  210. return (*(*[SizeofXfrmLifetimeCfg]byte)(unsafe.Pointer(msg)))[:]
  211. }
  212. // struct xfrm_lifetime_cur {
  213. // __u64 bytes;
  214. // __u64 packets;
  215. // __u64 add_time;
  216. // __u64 use_time;
  217. // };
  218. type XfrmLifetimeCur struct {
  219. Bytes uint64
  220. Packets uint64
  221. AddTime uint64
  222. UseTime uint64
  223. }
  224. func (msg *XfrmLifetimeCur) Len() int {
  225. return SizeofXfrmLifetimeCur
  226. }
  227. func DeserializeXfrmLifetimeCur(b []byte) *XfrmLifetimeCur {
  228. return (*XfrmLifetimeCur)(unsafe.Pointer(&b[0:SizeofXfrmLifetimeCur][0]))
  229. }
  230. func (msg *XfrmLifetimeCur) Serialize() []byte {
  231. return (*(*[SizeofXfrmLifetimeCur]byte)(unsafe.Pointer(msg)))[:]
  232. }
  233. // struct xfrm_id {
  234. // xfrm_address_t daddr;
  235. // __be32 spi;
  236. // __u8 proto;
  237. // };
  238. type XfrmId struct {
  239. Daddr XfrmAddress
  240. Spi uint32 // big endian
  241. Proto uint8
  242. Pad [3]byte
  243. }
  244. func (msg *XfrmId) Len() int {
  245. return SizeofXfrmId
  246. }
  247. func DeserializeXfrmId(b []byte) *XfrmId {
  248. return (*XfrmId)(unsafe.Pointer(&b[0:SizeofXfrmId][0]))
  249. }
  250. func (msg *XfrmId) Serialize() []byte {
  251. return (*(*[SizeofXfrmId]byte)(unsafe.Pointer(msg)))[:]
  252. }
  253. type XfrmMark struct {
  254. Value uint32
  255. Mask uint32
  256. }
  257. func (msg *XfrmMark) Len() int {
  258. return SizeofXfrmMark
  259. }
  260. func DeserializeXfrmMark(b []byte) *XfrmMark {
  261. return (*XfrmMark)(unsafe.Pointer(&b[0:SizeofXfrmMark][0]))
  262. }
  263. func (msg *XfrmMark) Serialize() []byte {
  264. return (*(*[SizeofXfrmMark]byte)(unsafe.Pointer(msg)))[:]
  265. }