github.com/sagernet/netlink@v0.0.0-20240612041022-b9a21c07ac6a/nl/xfrm_state_linux.go (about) 1 package nl 2 3 import ( 4 "unsafe" 5 ) 6 7 const ( 8 SizeofXfrmUsersaId = 0x18 9 SizeofXfrmStats = 0x0c 10 SizeofXfrmUsersaInfo = 0xe0 11 SizeofXfrmUserSpiInfo = 0xe8 12 SizeofXfrmAlgo = 0x44 13 SizeofXfrmAlgoAuth = 0x48 14 SizeofXfrmAlgoAEAD = 0x48 15 SizeofXfrmEncapTmpl = 0x18 16 SizeofXfrmUsersaFlush = 0x1 17 SizeofXfrmReplayStateEsn = 0x18 18 SizeofXfrmReplayState = 0x0c 19 ) 20 21 const ( 22 XFRM_STATE_NOECN = 1 23 XFRM_STATE_DECAP_DSCP = 2 24 XFRM_STATE_NOPMTUDISC = 4 25 XFRM_STATE_WILDRECV = 8 26 XFRM_STATE_ICMP = 16 27 XFRM_STATE_AF_UNSPEC = 32 28 XFRM_STATE_ALIGN4 = 64 29 XFRM_STATE_ESN = 128 30 ) 31 32 const ( 33 XFRM_SA_XFLAG_DONT_ENCAP_DSCP = 1 34 XFRM_SA_XFLAG_OSEQ_MAY_WRAP = 2 35 ) 36 37 // struct xfrm_usersa_id { 38 // xfrm_address_t daddr; 39 // __be32 spi; 40 // __u16 family; 41 // __u8 proto; 42 // }; 43 44 type XfrmUsersaId struct { 45 Daddr XfrmAddress 46 Spi uint32 // big endian 47 Family uint16 48 Proto uint8 49 Pad byte 50 } 51 52 func (msg *XfrmUsersaId) Len() int { 53 return SizeofXfrmUsersaId 54 } 55 56 func DeserializeXfrmUsersaId(b []byte) *XfrmUsersaId { 57 return (*XfrmUsersaId)(unsafe.Pointer(&b[0:SizeofXfrmUsersaId][0])) 58 } 59 60 func (msg *XfrmUsersaId) Serialize() []byte { 61 return (*(*[SizeofXfrmUsersaId]byte)(unsafe.Pointer(msg)))[:] 62 } 63 64 // struct xfrm_stats { 65 // __u32 replay_window; 66 // __u32 replay; 67 // __u32 integrity_failed; 68 // }; 69 70 type XfrmStats struct { 71 ReplayWindow uint32 72 Replay uint32 73 IntegrityFailed uint32 74 } 75 76 func (msg *XfrmStats) Len() int { 77 return SizeofXfrmStats 78 } 79 80 func DeserializeXfrmStats(b []byte) *XfrmStats { 81 return (*XfrmStats)(unsafe.Pointer(&b[0:SizeofXfrmStats][0])) 82 } 83 84 func (msg *XfrmStats) Serialize() []byte { 85 return (*(*[SizeofXfrmStats]byte)(unsafe.Pointer(msg)))[:] 86 } 87 88 // struct xfrm_usersa_info { 89 // struct xfrm_selector sel; 90 // struct xfrm_id id; 91 // xfrm_address_t saddr; 92 // struct xfrm_lifetime_cfg lft; 93 // struct xfrm_lifetime_cur curlft; 94 // struct xfrm_stats stats; 95 // __u32 seq; 96 // __u32 reqid; 97 // __u16 family; 98 // __u8 mode; /* XFRM_MODE_xxx */ 99 // __u8 replay_window; 100 // __u8 flags; 101 // #define XFRM_STATE_NOECN 1 102 // #define XFRM_STATE_DECAP_DSCP 2 103 // #define XFRM_STATE_NOPMTUDISC 4 104 // #define XFRM_STATE_WILDRECV 8 105 // #define XFRM_STATE_ICMP 16 106 // #define XFRM_STATE_AF_UNSPEC 32 107 // #define XFRM_STATE_ALIGN4 64 108 // #define XFRM_STATE_ESN 128 109 // }; 110 // 111 // #define XFRM_SA_XFLAG_DONT_ENCAP_DSCP 1 112 // #define XFRM_SA_XFLAG_OSEQ_MAY_WRAP 2 113 // 114 115 type XfrmUsersaInfo struct { 116 Sel XfrmSelector 117 Id XfrmId 118 Saddr XfrmAddress 119 Lft XfrmLifetimeCfg 120 Curlft XfrmLifetimeCur 121 Stats XfrmStats 122 Seq uint32 123 Reqid uint32 124 Family uint16 125 Mode uint8 126 ReplayWindow uint8 127 Flags uint8 128 Pad [7]byte 129 } 130 131 func (msg *XfrmUsersaInfo) Len() int { 132 return SizeofXfrmUsersaInfo 133 } 134 135 func DeserializeXfrmUsersaInfo(b []byte) *XfrmUsersaInfo { 136 return (*XfrmUsersaInfo)(unsafe.Pointer(&b[0:SizeofXfrmUsersaInfo][0])) 137 } 138 139 func (msg *XfrmUsersaInfo) Serialize() []byte { 140 return (*(*[SizeofXfrmUsersaInfo]byte)(unsafe.Pointer(msg)))[:] 141 } 142 143 // struct xfrm_userspi_info { 144 // struct xfrm_usersa_info info; 145 // __u32 min; 146 // __u32 max; 147 // }; 148 149 type XfrmUserSpiInfo struct { 150 XfrmUsersaInfo XfrmUsersaInfo 151 Min uint32 152 Max uint32 153 } 154 155 func (msg *XfrmUserSpiInfo) Len() int { 156 return SizeofXfrmUserSpiInfo 157 } 158 159 func DeserializeXfrmUserSpiInfo(b []byte) *XfrmUserSpiInfo { 160 return (*XfrmUserSpiInfo)(unsafe.Pointer(&b[0:SizeofXfrmUserSpiInfo][0])) 161 } 162 163 func (msg *XfrmUserSpiInfo) Serialize() []byte { 164 return (*(*[SizeofXfrmUserSpiInfo]byte)(unsafe.Pointer(msg)))[:] 165 } 166 167 // struct xfrm_algo { 168 // char alg_name[64]; 169 // unsigned int alg_key_len; /* in bits */ 170 // char alg_key[0]; 171 // }; 172 173 type XfrmAlgo struct { 174 AlgName [64]byte 175 AlgKeyLen uint32 176 AlgKey []byte 177 } 178 179 func (msg *XfrmAlgo) Len() int { 180 return SizeofXfrmAlgo + int(msg.AlgKeyLen/8) 181 } 182 183 func DeserializeXfrmAlgo(b []byte) *XfrmAlgo { 184 ret := XfrmAlgo{} 185 copy(ret.AlgName[:], b[0:64]) 186 ret.AlgKeyLen = *(*uint32)(unsafe.Pointer(&b[64])) 187 ret.AlgKey = b[68:ret.Len()] 188 return &ret 189 } 190 191 func (msg *XfrmAlgo) Serialize() []byte { 192 b := make([]byte, msg.Len()) 193 copy(b[0:64], msg.AlgName[:]) 194 copy(b[64:68], (*(*[4]byte)(unsafe.Pointer(&msg.AlgKeyLen)))[:]) 195 copy(b[68:msg.Len()], msg.AlgKey[:]) 196 return b 197 } 198 199 // struct xfrm_algo_auth { 200 // char alg_name[64]; 201 // unsigned int alg_key_len; /* in bits */ 202 // unsigned int alg_trunc_len; /* in bits */ 203 // char alg_key[0]; 204 // }; 205 206 type XfrmAlgoAuth struct { 207 AlgName [64]byte 208 AlgKeyLen uint32 209 AlgTruncLen uint32 210 AlgKey []byte 211 } 212 213 func (msg *XfrmAlgoAuth) Len() int { 214 return SizeofXfrmAlgoAuth + int(msg.AlgKeyLen/8) 215 } 216 217 func DeserializeXfrmAlgoAuth(b []byte) *XfrmAlgoAuth { 218 ret := XfrmAlgoAuth{} 219 copy(ret.AlgName[:], b[0:64]) 220 ret.AlgKeyLen = *(*uint32)(unsafe.Pointer(&b[64])) 221 ret.AlgTruncLen = *(*uint32)(unsafe.Pointer(&b[68])) 222 ret.AlgKey = b[72:ret.Len()] 223 return &ret 224 } 225 226 func (msg *XfrmAlgoAuth) Serialize() []byte { 227 b := make([]byte, msg.Len()) 228 copy(b[0:64], msg.AlgName[:]) 229 copy(b[64:68], (*(*[4]byte)(unsafe.Pointer(&msg.AlgKeyLen)))[:]) 230 copy(b[68:72], (*(*[4]byte)(unsafe.Pointer(&msg.AlgTruncLen)))[:]) 231 copy(b[72:msg.Len()], msg.AlgKey[:]) 232 return b 233 } 234 235 // struct xfrm_algo_aead { 236 // char alg_name[64]; 237 // unsigned int alg_key_len; /* in bits */ 238 // unsigned int alg_icv_len; /* in bits */ 239 // char alg_key[0]; 240 // } 241 242 type XfrmAlgoAEAD struct { 243 AlgName [64]byte 244 AlgKeyLen uint32 245 AlgICVLen uint32 246 AlgKey []byte 247 } 248 249 func (msg *XfrmAlgoAEAD) Len() int { 250 return SizeofXfrmAlgoAEAD + int(msg.AlgKeyLen/8) 251 } 252 253 func DeserializeXfrmAlgoAEAD(b []byte) *XfrmAlgoAEAD { 254 ret := XfrmAlgoAEAD{} 255 copy(ret.AlgName[:], b[0:64]) 256 ret.AlgKeyLen = *(*uint32)(unsafe.Pointer(&b[64])) 257 ret.AlgICVLen = *(*uint32)(unsafe.Pointer(&b[68])) 258 ret.AlgKey = b[72:ret.Len()] 259 return &ret 260 } 261 262 func (msg *XfrmAlgoAEAD) Serialize() []byte { 263 b := make([]byte, msg.Len()) 264 copy(b[0:64], msg.AlgName[:]) 265 copy(b[64:68], (*(*[4]byte)(unsafe.Pointer(&msg.AlgKeyLen)))[:]) 266 copy(b[68:72], (*(*[4]byte)(unsafe.Pointer(&msg.AlgICVLen)))[:]) 267 copy(b[72:msg.Len()], msg.AlgKey[:]) 268 return b 269 } 270 271 // struct xfrm_encap_tmpl { 272 // __u16 encap_type; 273 // __be16 encap_sport; 274 // __be16 encap_dport; 275 // xfrm_address_t encap_oa; 276 // }; 277 278 type XfrmEncapTmpl struct { 279 EncapType uint16 280 EncapSport uint16 // big endian 281 EncapDport uint16 // big endian 282 Pad [2]byte 283 EncapOa XfrmAddress 284 } 285 286 func (msg *XfrmEncapTmpl) Len() int { 287 return SizeofXfrmEncapTmpl 288 } 289 290 func DeserializeXfrmEncapTmpl(b []byte) *XfrmEncapTmpl { 291 return (*XfrmEncapTmpl)(unsafe.Pointer(&b[0:SizeofXfrmEncapTmpl][0])) 292 } 293 294 func (msg *XfrmEncapTmpl) Serialize() []byte { 295 return (*(*[SizeofXfrmEncapTmpl]byte)(unsafe.Pointer(msg)))[:] 296 } 297 298 // struct xfrm_usersa_flush { 299 // __u8 proto; 300 // }; 301 302 type XfrmUsersaFlush struct { 303 Proto uint8 304 } 305 306 func (msg *XfrmUsersaFlush) Len() int { 307 return SizeofXfrmUsersaFlush 308 } 309 310 func DeserializeXfrmUsersaFlush(b []byte) *XfrmUsersaFlush { 311 return (*XfrmUsersaFlush)(unsafe.Pointer(&b[0:SizeofXfrmUsersaFlush][0])) 312 } 313 314 func (msg *XfrmUsersaFlush) Serialize() []byte { 315 return (*(*[SizeofXfrmUsersaFlush]byte)(unsafe.Pointer(msg)))[:] 316 } 317 318 // struct xfrm_replay_state_esn { 319 // unsigned int bmp_len; 320 // __u32 oseq; 321 // __u32 seq; 322 // __u32 oseq_hi; 323 // __u32 seq_hi; 324 // __u32 replay_window; 325 // __u32 bmp[0]; 326 // }; 327 328 type XfrmReplayStateEsn struct { 329 BmpLen uint32 330 OSeq uint32 331 Seq uint32 332 OSeqHi uint32 333 SeqHi uint32 334 ReplayWindow uint32 335 Bmp []uint32 336 } 337 338 func (msg *XfrmReplayStateEsn) Serialize() []byte { 339 // We deliberately do not pass Bmp, as it gets set by the kernel. 340 return (*(*[SizeofXfrmReplayStateEsn]byte)(unsafe.Pointer(msg)))[:] 341 } 342 343 // struct xfrm_replay_state { 344 // __u32 oseq; 345 // __u32 seq; 346 // __u32 bitmap; 347 // }; 348 349 type XfrmReplayState struct { 350 OSeq uint32 351 Seq uint32 352 BitMap uint32 353 } 354 355 func DeserializeXfrmReplayState(b []byte) *XfrmReplayState { 356 return (*XfrmReplayState)(unsafe.Pointer(&b[0:SizeofXfrmReplayState][0])) 357 } 358 359 func (msg *XfrmReplayState) Serialize() []byte { 360 return (*(*[SizeofXfrmReplayState]byte)(unsafe.Pointer(msg)))[:] 361 }