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> |
| 21 | |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 22 | typedef struct |
| 23 | { |
Neale Ranns | 41afb33 | 2019-07-16 06:19:35 -0700 | [diff] [blame] | 24 | union |
| 25 | { |
| 26 | u32 spi; |
| 27 | u8 spi_bytes[4]; |
| 28 | }; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 29 | u32 seq; |
| 30 | u8 data[0]; |
| 31 | } esp_header_t; |
| 32 | |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 33 | typedef struct |
| 34 | { |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 35 | u8 pad_length; |
| 36 | u8 next_header; |
| 37 | } esp_footer_t; |
| 38 | |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 39 | /* *INDENT-OFF* */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 40 | typedef CLIB_PACKED (struct { |
| 41 | ip4_header_t ip4; |
| 42 | esp_header_t esp; |
| 43 | }) ip4_and_esp_header_t; |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 44 | /* *INDENT-ON* */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 45 | |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 46 | /* *INDENT-OFF* */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 47 | typedef CLIB_PACKED (struct { |
Klement Sekera | 4b089f2 | 2018-04-17 18:04:57 +0200 | [diff] [blame] | 48 | ip4_header_t ip4; |
| 49 | udp_header_t udp; |
| 50 | esp_header_t esp; |
| 51 | }) ip4_and_udp_and_esp_header_t; |
| 52 | /* *INDENT-ON* */ |
| 53 | |
| 54 | /* *INDENT-OFF* */ |
| 55 | typedef CLIB_PACKED (struct { |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 56 | ip6_header_t ip6; |
| 57 | esp_header_t esp; |
| 58 | }) ip6_and_esp_header_t; |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 59 | /* *INDENT-ON* */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 60 | |
Neale Ranns | 47feb11 | 2019-04-11 15:14:07 +0000 | [diff] [blame] | 61 | /** |
Benoît Ganne | 490b927 | 2021-01-22 18:03:09 +0100 | [diff] [blame] | 62 | * AES counter mode nonce |
| 63 | */ |
| 64 | typedef struct |
| 65 | { |
| 66 | u32 salt; |
| 67 | u64 iv; |
| 68 | u32 ctr; /* counter: 1 in big-endian for ctr, unused for gcm */ |
| 69 | } __clib_packed esp_ctr_nonce_t; |
| 70 | |
| 71 | STATIC_ASSERT_SIZEOF (esp_ctr_nonce_t, 16); |
| 72 | |
| 73 | /** |
Neale Ranns | 47feb11 | 2019-04-11 15:14:07 +0000 | [diff] [blame] | 74 | * AES GCM Additional Authentication data |
| 75 | */ |
| 76 | typedef struct esp_aead_t_ |
| 77 | { |
| 78 | /** |
| 79 | * for GCM: when using ESN it's: |
| 80 | * SPI, seq-hi, seg-low |
| 81 | * else |
| 82 | * SPI, seq-low |
| 83 | */ |
| 84 | u32 data[3]; |
| 85 | } __clib_packed esp_aead_t; |
| 86 | |
Damjan Marion | c59b9a2 | 2019-03-19 15:38:40 +0100 | [diff] [blame] | 87 | #define ESP_SEQ_MAX (4294967295UL) |
| 88 | #define ESP_MAX_BLOCK_SIZE (16) |
Damjan Marion | b4fff3a | 2019-03-25 15:54:40 +0100 | [diff] [blame] | 89 | #define ESP_MAX_IV_SIZE (16) |
Neale Ranns | 1091c4a | 2019-04-08 14:48:23 +0000 | [diff] [blame] | 90 | #define ESP_MAX_ICV_SIZE (32) |
Sergio Gonzalez Monroy | a10f62b | 2016-11-25 13:36:12 +0000 | [diff] [blame] | 91 | |
Sergio Gonzalez Monroy | db93cd9 | 2017-08-26 15:22:05 +0100 | [diff] [blame] | 92 | u8 *format_esp_header (u8 * s, va_list * args); |
Sergio Gonzalez Monroy | a10f62b | 2016-11-25 13:36:12 +0000 | [diff] [blame] | 93 | |
Sergio Gonzalez Monroy | a10f62b | 2016-11-25 13:36:12 +0000 | [diff] [blame] | 94 | /* 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] | 95 | always_inline int |
| 96 | esp_seq_advance (ipsec_sa_t * sa) |
| 97 | { |
Damjan Marion | 1e3aa5e | 2019-03-28 10:58:59 +0100 | [diff] [blame] | 98 | if (PREDICT_TRUE (ipsec_sa_is_set_USE_ESN (sa))) |
Sergio Gonzalez Monroy | a10f62b | 2016-11-25 13:36:12 +0000 | [diff] [blame] | 99 | { |
| 100 | if (PREDICT_FALSE (sa->seq == ESP_SEQ_MAX)) |
| 101 | { |
Damjan Marion | d709cbc | 2019-03-26 13:16:42 +0100 | [diff] [blame] | 102 | if (PREDICT_FALSE (ipsec_sa_is_set_USE_ANTI_REPLAY (sa) && |
| 103 | sa->seq_hi == ESP_SEQ_MAX)) |
Sergio Gonzalez Monroy | a10f62b | 2016-11-25 13:36:12 +0000 | [diff] [blame] | 104 | return 1; |
| 105 | sa->seq_hi++; |
| 106 | } |
| 107 | sa->seq++; |
| 108 | } |
| 109 | else |
| 110 | { |
Damjan Marion | d709cbc | 2019-03-26 13:16:42 +0100 | [diff] [blame] | 111 | if (PREDICT_FALSE (ipsec_sa_is_set_USE_ANTI_REPLAY (sa) && |
| 112 | sa->seq == ESP_SEQ_MAX)) |
Sergio Gonzalez Monroy | a10f62b | 2016-11-25 13:36:12 +0000 | [diff] [blame] | 113 | return 1; |
| 114 | sa->seq++; |
| 115 | } |
| 116 | |
| 117 | return 0; |
| 118 | } |
| 119 | |
Fan Zhang | f539578 | 2020-04-29 14:00:03 +0100 | [diff] [blame] | 120 | always_inline u16 |
| 121 | esp_aad_fill (u8 * data, const esp_header_t * esp, const ipsec_sa_t * sa) |
Neale Ranns | 47feb11 | 2019-04-11 15:14:07 +0000 | [diff] [blame] | 122 | { |
| 123 | esp_aead_t *aad; |
| 124 | |
Fan Zhang | f539578 | 2020-04-29 14:00:03 +0100 | [diff] [blame] | 125 | aad = (esp_aead_t *) data; |
Damjan Marion | 30b8b4a | 2019-05-29 18:49:25 +0200 | [diff] [blame] | 126 | aad->data[0] = esp->spi; |
Neale Ranns | 47feb11 | 2019-04-11 15:14:07 +0000 | [diff] [blame] | 127 | |
| 128 | if (ipsec_sa_is_set_USE_ESN (sa)) |
| 129 | { |
| 130 | /* SPI, seq-hi, seq-low */ |
Fan Zhang | f539578 | 2020-04-29 14:00:03 +0100 | [diff] [blame] | 131 | aad->data[1] = (u32) clib_host_to_net_u32 (sa->seq_hi); |
Damjan Marion | 30b8b4a | 2019-05-29 18:49:25 +0200 | [diff] [blame] | 132 | aad->data[2] = esp->seq; |
Fan Zhang | f539578 | 2020-04-29 14:00:03 +0100 | [diff] [blame] | 133 | return 12; |
Neale Ranns | 47feb11 | 2019-04-11 15:14:07 +0000 | [diff] [blame] | 134 | } |
| 135 | else |
Damjan Marion | 30b8b4a | 2019-05-29 18:49:25 +0200 | [diff] [blame] | 136 | { |
| 137 | /* SPI, seq-low */ |
| 138 | aad->data[1] = esp->seq; |
Fan Zhang | f539578 | 2020-04-29 14:00:03 +0100 | [diff] [blame] | 139 | return 8; |
Damjan Marion | 30b8b4a | 2019-05-29 18:49:25 +0200 | [diff] [blame] | 140 | } |
Neale Ranns | 47feb11 | 2019-04-11 15:14:07 +0000 | [diff] [blame] | 141 | } |
Fan Zhang | f539578 | 2020-04-29 14:00:03 +0100 | [diff] [blame] | 142 | |
Fan Zhang | 18f0e31 | 2020-10-19 13:08:34 +0100 | [diff] [blame] | 143 | /* Special case to drop or hand off packets for sync/async modes. |
| 144 | * |
| 145 | * Different than sync mode, async mode only enqueue drop or hand-off packets |
| 146 | * to next nodes. |
| 147 | */ |
| 148 | always_inline void |
Neale Ranns | f16e9a5 | 2021-02-25 19:09:24 +0000 | [diff] [blame] | 149 | esp_set_next_index (vlib_buffer_t *b, vlib_node_runtime_t *node, u32 err, |
| 150 | u16 index, u16 *nexts, u16 drop_next) |
Fan Zhang | 18f0e31 | 2020-10-19 13:08:34 +0100 | [diff] [blame] | 151 | { |
Neale Ranns | f16e9a5 | 2021-02-25 19:09:24 +0000 | [diff] [blame] | 152 | nexts[index] = drop_next; |
| 153 | b->error = node->errors[err]; |
Fan Zhang | 18f0e31 | 2020-10-19 13:08:34 +0100 | [diff] [blame] | 154 | } |
| 155 | |
| 156 | /* when submitting a frame is failed, drop all buffers in the frame */ |
Neale Ranns | f16e9a5 | 2021-02-25 19:09:24 +0000 | [diff] [blame] | 157 | always_inline u32 |
| 158 | esp_async_recycle_failed_submit (vlib_main_t *vm, vnet_crypto_async_frame_t *f, |
| 159 | vlib_node_runtime_t *node, u32 err, u16 index, |
| 160 | u32 *from, u16 *nexts, u16 drop_next_index) |
Fan Zhang | 18f0e31 | 2020-10-19 13:08:34 +0100 | [diff] [blame] | 161 | { |
| 162 | u32 n_drop = f->n_elts; |
| 163 | u32 *bi = f->buffer_indices; |
Neale Ranns | f16e9a5 | 2021-02-25 19:09:24 +0000 | [diff] [blame] | 164 | |
Fan Zhang | 18f0e31 | 2020-10-19 13:08:34 +0100 | [diff] [blame] | 165 | while (n_drop--) |
| 166 | { |
Neale Ranns | f16e9a5 | 2021-02-25 19:09:24 +0000 | [diff] [blame] | 167 | from[index] = bi[0]; |
| 168 | esp_set_next_index (vlib_get_buffer (vm, bi[0]), node, err, index, nexts, |
| 169 | drop_next_index); |
Fan Zhang | 18f0e31 | 2020-10-19 13:08:34 +0100 | [diff] [blame] | 170 | bi++; |
Neale Ranns | f16e9a5 | 2021-02-25 19:09:24 +0000 | [diff] [blame] | 171 | index++; |
Fan Zhang | 18f0e31 | 2020-10-19 13:08:34 +0100 | [diff] [blame] | 172 | } |
| 173 | vnet_crypto_async_reset_frame (f); |
Neale Ranns | f16e9a5 | 2021-02-25 19:09:24 +0000 | [diff] [blame] | 174 | |
| 175 | return (f->n_elts); |
Fan Zhang | 18f0e31 | 2020-10-19 13:08:34 +0100 | [diff] [blame] | 176 | } |
| 177 | |
Fan Zhang | f539578 | 2020-04-29 14:00:03 +0100 | [diff] [blame] | 178 | /** |
| 179 | * The post data structure to for esp_encrypt/decrypt_inline to write to |
| 180 | * vib_buffer_t opaque unused field, and for post nodes to pick up after |
| 181 | * dequeue. |
| 182 | **/ |
| 183 | typedef struct |
| 184 | { |
| 185 | union |
| 186 | { |
| 187 | struct |
| 188 | { |
| 189 | u8 icv_sz; |
| 190 | u8 iv_sz; |
| 191 | ipsec_sa_flags_t flags; |
| 192 | u32 sa_index; |
| 193 | }; |
| 194 | u64 sa_data; |
| 195 | }; |
| 196 | |
| 197 | u32 seq; |
| 198 | i16 current_data; |
| 199 | i16 current_length; |
| 200 | u16 hdr_sz; |
| 201 | u16 is_chain; |
| 202 | u32 protect_index; |
| 203 | } esp_decrypt_packet_data_t; |
| 204 | |
| 205 | STATIC_ASSERT_SIZEOF (esp_decrypt_packet_data_t, 3 * sizeof (u64)); |
Benoît Ganne | 490b927 | 2021-01-22 18:03:09 +0100 | [diff] [blame] | 206 | STATIC_ASSERT_OFFSET_OF (esp_decrypt_packet_data_t, seq, sizeof (u64)); |
Fan Zhang | f539578 | 2020-04-29 14:00:03 +0100 | [diff] [blame] | 207 | |
| 208 | /* we are forced to store the decrypt post data into 2 separate places - |
| 209 | vlib_opaque and opaque2. */ |
| 210 | typedef struct |
| 211 | { |
| 212 | vlib_buffer_t *lb; |
| 213 | u32 free_buffer_index; |
| 214 | u8 icv_removed; |
| 215 | } esp_decrypt_packet_data2_t; |
| 216 | |
| 217 | typedef union |
| 218 | { |
| 219 | u16 next_index; |
| 220 | esp_decrypt_packet_data_t decrypt_data; |
| 221 | } esp_post_data_t; |
| 222 | |
| 223 | STATIC_ASSERT (sizeof (esp_post_data_t) <= |
| 224 | STRUCT_SIZE_OF (vnet_buffer_opaque_t, unused), |
| 225 | "Custom meta-data too large for vnet_buffer_opaque_t"); |
| 226 | |
| 227 | #define esp_post_data(b) \ |
| 228 | ((esp_post_data_t *)((u8 *)((b)->opaque) \ |
| 229 | + STRUCT_OFFSET_OF (vnet_buffer_opaque_t, unused))) |
| 230 | |
| 231 | STATIC_ASSERT (sizeof (esp_decrypt_packet_data2_t) <= |
| 232 | STRUCT_SIZE_OF (vnet_buffer_opaque2_t, unused), |
| 233 | "Custom meta-data too large for vnet_buffer_opaque2_t"); |
| 234 | |
| 235 | #define esp_post_data2(b) \ |
| 236 | ((esp_decrypt_packet_data2_t *)((u8 *)((b)->opaque2) \ |
| 237 | + STRUCT_OFFSET_OF (vnet_buffer_opaque2_t, unused))) |
| 238 | |
| 239 | typedef struct |
| 240 | { |
| 241 | /* esp post node index for async crypto */ |
| 242 | u32 esp4_post_next; |
| 243 | u32 esp6_post_next; |
| 244 | u32 esp4_tun_post_next; |
| 245 | u32 esp6_tun_post_next; |
Neale Ranns | 4a58e49 | 2020-12-21 13:19:10 +0000 | [diff] [blame] | 246 | u32 esp_mpls_tun_post_next; |
Fan Zhang | f539578 | 2020-04-29 14:00:03 +0100 | [diff] [blame] | 247 | } esp_async_post_next_t; |
| 248 | |
| 249 | extern esp_async_post_next_t esp_encrypt_async_next; |
| 250 | extern esp_async_post_next_t esp_decrypt_async_next; |
| 251 | |
Sergio Gonzalez Monroy | a10f62b | 2016-11-25 13:36:12 +0000 | [diff] [blame] | 252 | #endif /* __ESP_H__ */ |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 253 | |
| 254 | /* |
| 255 | * fd.io coding-style-patch-verification: ON |
| 256 | * |
| 257 | * Local Variables: |
| 258 | * eval: (c-set-style "gnu") |
| 259 | * End: |
| 260 | */ |