xfrm_linux.go 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318
  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_MTIMER_THRESH /* __u32 in seconds for input SA */
  80. XFRMA_SA_DIR /* __u8 */
  81. XFRMA_NAT_KEEPALIVE_INTERVAL /* __u32 in seconds for NAT keepalive */
  82. XFRMA_SA_PCPU /* __u32 */
  83. XFRMA_MAX = iota - 1
  84. )
  85. const XFRMA_OUTPUT_MARK = XFRMA_SET_MARK
  86. const (
  87. SizeofXfrmAddress = 0x10
  88. SizeofXfrmSelector = 0x38
  89. SizeofXfrmLifetimeCfg = 0x40
  90. SizeofXfrmLifetimeCur = 0x20
  91. SizeofXfrmId = 0x18
  92. SizeofXfrmMark = 0x08
  93. )
  94. // Netlink groups
  95. const (
  96. XFRMNLGRP_NONE = 0x0
  97. XFRMNLGRP_ACQUIRE = 0x1
  98. XFRMNLGRP_EXPIRE = 0x2
  99. XFRMNLGRP_SA = 0x3
  100. XFRMNLGRP_POLICY = 0x4
  101. XFRMNLGRP_AEVENTS = 0x5
  102. XFRMNLGRP_REPORT = 0x6
  103. XFRMNLGRP_MIGRATE = 0x7
  104. XFRMNLGRP_MAPPING = 0x8
  105. __XFRMNLGRP_MAX = 0x9
  106. )
  107. // typedef union {
  108. // __be32 a4;
  109. // __be32 a6[4];
  110. // } xfrm_address_t;
  111. type XfrmAddress [SizeofXfrmAddress]byte
  112. func (x *XfrmAddress) ToIP() net.IP {
  113. var empty = [12]byte{}
  114. ip := make(net.IP, net.IPv6len)
  115. if bytes.Equal(x[4:16], empty[:]) {
  116. ip[10] = 0xff
  117. ip[11] = 0xff
  118. copy(ip[12:16], x[0:4])
  119. } else {
  120. copy(ip[:], x[:])
  121. }
  122. return ip
  123. }
  124. // family is only used when x and prefixlen are both 0
  125. func (x *XfrmAddress) ToIPNet(prefixlen uint8, family uint16) *net.IPNet {
  126. empty := [SizeofXfrmAddress]byte{}
  127. if bytes.Equal(x[:], empty[:]) && prefixlen == 0 {
  128. if family == FAMILY_V6 {
  129. return &net.IPNet{IP: net.ParseIP("::"), Mask: net.CIDRMask(int(prefixlen), 128)}
  130. }
  131. return &net.IPNet{IP: net.ParseIP("0.0.0.0"), Mask: net.CIDRMask(int(prefixlen), 32)}
  132. }
  133. ip := x.ToIP()
  134. if GetIPFamily(ip) == FAMILY_V4 {
  135. return &net.IPNet{IP: ip, Mask: net.CIDRMask(int(prefixlen), 32)}
  136. }
  137. return &net.IPNet{IP: ip, Mask: net.CIDRMask(int(prefixlen), 128)}
  138. }
  139. func (x *XfrmAddress) FromIP(ip net.IP) {
  140. var empty = [16]byte{}
  141. if len(ip) < net.IPv4len {
  142. copy(x[4:16], empty[:])
  143. } else if GetIPFamily(ip) == FAMILY_V4 {
  144. copy(x[0:4], ip.To4()[0:4])
  145. copy(x[4:16], empty[:12])
  146. } else {
  147. copy(x[0:16], ip.To16()[0:16])
  148. }
  149. }
  150. func DeserializeXfrmAddress(b []byte) *XfrmAddress {
  151. return (*XfrmAddress)(unsafe.Pointer(&b[0:SizeofXfrmAddress][0]))
  152. }
  153. func (x *XfrmAddress) Serialize() []byte {
  154. return (*(*[SizeofXfrmAddress]byte)(unsafe.Pointer(x)))[:]
  155. }
  156. // struct xfrm_selector {
  157. // xfrm_address_t daddr;
  158. // xfrm_address_t saddr;
  159. // __be16 dport;
  160. // __be16 dport_mask;
  161. // __be16 sport;
  162. // __be16 sport_mask;
  163. // __u16 family;
  164. // __u8 prefixlen_d;
  165. // __u8 prefixlen_s;
  166. // __u8 proto;
  167. // int ifindex;
  168. // __kernel_uid32_t user;
  169. // };
  170. type XfrmSelector struct {
  171. Daddr XfrmAddress
  172. Saddr XfrmAddress
  173. Dport uint16 // big endian
  174. DportMask uint16 // big endian
  175. Sport uint16 // big endian
  176. SportMask uint16 // big endian
  177. Family uint16
  178. PrefixlenD uint8
  179. PrefixlenS uint8
  180. Proto uint8
  181. Pad [3]byte
  182. Ifindex int32
  183. User uint32
  184. }
  185. func (msg *XfrmSelector) Len() int {
  186. return SizeofXfrmSelector
  187. }
  188. func DeserializeXfrmSelector(b []byte) *XfrmSelector {
  189. return (*XfrmSelector)(unsafe.Pointer(&b[0:SizeofXfrmSelector][0]))
  190. }
  191. func (msg *XfrmSelector) Serialize() []byte {
  192. return (*(*[SizeofXfrmSelector]byte)(unsafe.Pointer(msg)))[:]
  193. }
  194. // struct xfrm_lifetime_cfg {
  195. // __u64 soft_byte_limit;
  196. // __u64 hard_byte_limit;
  197. // __u64 soft_packet_limit;
  198. // __u64 hard_packet_limit;
  199. // __u64 soft_add_expires_seconds;
  200. // __u64 hard_add_expires_seconds;
  201. // __u64 soft_use_expires_seconds;
  202. // __u64 hard_use_expires_seconds;
  203. // };
  204. //
  205. type XfrmLifetimeCfg struct {
  206. SoftByteLimit uint64
  207. HardByteLimit uint64
  208. SoftPacketLimit uint64
  209. HardPacketLimit uint64
  210. SoftAddExpiresSeconds uint64
  211. HardAddExpiresSeconds uint64
  212. SoftUseExpiresSeconds uint64
  213. HardUseExpiresSeconds uint64
  214. }
  215. func (msg *XfrmLifetimeCfg) Len() int {
  216. return SizeofXfrmLifetimeCfg
  217. }
  218. func DeserializeXfrmLifetimeCfg(b []byte) *XfrmLifetimeCfg {
  219. return (*XfrmLifetimeCfg)(unsafe.Pointer(&b[0:SizeofXfrmLifetimeCfg][0]))
  220. }
  221. func (msg *XfrmLifetimeCfg) Serialize() []byte {
  222. return (*(*[SizeofXfrmLifetimeCfg]byte)(unsafe.Pointer(msg)))[:]
  223. }
  224. // struct xfrm_lifetime_cur {
  225. // __u64 bytes;
  226. // __u64 packets;
  227. // __u64 add_time;
  228. // __u64 use_time;
  229. // };
  230. type XfrmLifetimeCur struct {
  231. Bytes uint64
  232. Packets uint64
  233. AddTime uint64
  234. UseTime uint64
  235. }
  236. func (msg *XfrmLifetimeCur) Len() int {
  237. return SizeofXfrmLifetimeCur
  238. }
  239. func DeserializeXfrmLifetimeCur(b []byte) *XfrmLifetimeCur {
  240. return (*XfrmLifetimeCur)(unsafe.Pointer(&b[0:SizeofXfrmLifetimeCur][0]))
  241. }
  242. func (msg *XfrmLifetimeCur) Serialize() []byte {
  243. return (*(*[SizeofXfrmLifetimeCur]byte)(unsafe.Pointer(msg)))[:]
  244. }
  245. // struct xfrm_id {
  246. // xfrm_address_t daddr;
  247. // __be32 spi;
  248. // __u8 proto;
  249. // };
  250. type XfrmId struct {
  251. Daddr XfrmAddress
  252. Spi uint32 // big endian
  253. Proto uint8
  254. Pad [3]byte
  255. }
  256. func (msg *XfrmId) Len() int {
  257. return SizeofXfrmId
  258. }
  259. func DeserializeXfrmId(b []byte) *XfrmId {
  260. return (*XfrmId)(unsafe.Pointer(&b[0:SizeofXfrmId][0]))
  261. }
  262. func (msg *XfrmId) Serialize() []byte {
  263. return (*(*[SizeofXfrmId]byte)(unsafe.Pointer(msg)))[:]
  264. }
  265. type XfrmMark struct {
  266. Value uint32
  267. Mask uint32
  268. }
  269. func (msg *XfrmMark) Len() int {
  270. return SizeofXfrmMark
  271. }
  272. func DeserializeXfrmMark(b []byte) *XfrmMark {
  273. return (*XfrmMark)(unsafe.Pointer(&b[0:SizeofXfrmMark][0]))
  274. }
  275. func (msg *XfrmMark) Serialize() []byte {
  276. return (*(*[SizeofXfrmMark]byte)(unsafe.Pointer(msg)))[:]
  277. }