xfrm_linux.go 6.2 KB

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