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