Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2015 Cisco and/or its affiliates. |
| 3 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | * you may not use this file except in compliance with the License. |
| 5 | * You may obtain a copy of the License at: |
| 6 | * |
| 7 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | * |
| 9 | * Unless required by applicable law or agreed to in writing, software |
| 10 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | * See the License for the specific language governing permissions and |
| 13 | * limitations under the License. |
| 14 | */ |
Sergio Gonzalez Monroy | a10f62b | 2016-11-25 13:36:12 +0000 | [diff] [blame] | 15 | #ifndef __ESP_H__ |
| 16 | #define __ESP_H__ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 17 | |
Sergio Gonzalez Monroy | db93cd9 | 2017-08-26 15:22:05 +0100 | [diff] [blame] | 18 | #include <vnet/ip/ip.h> |
Damjan Marion | 91f17dc | 2019-03-18 18:59:25 +0100 | [diff] [blame] | 19 | #include <vnet/crypto/crypto.h> |
Sergio Gonzalez Monroy | db93cd9 | 2017-08-26 15:22:05 +0100 | [diff] [blame] | 20 | #include <vnet/ipsec/ipsec.h> |
Arthur de Kerhor | ad95b06 | 2022-11-16 19:12:05 +0100 | [diff] [blame] | 21 | #include <vnet/ipsec/ipsec.api_enum.h> |
Sergio Gonzalez Monroy | db93cd9 | 2017-08-26 15:22:05 +0100 | [diff] [blame] | 22 | |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 23 | typedef struct |
| 24 | { |
Neale Ranns | 41afb33 | 2019-07-16 06:19:35 -0700 | [diff] [blame] | 25 | union |
| 26 | { |
| 27 | u32 spi; |
| 28 | u8 spi_bytes[4]; |
| 29 | }; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 30 | u32 seq; |
| 31 | u8 data[0]; |
| 32 | } esp_header_t; |
| 33 | |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 34 | typedef struct |
| 35 | { |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 36 | u8 pad_length; |
| 37 | u8 next_header; |
| 38 | } esp_footer_t; |
| 39 | |
| 40 | typedef CLIB_PACKED (struct { |
| 41 | ip4_header_t ip4; |
| 42 | esp_header_t esp; |
| 43 | }) ip4_and_esp_header_t; |
| 44 | |
| 45 | typedef CLIB_PACKED (struct { |
Klement Sekera | 4b089f2 | 2018-04-17 18:04:57 +0200 | [diff] [blame] | 46 | ip4_header_t ip4; |
| 47 | udp_header_t udp; |
| 48 | esp_header_t esp; |
| 49 | }) ip4_and_udp_and_esp_header_t; |
Klement Sekera | 4b089f2 | 2018-04-17 18:04:57 +0200 | [diff] [blame] | 50 | |
Klement Sekera | 4b089f2 | 2018-04-17 18:04:57 +0200 | [diff] [blame] | 51 | typedef CLIB_PACKED (struct { |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 52 | ip6_header_t ip6; |
| 53 | esp_header_t esp; |
| 54 | }) ip6_and_esp_header_t; |
| 55 | |
Neale Ranns | 47feb11 | 2019-04-11 15:14:07 +0000 | [diff] [blame] | 56 | /** |
Benoît Ganne | 490b927 | 2021-01-22 18:03:09 +0100 | [diff] [blame] | 57 | * AES counter mode nonce |
| 58 | */ |
| 59 | typedef struct |
| 60 | { |
| 61 | u32 salt; |
| 62 | u64 iv; |
| 63 | u32 ctr; /* counter: 1 in big-endian for ctr, unused for gcm */ |
| 64 | } __clib_packed esp_ctr_nonce_t; |
| 65 | |
| 66 | STATIC_ASSERT_SIZEOF (esp_ctr_nonce_t, 16); |
| 67 | |
| 68 | /** |
Neale Ranns | 47feb11 | 2019-04-11 15:14:07 +0000 | [diff] [blame] | 69 | * AES GCM Additional Authentication data |
| 70 | */ |
| 71 | typedef struct esp_aead_t_ |
| 72 | { |
| 73 | /** |
| 74 | * for GCM: when using ESN it's: |
| 75 | * SPI, seq-hi, seg-low |
| 76 | * else |
| 77 | * SPI, seq-low |
| 78 | */ |
| 79 | u32 data[3]; |
| 80 | } __clib_packed esp_aead_t; |
| 81 | |
Damjan Marion | c59b9a2 | 2019-03-19 15:38:40 +0100 | [diff] [blame] | 82 | #define ESP_SEQ_MAX (4294967295UL) |
Sergio Gonzalez Monroy | a10f62b | 2016-11-25 13:36:12 +0000 | [diff] [blame] | 83 | |
Sergio Gonzalez Monroy | db93cd9 | 2017-08-26 15:22:05 +0100 | [diff] [blame] | 84 | u8 *format_esp_header (u8 * s, va_list * args); |
Sergio Gonzalez Monroy | a10f62b | 2016-11-25 13:36:12 +0000 | [diff] [blame] | 85 | |
Sergio Gonzalez Monroy | a10f62b | 2016-11-25 13:36:12 +0000 | [diff] [blame] | 86 | /* TODO seq increment should be atomic to be accessed by multiple workers */ |
Sergio Gonzalez Monroy | a10f62b | 2016-11-25 13:36:12 +0000 | [diff] [blame] | 87 | always_inline int |
| 88 | esp_seq_advance (ipsec_sa_t * sa) |
| 89 | { |
Damjan Marion | 1e3aa5e | 2019-03-28 10:58:59 +0100 | [diff] [blame] | 90 | if (PREDICT_TRUE (ipsec_sa_is_set_USE_ESN (sa))) |
Sergio Gonzalez Monroy | a10f62b | 2016-11-25 13:36:12 +0000 | [diff] [blame] | 91 | { |
| 92 | if (PREDICT_FALSE (sa->seq == ESP_SEQ_MAX)) |
| 93 | { |
Damjan Marion | d709cbc | 2019-03-26 13:16:42 +0100 | [diff] [blame] | 94 | if (PREDICT_FALSE (ipsec_sa_is_set_USE_ANTI_REPLAY (sa) && |
| 95 | sa->seq_hi == ESP_SEQ_MAX)) |
Sergio Gonzalez Monroy | a10f62b | 2016-11-25 13:36:12 +0000 | [diff] [blame] | 96 | return 1; |
| 97 | sa->seq_hi++; |
| 98 | } |
| 99 | sa->seq++; |
| 100 | } |
| 101 | else |
| 102 | { |
Damjan Marion | d709cbc | 2019-03-26 13:16:42 +0100 | [diff] [blame] | 103 | if (PREDICT_FALSE (ipsec_sa_is_set_USE_ANTI_REPLAY (sa) && |
| 104 | sa->seq == ESP_SEQ_MAX)) |
Sergio Gonzalez Monroy | a10f62b | 2016-11-25 13:36:12 +0000 | [diff] [blame] | 105 | return 1; |
| 106 | sa->seq++; |
| 107 | } |
| 108 | |
| 109 | return 0; |
| 110 | } |
| 111 | |
Fan Zhang | f539578 | 2020-04-29 14:00:03 +0100 | [diff] [blame] | 112 | always_inline u16 |
Neale Ranns | 5b89110 | 2021-06-28 13:31:28 +0000 | [diff] [blame] | 113 | esp_aad_fill (u8 *data, const esp_header_t *esp, const ipsec_sa_t *sa, |
| 114 | u32 seq_hi) |
Neale Ranns | 47feb11 | 2019-04-11 15:14:07 +0000 | [diff] [blame] | 115 | { |
| 116 | esp_aead_t *aad; |
| 117 | |
Fan Zhang | f539578 | 2020-04-29 14:00:03 +0100 | [diff] [blame] | 118 | aad = (esp_aead_t *) data; |
Damjan Marion | 30b8b4a | 2019-05-29 18:49:25 +0200 | [diff] [blame] | 119 | aad->data[0] = esp->spi; |
Neale Ranns | 47feb11 | 2019-04-11 15:14:07 +0000 | [diff] [blame] | 120 | |
| 121 | if (ipsec_sa_is_set_USE_ESN (sa)) |
| 122 | { |
| 123 | /* SPI, seq-hi, seq-low */ |
Neale Ranns | 5b89110 | 2021-06-28 13:31:28 +0000 | [diff] [blame] | 124 | aad->data[1] = (u32) clib_host_to_net_u32 (seq_hi); |
Damjan Marion | 30b8b4a | 2019-05-29 18:49:25 +0200 | [diff] [blame] | 125 | aad->data[2] = esp->seq; |
Fan Zhang | f539578 | 2020-04-29 14:00:03 +0100 | [diff] [blame] | 126 | return 12; |
Neale Ranns | 47feb11 | 2019-04-11 15:14:07 +0000 | [diff] [blame] | 127 | } |
| 128 | else |
Damjan Marion | 30b8b4a | 2019-05-29 18:49:25 +0200 | [diff] [blame] | 129 | { |
| 130 | /* SPI, seq-low */ |
| 131 | aad->data[1] = esp->seq; |
Fan Zhang | f539578 | 2020-04-29 14:00:03 +0100 | [diff] [blame] | 132 | return 8; |
Damjan Marion | 30b8b4a | 2019-05-29 18:49:25 +0200 | [diff] [blame] | 133 | } |
Neale Ranns | 47feb11 | 2019-04-11 15:14:07 +0000 | [diff] [blame] | 134 | } |
Fan Zhang | f539578 | 2020-04-29 14:00:03 +0100 | [diff] [blame] | 135 | |
Arthur de Kerhor | ad95b06 | 2022-11-16 19:12:05 +0100 | [diff] [blame] | 136 | always_inline u32 |
| 137 | esp_encrypt_err_to_sa_err (u32 err) |
Fan Zhang | 18f0e31 | 2020-10-19 13:08:34 +0100 | [diff] [blame] | 138 | { |
Arthur de Kerhor | ad95b06 | 2022-11-16 19:12:05 +0100 | [diff] [blame] | 139 | switch (err) |
| 140 | { |
| 141 | case ESP_ENCRYPT_ERROR_HANDOFF: |
| 142 | return IPSEC_SA_ERROR_HANDOFF; |
| 143 | case ESP_ENCRYPT_ERROR_SEQ_CYCLED: |
| 144 | return IPSEC_SA_ERROR_SEQ_CYCLED; |
| 145 | case ESP_ENCRYPT_ERROR_CRYPTO_ENGINE_ERROR: |
| 146 | return IPSEC_SA_ERROR_CRYPTO_ENGINE_ERROR; |
| 147 | case ESP_ENCRYPT_ERROR_CRYPTO_QUEUE_FULL: |
| 148 | return IPSEC_SA_ERROR_CRYPTO_QUEUE_FULL; |
| 149 | case ESP_ENCRYPT_ERROR_NO_BUFFERS: |
| 150 | return IPSEC_SA_ERROR_NO_BUFFERS; |
| 151 | case ESP_ENCRYPT_ERROR_NO_ENCRYPTION: |
| 152 | return IPSEC_SA_ERROR_NO_ENCRYPTION; |
| 153 | } |
| 154 | return ~0; |
| 155 | } |
| 156 | |
| 157 | always_inline u32 |
| 158 | esp_decrypt_err_to_sa_err (u32 err) |
| 159 | { |
| 160 | switch (err) |
| 161 | { |
| 162 | case ESP_DECRYPT_ERROR_HANDOFF: |
| 163 | return IPSEC_SA_ERROR_HANDOFF; |
| 164 | case ESP_DECRYPT_ERROR_DECRYPTION_FAILED: |
| 165 | return IPSEC_SA_ERROR_DECRYPTION_FAILED; |
| 166 | case ESP_DECRYPT_ERROR_INTEG_ERROR: |
| 167 | return IPSEC_SA_ERROR_INTEG_ERROR; |
| 168 | case ESP_DECRYPT_ERROR_CRYPTO_ENGINE_ERROR: |
| 169 | return IPSEC_SA_ERROR_CRYPTO_ENGINE_ERROR; |
| 170 | case ESP_DECRYPT_ERROR_REPLAY: |
| 171 | return IPSEC_SA_ERROR_REPLAY; |
| 172 | case ESP_DECRYPT_ERROR_RUNT: |
| 173 | return IPSEC_SA_ERROR_RUNT; |
| 174 | case ESP_DECRYPT_ERROR_NO_BUFFERS: |
| 175 | return IPSEC_SA_ERROR_NO_BUFFERS; |
| 176 | case ESP_DECRYPT_ERROR_OVERSIZED_HEADER: |
| 177 | return IPSEC_SA_ERROR_OVERSIZED_HEADER; |
| 178 | case ESP_DECRYPT_ERROR_NO_TAIL_SPACE: |
| 179 | return IPSEC_SA_ERROR_NO_TAIL_SPACE; |
| 180 | case ESP_DECRYPT_ERROR_TUN_NO_PROTO: |
| 181 | return IPSEC_SA_ERROR_TUN_NO_PROTO; |
| 182 | case ESP_DECRYPT_ERROR_UNSUP_PAYLOAD: |
| 183 | return IPSEC_SA_ERROR_UNSUP_PAYLOAD; |
| 184 | } |
| 185 | return ~0; |
| 186 | } |
| 187 | |
| 188 | always_inline void |
| 189 | esp_encrypt_set_next_index (vlib_buffer_t *b, vlib_node_runtime_t *node, |
| 190 | u32 thread_index, u32 err, u16 index, u16 *nexts, |
| 191 | u16 drop_next, u32 sa_index) |
| 192 | { |
| 193 | ipsec_set_next_index (b, node, thread_index, err, |
| 194 | esp_encrypt_err_to_sa_err (err), index, nexts, |
| 195 | drop_next, sa_index); |
| 196 | } |
| 197 | |
| 198 | always_inline void |
| 199 | esp_decrypt_set_next_index (vlib_buffer_t *b, vlib_node_runtime_t *node, |
| 200 | u32 thread_index, u32 err, u16 index, u16 *nexts, |
| 201 | u16 drop_next, u32 sa_index) |
| 202 | { |
| 203 | ipsec_set_next_index (b, node, thread_index, err, |
| 204 | esp_decrypt_err_to_sa_err (err), index, nexts, |
| 205 | drop_next, sa_index); |
Fan Zhang | 18f0e31 | 2020-10-19 13:08:34 +0100 | [diff] [blame] | 206 | } |
| 207 | |
Fan Zhang | f539578 | 2020-04-29 14:00:03 +0100 | [diff] [blame] | 208 | /** |
| 209 | * The post data structure to for esp_encrypt/decrypt_inline to write to |
| 210 | * vib_buffer_t opaque unused field, and for post nodes to pick up after |
| 211 | * dequeue. |
| 212 | **/ |
| 213 | typedef struct |
| 214 | { |
| 215 | union |
| 216 | { |
| 217 | struct |
| 218 | { |
| 219 | u8 icv_sz; |
| 220 | u8 iv_sz; |
| 221 | ipsec_sa_flags_t flags; |
| 222 | u32 sa_index; |
| 223 | }; |
| 224 | u64 sa_data; |
| 225 | }; |
| 226 | |
| 227 | u32 seq; |
| 228 | i16 current_data; |
| 229 | i16 current_length; |
| 230 | u16 hdr_sz; |
| 231 | u16 is_chain; |
Neale Ranns | 5b89110 | 2021-06-28 13:31:28 +0000 | [diff] [blame] | 232 | u32 seq_hi; |
Fan Zhang | f539578 | 2020-04-29 14:00:03 +0100 | [diff] [blame] | 233 | } esp_decrypt_packet_data_t; |
| 234 | |
| 235 | STATIC_ASSERT_SIZEOF (esp_decrypt_packet_data_t, 3 * sizeof (u64)); |
Benoît Ganne | 490b927 | 2021-01-22 18:03:09 +0100 | [diff] [blame] | 236 | STATIC_ASSERT_OFFSET_OF (esp_decrypt_packet_data_t, seq, sizeof (u64)); |
Fan Zhang | f539578 | 2020-04-29 14:00:03 +0100 | [diff] [blame] | 237 | |
| 238 | /* we are forced to store the decrypt post data into 2 separate places - |
| 239 | vlib_opaque and opaque2. */ |
| 240 | typedef struct |
| 241 | { |
| 242 | vlib_buffer_t *lb; |
| 243 | u32 free_buffer_index; |
| 244 | u8 icv_removed; |
| 245 | } esp_decrypt_packet_data2_t; |
| 246 | |
| 247 | typedef union |
| 248 | { |
| 249 | u16 next_index; |
| 250 | esp_decrypt_packet_data_t decrypt_data; |
| 251 | } esp_post_data_t; |
| 252 | |
| 253 | STATIC_ASSERT (sizeof (esp_post_data_t) <= |
| 254 | STRUCT_SIZE_OF (vnet_buffer_opaque_t, unused), |
| 255 | "Custom meta-data too large for vnet_buffer_opaque_t"); |
| 256 | |
| 257 | #define esp_post_data(b) \ |
| 258 | ((esp_post_data_t *)((u8 *)((b)->opaque) \ |
| 259 | + STRUCT_OFFSET_OF (vnet_buffer_opaque_t, unused))) |
| 260 | |
| 261 | STATIC_ASSERT (sizeof (esp_decrypt_packet_data2_t) <= |
| 262 | STRUCT_SIZE_OF (vnet_buffer_opaque2_t, unused), |
| 263 | "Custom meta-data too large for vnet_buffer_opaque2_t"); |
| 264 | |
| 265 | #define esp_post_data2(b) \ |
| 266 | ((esp_decrypt_packet_data2_t *)((u8 *)((b)->opaque2) \ |
| 267 | + STRUCT_OFFSET_OF (vnet_buffer_opaque2_t, unused))) |
| 268 | |
| 269 | typedef struct |
| 270 | { |
| 271 | /* esp post node index for async crypto */ |
| 272 | u32 esp4_post_next; |
| 273 | u32 esp6_post_next; |
| 274 | u32 esp4_tun_post_next; |
| 275 | u32 esp6_tun_post_next; |
Neale Ranns | 4a58e49 | 2020-12-21 13:19:10 +0000 | [diff] [blame] | 276 | u32 esp_mpls_tun_post_next; |
Fan Zhang | f539578 | 2020-04-29 14:00:03 +0100 | [diff] [blame] | 277 | } esp_async_post_next_t; |
| 278 | |
| 279 | extern esp_async_post_next_t esp_encrypt_async_next; |
| 280 | extern esp_async_post_next_t esp_decrypt_async_next; |
| 281 | |
Xiaoming Jiang | 0c1454c | 2023-05-05 02:28:20 +0000 | [diff] [blame] | 282 | /* when submitting a frame is failed, drop all buffers in the frame */ |
| 283 | always_inline u32 |
| 284 | esp_async_recycle_failed_submit (vlib_main_t *vm, vnet_crypto_async_frame_t *f, |
| 285 | vlib_node_runtime_t *node, u32 err, |
| 286 | u32 ipsec_sa_err, u16 index, u32 *from, |
| 287 | u16 *nexts, u16 drop_next_index, |
| 288 | bool is_encrypt) |
| 289 | { |
| 290 | vlib_buffer_t *b; |
| 291 | u32 n_drop = f->n_elts; |
| 292 | u32 *bi = f->buffer_indices; |
| 293 | |
| 294 | while (n_drop--) |
| 295 | { |
| 296 | u32 sa_index; |
| 297 | |
| 298 | from[index] = bi[0]; |
| 299 | b = vlib_get_buffer (vm, bi[0]); |
| 300 | |
| 301 | if (is_encrypt) |
| 302 | { |
| 303 | sa_index = vnet_buffer (b)->ipsec.sad_index; |
| 304 | } |
| 305 | else |
| 306 | { |
| 307 | sa_index = esp_post_data (b)->decrypt_data.sa_index; |
| 308 | } |
| 309 | |
| 310 | ipsec_set_next_index (b, node, vm->thread_index, err, ipsec_sa_err, |
| 311 | index, nexts, drop_next_index, sa_index); |
| 312 | bi++; |
| 313 | index++; |
| 314 | } |
| 315 | |
| 316 | return (f->n_elts); |
| 317 | } |
| 318 | |
Sergio Gonzalez Monroy | a10f62b | 2016-11-25 13:36:12 +0000 | [diff] [blame] | 319 | #endif /* __ESP_H__ */ |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 320 | |
| 321 | /* |
| 322 | * fd.io coding-style-patch-verification: ON |
| 323 | * |
| 324 | * Local Variables: |
| 325 | * eval: (c-set-style "gnu") |
| 326 | * End: |
| 327 | */ |