xfrm_linux.go 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297
  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 = 0x00
  48. XFRMA_ALG_AUTH = 0x01 /* struct xfrm_algo */
  49. XFRMA_ALG_CRYPT = 0x02 /* struct xfrm_algo */
  50. XFRMA_ALG_COMP = 0x03 /* struct xfrm_algo */
  51. XFRMA_ENCAP = 0x04 /* struct xfrm_algo + struct xfrm_encap_tmpl */
  52. XFRMA_TMPL = 0x05 /* 1 or more struct xfrm_user_tmpl */
  53. XFRMA_SA = 0x06 /* struct xfrm_usersa_info */
  54. XFRMA_POLICY = 0x07 /* struct xfrm_userpolicy_info */
  55. XFRMA_SEC_CTX = 0x08 /* struct xfrm_sec_ctx */
  56. XFRMA_LTIME_VAL = 0x09
  57. XFRMA_REPLAY_VAL = 0x0a
  58. XFRMA_REPLAY_THRESH = 0x0b
  59. XFRMA_ETIMER_THRESH = 0x0c
  60. XFRMA_SRCADDR = 0x0d /* xfrm_address_t */
  61. XFRMA_COADDR = 0x0e /* xfrm_address_t */
  62. XFRMA_LASTUSED = 0x0f /* unsigned long */
  63. XFRMA_POLICY_TYPE = 0x10 /* struct xfrm_userpolicy_type */
  64. XFRMA_MIGRATE = 0x11
  65. XFRMA_ALG_AEAD = 0x12 /* struct xfrm_algo_aead */
  66. XFRMA_KMADDRESS = 0x13 /* struct xfrm_user_kmaddress */
  67. XFRMA_ALG_AUTH_TRUNC = 0x14 /* struct xfrm_algo_auth */
  68. XFRMA_MARK = 0x15 /* struct xfrm_mark */
  69. XFRMA_TFCPAD = 0x16 /* __u32 */
  70. XFRMA_REPLAY_ESN_VAL = 0x17 /* struct xfrm_replay_esn */
  71. XFRMA_SA_EXTRA_FLAGS = 0x18 /* __u32 */
  72. XFRMA_MAX = 0x18
  73. )
  74. const (
  75. SizeofXfrmAddress = 0x10
  76. SizeofXfrmSelector = 0x38
  77. SizeofXfrmLifetimeCfg = 0x40
  78. SizeofXfrmLifetimeCur = 0x20
  79. SizeofXfrmId = 0x18
  80. SizeofXfrmMark = 0x08
  81. )
  82. // Netlink groups
  83. const (
  84. XFRMNLGRP_NONE = 0x0
  85. XFRMNLGRP_ACQUIRE = 0x1
  86. XFRMNLGRP_EXPIRE = 0x2
  87. XFRMNLGRP_SA = 0x3
  88. XFRMNLGRP_POLICY = 0x4
  89. XFRMNLGRP_AEVENTS = 0x5
  90. XFRMNLGRP_REPORT = 0x6
  91. XFRMNLGRP_MIGRATE = 0x7
  92. XFRMNLGRP_MAPPING = 0x8
  93. __XFRMNLGRP_MAX = 0x9
  94. )
  95. // typedef union {
  96. // __be32 a4;
  97. // __be32 a6[4];
  98. // } xfrm_address_t;
  99. type XfrmAddress [SizeofXfrmAddress]byte
  100. func (x *XfrmAddress) ToIP() net.IP {
  101. var empty = [12]byte{}
  102. ip := make(net.IP, net.IPv6len)
  103. if bytes.Equal(x[4:16], empty[:]) {
  104. ip[10] = 0xff
  105. ip[11] = 0xff
  106. copy(ip[12:16], x[0:4])
  107. } else {
  108. copy(ip[:], x[:])
  109. }
  110. return ip
  111. }
  112. func (x *XfrmAddress) ToIPNet(prefixlen uint8) *net.IPNet {
  113. ip := x.ToIP()
  114. if GetIPFamily(ip) == FAMILY_V4 {
  115. return &net.IPNet{IP: ip, Mask: net.CIDRMask(int(prefixlen), 32)}
  116. }
  117. return &net.IPNet{IP: ip, Mask: net.CIDRMask(int(prefixlen), 128)}
  118. }
  119. func (x *XfrmAddress) FromIP(ip net.IP) {
  120. var empty = [16]byte{}
  121. if len(ip) < net.IPv4len {
  122. copy(x[4:16], empty[:])
  123. } else if GetIPFamily(ip) == FAMILY_V4 {
  124. copy(x[0:4], ip.To4()[0:4])
  125. copy(x[4:16], empty[:12])
  126. } else {
  127. copy(x[0:16], ip.To16()[0:16])
  128. }
  129. }
  130. func DeserializeXfrmAddress(b []byte) *XfrmAddress {
  131. return (*XfrmAddress)(unsafe.Pointer(&b[0:SizeofXfrmAddress][0]))
  132. }
  133. func (x *XfrmAddress) Serialize() []byte {
  134. return (*(*[SizeofXfrmAddress]byte)(unsafe.Pointer(x)))[:]
  135. }
  136. // struct xfrm_selector {
  137. // xfrm_address_t daddr;
  138. // xfrm_address_t saddr;
  139. // __be16 dport;
  140. // __be16 dport_mask;
  141. // __be16 sport;
  142. // __be16 sport_mask;
  143. // __u16 family;
  144. // __u8 prefixlen_d;
  145. // __u8 prefixlen_s;
  146. // __u8 proto;
  147. // int ifindex;
  148. // __kernel_uid32_t user;
  149. // };
  150. type XfrmSelector struct {
  151. Daddr XfrmAddress
  152. Saddr XfrmAddress
  153. Dport uint16 // big endian
  154. DportMask uint16 // big endian
  155. Sport uint16 // big endian
  156. SportMask uint16 // big endian
  157. Family uint16
  158. PrefixlenD uint8
  159. PrefixlenS uint8
  160. Proto uint8
  161. Pad [3]byte
  162. Ifindex int32
  163. User uint32
  164. }
  165. func (msg *XfrmSelector) Len() int {
  166. return SizeofXfrmSelector
  167. }
  168. func DeserializeXfrmSelector(b []byte) *XfrmSelector {
  169. return (*XfrmSelector)(unsafe.Pointer(&b[0:SizeofXfrmSelector][0]))
  170. }
  171. func (msg *XfrmSelector) Serialize() []byte {
  172. return (*(*[SizeofXfrmSelector]byte)(unsafe.Pointer(msg)))[:]
  173. }
  174. // struct xfrm_lifetime_cfg {
  175. // __u64 soft_byte_limit;
  176. // __u64 hard_byte_limit;
  177. // __u64 soft_packet_limit;
  178. // __u64 hard_packet_limit;
  179. // __u64 soft_add_expires_seconds;
  180. // __u64 hard_add_expires_seconds;
  181. // __u64 soft_use_expires_seconds;
  182. // __u64 hard_use_expires_seconds;
  183. // };
  184. //
  185. type XfrmLifetimeCfg struct {
  186. SoftByteLimit uint64
  187. HardByteLimit uint64
  188. SoftPacketLimit uint64
  189. HardPacketLimit uint64
  190. SoftAddExpiresSeconds uint64
  191. HardAddExpiresSeconds uint64
  192. SoftUseExpiresSeconds uint64
  193. HardUseExpiresSeconds uint64
  194. }
  195. func (msg *XfrmLifetimeCfg) Len() int {
  196. return SizeofXfrmLifetimeCfg
  197. }
  198. func DeserializeXfrmLifetimeCfg(b []byte) *XfrmLifetimeCfg {
  199. return (*XfrmLifetimeCfg)(unsafe.Pointer(&b[0:SizeofXfrmLifetimeCfg][0]))
  200. }
  201. func (msg *XfrmLifetimeCfg) Serialize() []byte {
  202. return (*(*[SizeofXfrmLifetimeCfg]byte)(unsafe.Pointer(msg)))[:]
  203. }
  204. // struct xfrm_lifetime_cur {
  205. // __u64 bytes;
  206. // __u64 packets;
  207. // __u64 add_time;
  208. // __u64 use_time;
  209. // };
  210. type XfrmLifetimeCur struct {
  211. Bytes uint64
  212. Packets uint64
  213. AddTime uint64
  214. UseTime uint64
  215. }
  216. func (msg *XfrmLifetimeCur) Len() int {
  217. return SizeofXfrmLifetimeCur
  218. }
  219. func DeserializeXfrmLifetimeCur(b []byte) *XfrmLifetimeCur {
  220. return (*XfrmLifetimeCur)(unsafe.Pointer(&b[0:SizeofXfrmLifetimeCur][0]))
  221. }
  222. func (msg *XfrmLifetimeCur) Serialize() []byte {
  223. return (*(*[SizeofXfrmLifetimeCur]byte)(unsafe.Pointer(msg)))[:]
  224. }
  225. // struct xfrm_id {
  226. // xfrm_address_t daddr;
  227. // __be32 spi;
  228. // __u8 proto;
  229. // };
  230. type XfrmId struct {
  231. Daddr XfrmAddress
  232. Spi uint32 // big endian
  233. Proto uint8
  234. Pad [3]byte
  235. }
  236. func (msg *XfrmId) Len() int {
  237. return SizeofXfrmId
  238. }
  239. func DeserializeXfrmId(b []byte) *XfrmId {
  240. return (*XfrmId)(unsafe.Pointer(&b[0:SizeofXfrmId][0]))
  241. }
  242. func (msg *XfrmId) Serialize() []byte {
  243. return (*(*[SizeofXfrmId]byte)(unsafe.Pointer(msg)))[:]
  244. }
  245. type XfrmMark struct {
  246. Value uint32
  247. Mask uint32
  248. }
  249. func (msg *XfrmMark) Len() int {
  250. return SizeofXfrmMark
  251. }
  252. func DeserializeXfrmMark(b []byte) *XfrmMark {
  253. return (*XfrmMark)(unsafe.Pointer(&b[0:SizeofXfrmMark][0]))
  254. }
  255. func (msg *XfrmMark) Serialize() []byte {
  256. return (*(*[SizeofXfrmMark]byte)(unsafe.Pointer(msg)))[:]
  257. }