Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1 | /* |
| 2 | * esp_decrypt.c : IPSec ESP decrypt node |
| 3 | * |
| 4 | * Copyright (c) 2015 Cisco and/or its affiliates. |
| 5 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | * you may not use this file except in compliance with the License. |
| 7 | * You may obtain a copy of the License at: |
| 8 | * |
| 9 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | * |
| 11 | * Unless required by applicable law or agreed to in writing, software |
| 12 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | * See the License for the specific language governing permissions and |
| 15 | * limitations under the License. |
| 16 | */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 17 | #include <vnet/vnet.h> |
| 18 | #include <vnet/api_errno.h> |
| 19 | #include <vnet/ip/ip.h> |
John Lo | 90430b6 | 2020-01-31 23:48:30 -0500 | [diff] [blame] | 20 | #include <vnet/l2/l2_input.h> |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 21 | |
| 22 | #include <vnet/ipsec/ipsec.h> |
| 23 | #include <vnet/ipsec/esp.h> |
Neale Ranns | 918c161 | 2019-02-21 23:34:59 -0800 | [diff] [blame] | 24 | #include <vnet/ipsec/ipsec_io.h> |
Neale Ranns | c87b66c | 2019-02-07 07:26:12 -0800 | [diff] [blame] | 25 | #include <vnet/ipsec/ipsec_tun.h> |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 26 | |
Neale Ranns | 119c0d7 | 2020-11-26 13:12:37 +0000 | [diff] [blame] | 27 | #include <vnet/gre/packet.h> |
Neale Ranns | 568acbb | 2019-12-18 05:54:40 +0000 | [diff] [blame] | 28 | |
Neale Ranns | 4a58e49 | 2020-12-21 13:19:10 +0000 | [diff] [blame] | 29 | #define foreach_esp_decrypt_next \ |
| 30 | _ (DROP, "error-drop") \ |
| 31 | _ (IP4_INPUT, "ip4-input-no-checksum") \ |
| 32 | _ (IP6_INPUT, "ip6-input") \ |
| 33 | _ (L2_INPUT, "l2-input") \ |
| 34 | _ (MPLS_INPUT, "mpls-input") \ |
| 35 | _ (HANDOFF, "handoff") |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 36 | |
| 37 | #define _(v, s) ESP_DECRYPT_NEXT_##v, |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 38 | typedef enum |
| 39 | { |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 40 | foreach_esp_decrypt_next |
| 41 | #undef _ |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 42 | ESP_DECRYPT_N_NEXT, |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 43 | } esp_decrypt_next_t; |
| 44 | |
Neale Ranns | 4a58e49 | 2020-12-21 13:19:10 +0000 | [diff] [blame] | 45 | #define foreach_esp_decrypt_post_next \ |
| 46 | _ (DROP, "error-drop") \ |
| 47 | _ (IP4_INPUT, "ip4-input-no-checksum") \ |
| 48 | _ (IP6_INPUT, "ip6-input") \ |
| 49 | _ (MPLS_INPUT, "mpls-input") \ |
| 50 | _ (L2_INPUT, "l2-input") |
Fan Zhang | f539578 | 2020-04-29 14:00:03 +0100 | [diff] [blame] | 51 | |
| 52 | #define _(v, s) ESP_DECRYPT_POST_NEXT_##v, |
| 53 | typedef enum |
| 54 | { |
| 55 | foreach_esp_decrypt_post_next |
| 56 | #undef _ |
| 57 | ESP_DECRYPT_POST_N_NEXT, |
| 58 | } esp_decrypt_post_next_t; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 59 | |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 60 | typedef struct |
| 61 | { |
Damjan Marion | b4fff3a | 2019-03-25 15:54:40 +0100 | [diff] [blame] | 62 | u32 seq; |
Neale Ranns | 6afaae1 | 2019-07-17 15:07:14 +0000 | [diff] [blame] | 63 | u32 sa_seq; |
| 64 | u32 sa_seq_hi; |
Neale Ranns | 5b89110 | 2021-06-28 13:31:28 +0000 | [diff] [blame] | 65 | u32 pkt_seq_hi; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 66 | ipsec_crypto_alg_t crypto_alg; |
| 67 | ipsec_integ_alg_t integ_alg; |
| 68 | } esp_decrypt_trace_t; |
| 69 | |
Neale Ranns | 93688d7 | 2022-08-09 03:34:51 +0000 | [diff] [blame] | 70 | typedef vl_counter_esp_decrypt_enum_t esp_decrypt_error_t; |
| 71 | |
Neale Ranns | 5b89110 | 2021-06-28 13:31:28 +0000 | [diff] [blame] | 72 | /* The number of byres in the hisequence number */ |
| 73 | #define N_HI_ESN_BYTES 4 |
| 74 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 75 | /* packet trace format function */ |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 76 | static u8 * |
| 77 | format_esp_decrypt_trace (u8 * s, va_list * args) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 78 | { |
| 79 | CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *); |
| 80 | CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *); |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 81 | esp_decrypt_trace_t *t = va_arg (*args, esp_decrypt_trace_t *); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 82 | |
Neale Ranns | 5b89110 | 2021-06-28 13:31:28 +0000 | [diff] [blame] | 83 | s = format (s, |
| 84 | "esp: crypto %U integrity %U pkt-seq %d sa-seq %u sa-seq-hi %u " |
| 85 | "pkt-seq-hi %u", |
| 86 | format_ipsec_crypto_alg, t->crypto_alg, format_ipsec_integ_alg, |
| 87 | t->integ_alg, t->seq, t->sa_seq, t->sa_seq_hi, t->pkt_seq_hi); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 88 | return s; |
| 89 | } |
| 90 | |
Damjan Marion | b4fff3a | 2019-03-25 15:54:40 +0100 | [diff] [blame] | 91 | #define ESP_ENCRYPT_PD_F_FD_TRANSPORT (1 << 2) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 92 | |
Filip Tehlar | efcad1a | 2020-02-04 09:36:04 +0000 | [diff] [blame] | 93 | static_always_inline void |
| 94 | esp_process_ops (vlib_main_t * vm, vlib_node_runtime_t * node, |
| 95 | vnet_crypto_op_t * ops, vlib_buffer_t * b[], u16 * nexts, |
| 96 | int e) |
| 97 | { |
| 98 | vnet_crypto_op_t *op = ops; |
| 99 | u32 n_fail, n_ops = vec_len (ops); |
| 100 | |
| 101 | if (n_ops == 0) |
| 102 | return; |
| 103 | |
| 104 | n_fail = n_ops - vnet_crypto_process_ops (vm, op, n_ops); |
| 105 | |
| 106 | while (n_fail) |
| 107 | { |
| 108 | ASSERT (op - ops < n_ops); |
| 109 | if (op->status != VNET_CRYPTO_OP_STATUS_COMPLETED) |
| 110 | { |
| 111 | u32 err, bi = op->user_data; |
| 112 | if (op->status == VNET_CRYPTO_OP_STATUS_FAIL_BAD_HMAC) |
| 113 | err = e; |
| 114 | else |
| 115 | err = ESP_DECRYPT_ERROR_CRYPTO_ENGINE_ERROR; |
Arthur de Kerhor | ad95b06 | 2022-11-16 19:12:05 +0100 | [diff] [blame] | 116 | esp_decrypt_set_next_index (b[bi], node, vm->thread_index, err, bi, |
| 117 | nexts, ESP_DECRYPT_NEXT_DROP, |
| 118 | vnet_buffer (b[bi])->ipsec.sad_index); |
Filip Tehlar | efcad1a | 2020-02-04 09:36:04 +0000 | [diff] [blame] | 119 | n_fail--; |
| 120 | } |
| 121 | op++; |
| 122 | } |
| 123 | } |
| 124 | |
| 125 | static_always_inline void |
| 126 | esp_process_chained_ops (vlib_main_t * vm, vlib_node_runtime_t * node, |
| 127 | vnet_crypto_op_t * ops, vlib_buffer_t * b[], |
| 128 | u16 * nexts, vnet_crypto_op_chunk_t * chunks, int e) |
| 129 | { |
| 130 | |
| 131 | vnet_crypto_op_t *op = ops; |
| 132 | u32 n_fail, n_ops = vec_len (ops); |
| 133 | |
Neale Ranns | f16e9a5 | 2021-02-25 19:09:24 +0000 | [diff] [blame] | 134 | if (PREDICT_TRUE (n_ops == 0)) |
Filip Tehlar | efcad1a | 2020-02-04 09:36:04 +0000 | [diff] [blame] | 135 | return; |
| 136 | |
| 137 | n_fail = n_ops - vnet_crypto_process_chained_ops (vm, op, chunks, n_ops); |
| 138 | |
| 139 | while (n_fail) |
| 140 | { |
| 141 | ASSERT (op - ops < n_ops); |
| 142 | if (op->status != VNET_CRYPTO_OP_STATUS_COMPLETED) |
| 143 | { |
| 144 | u32 err, bi = op->user_data; |
| 145 | if (op->status == VNET_CRYPTO_OP_STATUS_FAIL_BAD_HMAC) |
| 146 | err = e; |
| 147 | else |
| 148 | err = ESP_DECRYPT_ERROR_CRYPTO_ENGINE_ERROR; |
Arthur de Kerhor | ad95b06 | 2022-11-16 19:12:05 +0100 | [diff] [blame] | 149 | esp_decrypt_set_next_index (b[bi], node, vm->thread_index, err, bi, |
| 150 | nexts, ESP_DECRYPT_NEXT_DROP, |
| 151 | vnet_buffer (b[bi])->ipsec.sad_index); |
Filip Tehlar | efcad1a | 2020-02-04 09:36:04 +0000 | [diff] [blame] | 152 | n_fail--; |
| 153 | } |
| 154 | op++; |
| 155 | } |
| 156 | } |
| 157 | |
| 158 | always_inline void |
| 159 | esp_remove_tail (vlib_main_t * vm, vlib_buffer_t * b, vlib_buffer_t * last, |
| 160 | u16 tail) |
| 161 | { |
| 162 | vlib_buffer_t *before_last = b; |
| 163 | |
Arthur de Kerhor | 8fce546 | 2023-06-16 09:48:52 +0200 | [diff] [blame] | 164 | if (b != last) |
| 165 | b->total_length_not_including_first_buffer -= tail; |
| 166 | |
Filip Tehlar | efcad1a | 2020-02-04 09:36:04 +0000 | [diff] [blame] | 167 | if (last->current_length > tail) |
| 168 | { |
| 169 | last->current_length -= tail; |
| 170 | return; |
| 171 | } |
| 172 | ASSERT (b->flags & VLIB_BUFFER_NEXT_PRESENT); |
| 173 | |
| 174 | while (b->flags & VLIB_BUFFER_NEXT_PRESENT) |
| 175 | { |
| 176 | before_last = b; |
| 177 | b = vlib_get_buffer (vm, b->next_buffer); |
| 178 | } |
| 179 | before_last->current_length -= tail - last->current_length; |
| 180 | vlib_buffer_free_one (vm, before_last->next_buffer); |
| 181 | before_last->flags &= ~VLIB_BUFFER_NEXT_PRESENT; |
| 182 | } |
| 183 | |
Arthur de Kerhor | 8fce546 | 2023-06-16 09:48:52 +0200 | [diff] [blame] | 184 | always_inline void |
| 185 | esp_remove_tail_and_tfc_padding (vlib_main_t *vm, vlib_node_runtime_t *node, |
| 186 | const esp_decrypt_packet_data_t *pd, |
| 187 | vlib_buffer_t *b, vlib_buffer_t *last, |
| 188 | u16 *next, u16 tail, int is_ip6) |
| 189 | { |
| 190 | const u16 total_buffer_length = vlib_buffer_length_in_chain (vm, b); |
| 191 | u16 ip_packet_length; |
| 192 | if (is_ip6) |
| 193 | { |
| 194 | const ip6_header_t *ip6 = vlib_buffer_get_current (b); |
| 195 | ip_packet_length = |
| 196 | clib_net_to_host_u16 (ip6->payload_length) + sizeof (ip6_header_t); |
| 197 | } |
| 198 | else |
| 199 | { |
| 200 | const ip4_header_t *ip4 = vlib_buffer_get_current (b); |
| 201 | ip_packet_length = clib_net_to_host_u16 (ip4->length); |
| 202 | } |
| 203 | /* In case of TFC padding, the size of the buffer data needs to be adjusted |
| 204 | * to the ip packet length */ |
| 205 | if (PREDICT_FALSE (total_buffer_length < ip_packet_length + tail)) |
| 206 | { |
| 207 | esp_decrypt_set_next_index (b, node, vm->thread_index, |
| 208 | ESP_DECRYPT_ERROR_NO_TAIL_SPACE, 0, next, |
| 209 | ESP_DECRYPT_NEXT_DROP, pd->sa_index); |
| 210 | return; |
| 211 | } |
| 212 | esp_remove_tail (vm, b, last, total_buffer_length - ip_packet_length); |
| 213 | } |
| 214 | |
Filip Tehlar | efcad1a | 2020-02-04 09:36:04 +0000 | [diff] [blame] | 215 | /* ICV is splitted in last two buffers so move it to the last buffer and |
| 216 | return pointer to it */ |
| 217 | static_always_inline u8 * |
| 218 | esp_move_icv (vlib_main_t * vm, vlib_buffer_t * first, |
PiotrX Kleski | a9585fd | 2020-12-11 15:10:31 +0000 | [diff] [blame] | 219 | esp_decrypt_packet_data_t * pd, |
Fan Zhang | f539578 | 2020-04-29 14:00:03 +0100 | [diff] [blame] | 220 | esp_decrypt_packet_data2_t * pd2, u16 icv_sz, u16 * dif) |
Filip Tehlar | efcad1a | 2020-02-04 09:36:04 +0000 | [diff] [blame] | 221 | { |
| 222 | vlib_buffer_t *before_last, *bp; |
Fan Zhang | f539578 | 2020-04-29 14:00:03 +0100 | [diff] [blame] | 223 | u16 last_sz = pd2->lb->current_length; |
Filip Tehlar | efcad1a | 2020-02-04 09:36:04 +0000 | [diff] [blame] | 224 | u16 first_sz = icv_sz - last_sz; |
| 225 | |
| 226 | bp = before_last = first; |
| 227 | while (bp->flags & VLIB_BUFFER_NEXT_PRESENT) |
| 228 | { |
| 229 | before_last = bp; |
| 230 | bp = vlib_get_buffer (vm, bp->next_buffer); |
| 231 | } |
| 232 | |
Fan Zhang | f539578 | 2020-04-29 14:00:03 +0100 | [diff] [blame] | 233 | u8 *lb_curr = vlib_buffer_get_current (pd2->lb); |
Filip Tehlar | efcad1a | 2020-02-04 09:36:04 +0000 | [diff] [blame] | 234 | memmove (lb_curr + first_sz, lb_curr, last_sz); |
| 235 | clib_memcpy_fast (lb_curr, vlib_buffer_get_tail (before_last) - first_sz, |
| 236 | first_sz); |
| 237 | before_last->current_length -= first_sz; |
PiotrX Kleski | a9585fd | 2020-12-11 15:10:31 +0000 | [diff] [blame] | 238 | if (before_last == first) |
| 239 | pd->current_length -= first_sz; |
Arthur de Kerhor | 8fce546 | 2023-06-16 09:48:52 +0200 | [diff] [blame] | 240 | else |
| 241 | first->total_length_not_including_first_buffer -= first_sz; |
Fan Zhang | f539578 | 2020-04-29 14:00:03 +0100 | [diff] [blame] | 242 | clib_memset (vlib_buffer_get_tail (before_last), 0, first_sz); |
Filip Tehlar | e4e8c6b | 2020-02-13 07:49:30 +0000 | [diff] [blame] | 243 | if (dif) |
| 244 | dif[0] = first_sz; |
Arthur de Kerhor | 8fce546 | 2023-06-16 09:48:52 +0200 | [diff] [blame] | 245 | first->total_length_not_including_first_buffer -= last_sz; |
Fan Zhang | f539578 | 2020-04-29 14:00:03 +0100 | [diff] [blame] | 246 | pd2->lb = before_last; |
| 247 | pd2->icv_removed = 1; |
| 248 | pd2->free_buffer_index = before_last->next_buffer; |
Filip Tehlar | efcad1a | 2020-02-04 09:36:04 +0000 | [diff] [blame] | 249 | before_last->flags &= ~VLIB_BUFFER_NEXT_PRESENT; |
| 250 | return lb_curr; |
| 251 | } |
| 252 | |
Neale Ranns | 5b89110 | 2021-06-28 13:31:28 +0000 | [diff] [blame] | 253 | static_always_inline u16 |
| 254 | esp_insert_esn (vlib_main_t *vm, ipsec_sa_t *sa, esp_decrypt_packet_data_t *pd, |
| 255 | esp_decrypt_packet_data2_t *pd2, u32 *data_len, u8 **digest, |
| 256 | u16 *len, vlib_buffer_t *b, u8 *payload) |
Filip Tehlar | e4e8c6b | 2020-02-13 07:49:30 +0000 | [diff] [blame] | 257 | { |
| 258 | if (!ipsec_sa_is_set_USE_ESN (sa)) |
Fan Zhang | f539578 | 2020-04-29 14:00:03 +0100 | [diff] [blame] | 259 | return 0; |
Filip Tehlar | e4e8c6b | 2020-02-13 07:49:30 +0000 | [diff] [blame] | 260 | /* shift ICV by 4 bytes to insert ESN */ |
Neale Ranns | 5b89110 | 2021-06-28 13:31:28 +0000 | [diff] [blame] | 261 | u32 seq_hi = clib_host_to_net_u32 (pd->seq_hi); |
| 262 | u8 tmp[ESP_MAX_ICV_SIZE]; |
Filip Tehlar | e4e8c6b | 2020-02-13 07:49:30 +0000 | [diff] [blame] | 263 | |
Fan Zhang | f539578 | 2020-04-29 14:00:03 +0100 | [diff] [blame] | 264 | if (pd2->icv_removed) |
Filip Tehlar | e4e8c6b | 2020-02-13 07:49:30 +0000 | [diff] [blame] | 265 | { |
Fan Zhang | f539578 | 2020-04-29 14:00:03 +0100 | [diff] [blame] | 266 | u16 space_left = vlib_buffer_space_left_at_end (vm, pd2->lb); |
Neale Ranns | 5b89110 | 2021-06-28 13:31:28 +0000 | [diff] [blame] | 267 | if (space_left >= N_HI_ESN_BYTES) |
Filip Tehlar | e4e8c6b | 2020-02-13 07:49:30 +0000 | [diff] [blame] | 268 | { |
Neale Ranns | 5b89110 | 2021-06-28 13:31:28 +0000 | [diff] [blame] | 269 | clib_memcpy_fast (vlib_buffer_get_tail (pd2->lb), &seq_hi, |
| 270 | N_HI_ESN_BYTES); |
| 271 | *data_len += N_HI_ESN_BYTES; |
Filip Tehlar | e4e8c6b | 2020-02-13 07:49:30 +0000 | [diff] [blame] | 272 | } |
| 273 | else |
Neale Ranns | 5b89110 | 2021-06-28 13:31:28 +0000 | [diff] [blame] | 274 | return N_HI_ESN_BYTES; |
Filip Tehlar | e4e8c6b | 2020-02-13 07:49:30 +0000 | [diff] [blame] | 275 | |
| 276 | len[0] = b->current_length; |
| 277 | } |
| 278 | else |
| 279 | { |
| 280 | clib_memcpy_fast (tmp, payload + len[0], ESP_MAX_ICV_SIZE); |
Neale Ranns | 5b89110 | 2021-06-28 13:31:28 +0000 | [diff] [blame] | 281 | clib_memcpy_fast (payload + len[0], &seq_hi, N_HI_ESN_BYTES); |
| 282 | clib_memcpy_fast (payload + len[0] + N_HI_ESN_BYTES, tmp, |
| 283 | ESP_MAX_ICV_SIZE); |
| 284 | *data_len += N_HI_ESN_BYTES; |
| 285 | *digest += N_HI_ESN_BYTES; |
Filip Tehlar | e4e8c6b | 2020-02-13 07:49:30 +0000 | [diff] [blame] | 286 | } |
Neale Ranns | 5b89110 | 2021-06-28 13:31:28 +0000 | [diff] [blame] | 287 | return N_HI_ESN_BYTES; |
Filip Tehlar | e4e8c6b | 2020-02-13 07:49:30 +0000 | [diff] [blame] | 288 | } |
| 289 | |
| 290 | static_always_inline u8 * |
| 291 | esp_move_icv_esn (vlib_main_t * vm, vlib_buffer_t * first, |
PiotrX Kleski | a9585fd | 2020-12-11 15:10:31 +0000 | [diff] [blame] | 292 | esp_decrypt_packet_data_t * pd, |
Fan Zhang | f539578 | 2020-04-29 14:00:03 +0100 | [diff] [blame] | 293 | esp_decrypt_packet_data2_t * pd2, u16 icv_sz, |
| 294 | ipsec_sa_t * sa, u8 * extra_esn, u32 * len) |
Filip Tehlar | e4e8c6b | 2020-02-13 07:49:30 +0000 | [diff] [blame] | 295 | { |
| 296 | u16 dif = 0; |
PiotrX Kleski | a9585fd | 2020-12-11 15:10:31 +0000 | [diff] [blame] | 297 | u8 *digest = esp_move_icv (vm, first, pd, pd2, icv_sz, &dif); |
Filip Tehlar | e4e8c6b | 2020-02-13 07:49:30 +0000 | [diff] [blame] | 298 | if (dif) |
Fan Zhang | f539578 | 2020-04-29 14:00:03 +0100 | [diff] [blame] | 299 | *len -= dif; |
Filip Tehlar | e4e8c6b | 2020-02-13 07:49:30 +0000 | [diff] [blame] | 300 | |
| 301 | if (ipsec_sa_is_set_USE_ESN (sa)) |
| 302 | { |
Neale Ranns | 5b89110 | 2021-06-28 13:31:28 +0000 | [diff] [blame] | 303 | u32 seq_hi = clib_host_to_net_u32 (pd->seq_hi); |
Fan Zhang | f539578 | 2020-04-29 14:00:03 +0100 | [diff] [blame] | 304 | u16 space_left = vlib_buffer_space_left_at_end (vm, pd2->lb); |
Filip Tehlar | e4e8c6b | 2020-02-13 07:49:30 +0000 | [diff] [blame] | 305 | |
Neale Ranns | 5b89110 | 2021-06-28 13:31:28 +0000 | [diff] [blame] | 306 | if (space_left >= N_HI_ESN_BYTES) |
Filip Tehlar | e4e8c6b | 2020-02-13 07:49:30 +0000 | [diff] [blame] | 307 | { |
Neale Ranns | 5b89110 | 2021-06-28 13:31:28 +0000 | [diff] [blame] | 308 | clib_memcpy_fast (vlib_buffer_get_tail (pd2->lb), &seq_hi, |
| 309 | N_HI_ESN_BYTES); |
| 310 | *len += N_HI_ESN_BYTES; |
Filip Tehlar | e4e8c6b | 2020-02-13 07:49:30 +0000 | [diff] [blame] | 311 | } |
| 312 | else |
| 313 | { |
| 314 | /* no space for ESN at the tail, use the next buffer |
| 315 | * (with ICV data) */ |
Fan Zhang | f539578 | 2020-04-29 14:00:03 +0100 | [diff] [blame] | 316 | ASSERT (pd2->icv_removed); |
| 317 | vlib_buffer_t *tmp = vlib_get_buffer (vm, pd2->free_buffer_index); |
Neale Ranns | 5b89110 | 2021-06-28 13:31:28 +0000 | [diff] [blame] | 318 | clib_memcpy_fast (vlib_buffer_get_current (tmp) - N_HI_ESN_BYTES, |
| 319 | &seq_hi, N_HI_ESN_BYTES); |
Filip Tehlar | e4e8c6b | 2020-02-13 07:49:30 +0000 | [diff] [blame] | 320 | extra_esn[0] = 1; |
| 321 | } |
| 322 | } |
| 323 | return digest; |
| 324 | } |
| 325 | |
Fan Zhang | f539578 | 2020-04-29 14:00:03 +0100 | [diff] [blame] | 326 | static_always_inline int |
Neale Ranns | 5b89110 | 2021-06-28 13:31:28 +0000 | [diff] [blame] | 327 | esp_decrypt_chain_integ (vlib_main_t *vm, ipsec_per_thread_data_t *ptd, |
| 328 | const esp_decrypt_packet_data_t *pd, |
| 329 | esp_decrypt_packet_data2_t *pd2, ipsec_sa_t *sa0, |
| 330 | vlib_buffer_t *b, u8 icv_sz, u8 *start_src, |
| 331 | u32 start_len, u8 **digest, u16 *n_ch, |
| 332 | u32 *integ_total_len) |
Fan Zhang | f539578 | 2020-04-29 14:00:03 +0100 | [diff] [blame] | 333 | { |
| 334 | vnet_crypto_op_chunk_t *ch; |
| 335 | vlib_buffer_t *cb = vlib_get_buffer (vm, b->next_buffer); |
| 336 | u16 n_chunks = 1; |
| 337 | u32 total_len; |
| 338 | vec_add2 (ptd->chunks, ch, 1); |
| 339 | total_len = ch->len = start_len; |
| 340 | ch->src = start_src; |
| 341 | |
| 342 | while (1) |
| 343 | { |
| 344 | vec_add2 (ptd->chunks, ch, 1); |
| 345 | n_chunks += 1; |
| 346 | ch->src = vlib_buffer_get_current (cb); |
| 347 | if (pd2->lb == cb) |
| 348 | { |
| 349 | if (pd2->icv_removed) |
| 350 | ch->len = cb->current_length; |
| 351 | else |
| 352 | ch->len = cb->current_length - icv_sz; |
| 353 | if (ipsec_sa_is_set_USE_ESN (sa0)) |
| 354 | { |
Neale Ranns | 5b89110 | 2021-06-28 13:31:28 +0000 | [diff] [blame] | 355 | u32 seq_hi = clib_host_to_net_u32 (pd->seq_hi); |
| 356 | u8 tmp[ESP_MAX_ICV_SIZE]; |
Fan Zhang | f539578 | 2020-04-29 14:00:03 +0100 | [diff] [blame] | 357 | u8 *esn; |
| 358 | vlib_buffer_t *tmp_b; |
| 359 | u16 space_left = vlib_buffer_space_left_at_end (vm, pd2->lb); |
Neale Ranns | 5b89110 | 2021-06-28 13:31:28 +0000 | [diff] [blame] | 360 | if (space_left < N_HI_ESN_BYTES) |
Fan Zhang | f539578 | 2020-04-29 14:00:03 +0100 | [diff] [blame] | 361 | { |
| 362 | if (pd2->icv_removed) |
| 363 | { |
| 364 | /* use pre-data area from the last bufer |
| 365 | that was removed from the chain */ |
| 366 | tmp_b = vlib_get_buffer (vm, pd2->free_buffer_index); |
Neale Ranns | 5b89110 | 2021-06-28 13:31:28 +0000 | [diff] [blame] | 367 | esn = tmp_b->data - N_HI_ESN_BYTES; |
Fan Zhang | f539578 | 2020-04-29 14:00:03 +0100 | [diff] [blame] | 368 | } |
| 369 | else |
| 370 | { |
| 371 | /* no space, need to allocate new buffer */ |
| 372 | u32 tmp_bi = 0; |
| 373 | if (vlib_buffer_alloc (vm, &tmp_bi, 1) != 1) |
| 374 | return -1; |
| 375 | tmp_b = vlib_get_buffer (vm, tmp_bi); |
| 376 | esn = tmp_b->data; |
| 377 | pd2->free_buffer_index = tmp_bi; |
| 378 | } |
Neale Ranns | 5b89110 | 2021-06-28 13:31:28 +0000 | [diff] [blame] | 379 | clib_memcpy_fast (esn, &seq_hi, N_HI_ESN_BYTES); |
Fan Zhang | f539578 | 2020-04-29 14:00:03 +0100 | [diff] [blame] | 380 | |
| 381 | vec_add2 (ptd->chunks, ch, 1); |
| 382 | n_chunks += 1; |
| 383 | ch->src = esn; |
Neale Ranns | 5b89110 | 2021-06-28 13:31:28 +0000 | [diff] [blame] | 384 | ch->len = N_HI_ESN_BYTES; |
Fan Zhang | f539578 | 2020-04-29 14:00:03 +0100 | [diff] [blame] | 385 | } |
| 386 | else |
| 387 | { |
| 388 | if (pd2->icv_removed) |
| 389 | { |
Neale Ranns | 5b89110 | 2021-06-28 13:31:28 +0000 | [diff] [blame] | 390 | clib_memcpy_fast (vlib_buffer_get_tail (pd2->lb), |
| 391 | &seq_hi, N_HI_ESN_BYTES); |
Fan Zhang | f539578 | 2020-04-29 14:00:03 +0100 | [diff] [blame] | 392 | } |
| 393 | else |
| 394 | { |
| 395 | clib_memcpy_fast (tmp, *digest, ESP_MAX_ICV_SIZE); |
Neale Ranns | 5b89110 | 2021-06-28 13:31:28 +0000 | [diff] [blame] | 396 | clib_memcpy_fast (*digest, &seq_hi, N_HI_ESN_BYTES); |
| 397 | clib_memcpy_fast (*digest + N_HI_ESN_BYTES, tmp, |
| 398 | ESP_MAX_ICV_SIZE); |
| 399 | *digest += N_HI_ESN_BYTES; |
Fan Zhang | f539578 | 2020-04-29 14:00:03 +0100 | [diff] [blame] | 400 | } |
Neale Ranns | 5b89110 | 2021-06-28 13:31:28 +0000 | [diff] [blame] | 401 | ch->len += N_HI_ESN_BYTES; |
Fan Zhang | f539578 | 2020-04-29 14:00:03 +0100 | [diff] [blame] | 402 | } |
| 403 | } |
| 404 | total_len += ch->len; |
| 405 | break; |
| 406 | } |
| 407 | else |
| 408 | total_len += ch->len = cb->current_length; |
| 409 | |
| 410 | if (!(cb->flags & VLIB_BUFFER_NEXT_PRESENT)) |
| 411 | break; |
| 412 | |
| 413 | cb = vlib_get_buffer (vm, cb->next_buffer); |
| 414 | } |
| 415 | |
| 416 | if (n_ch) |
| 417 | *n_ch = n_chunks; |
| 418 | if (integ_total_len) |
| 419 | *integ_total_len = total_len; |
| 420 | |
| 421 | return 0; |
| 422 | } |
| 423 | |
| 424 | static_always_inline u32 |
| 425 | esp_decrypt_chain_crypto (vlib_main_t * vm, ipsec_per_thread_data_t * ptd, |
PiotrX Kleski | a9585fd | 2020-12-11 15:10:31 +0000 | [diff] [blame] | 426 | esp_decrypt_packet_data_t * pd, |
Fan Zhang | f539578 | 2020-04-29 14:00:03 +0100 | [diff] [blame] | 427 | esp_decrypt_packet_data2_t * pd2, |
| 428 | ipsec_sa_t * sa0, vlib_buffer_t * b, u8 icv_sz, |
| 429 | u8 * start, u32 start_len, u8 ** tag, u16 * n_ch) |
| 430 | { |
| 431 | vnet_crypto_op_chunk_t *ch; |
| 432 | vlib_buffer_t *cb = b; |
| 433 | u16 n_chunks = 1; |
| 434 | u32 total_len; |
| 435 | vec_add2 (ptd->chunks, ch, 1); |
| 436 | total_len = ch->len = start_len; |
| 437 | ch->src = ch->dst = start; |
| 438 | cb = vlib_get_buffer (vm, cb->next_buffer); |
| 439 | n_chunks = 1; |
| 440 | |
| 441 | while (1) |
| 442 | { |
| 443 | vec_add2 (ptd->chunks, ch, 1); |
| 444 | n_chunks += 1; |
| 445 | ch->src = ch->dst = vlib_buffer_get_current (cb); |
| 446 | if (pd2->lb == cb) |
| 447 | { |
| 448 | if (ipsec_sa_is_set_IS_AEAD (sa0)) |
| 449 | { |
| 450 | if (pd2->lb->current_length < icv_sz) |
| 451 | { |
| 452 | u16 dif = 0; |
PiotrX Kleski | a9585fd | 2020-12-11 15:10:31 +0000 | [diff] [blame] | 453 | *tag = esp_move_icv (vm, b, pd, pd2, icv_sz, &dif); |
Fan Zhang | f539578 | 2020-04-29 14:00:03 +0100 | [diff] [blame] | 454 | |
| 455 | /* this chunk does not contain crypto data */ |
| 456 | n_chunks -= 1; |
| 457 | /* and fix previous chunk's length as it might have |
| 458 | been changed */ |
| 459 | ASSERT (n_chunks > 0); |
| 460 | if (pd2->lb == b) |
| 461 | { |
| 462 | total_len -= dif; |
| 463 | ch[-1].len -= dif; |
| 464 | } |
| 465 | else |
| 466 | { |
| 467 | total_len = total_len + pd2->lb->current_length - |
| 468 | ch[-1].len; |
| 469 | ch[-1].len = pd2->lb->current_length; |
| 470 | } |
| 471 | break; |
| 472 | } |
| 473 | else |
| 474 | *tag = vlib_buffer_get_tail (pd2->lb) - icv_sz; |
| 475 | } |
| 476 | |
| 477 | if (pd2->icv_removed) |
| 478 | total_len += ch->len = cb->current_length; |
| 479 | else |
| 480 | total_len += ch->len = cb->current_length - icv_sz; |
| 481 | } |
| 482 | else |
| 483 | total_len += ch->len = cb->current_length; |
| 484 | |
| 485 | if (!(cb->flags & VLIB_BUFFER_NEXT_PRESENT)) |
| 486 | break; |
| 487 | |
| 488 | cb = vlib_get_buffer (vm, cb->next_buffer); |
| 489 | } |
| 490 | |
| 491 | if (n_ch) |
| 492 | *n_ch = n_chunks; |
| 493 | |
| 494 | return total_len; |
| 495 | } |
| 496 | |
| 497 | static_always_inline void |
| 498 | esp_decrypt_prepare_sync_op (vlib_main_t * vm, vlib_node_runtime_t * node, |
| 499 | ipsec_per_thread_data_t * ptd, |
| 500 | vnet_crypto_op_t *** crypto_ops, |
| 501 | vnet_crypto_op_t *** integ_ops, |
| 502 | vnet_crypto_op_t * op, |
| 503 | ipsec_sa_t * sa0, u8 * payload, |
| 504 | u16 len, u8 icv_sz, u8 iv_sz, |
| 505 | esp_decrypt_packet_data_t * pd, |
| 506 | esp_decrypt_packet_data2_t * pd2, |
| 507 | vlib_buffer_t * b, u16 * next, u32 index) |
| 508 | { |
| 509 | const u8 esp_sz = sizeof (esp_header_t); |
| 510 | |
| 511 | if (PREDICT_TRUE (sa0->integ_op_id != VNET_CRYPTO_OP_NONE)) |
| 512 | { |
| 513 | vnet_crypto_op_init (op, sa0->integ_op_id); |
| 514 | op->key_index = sa0->integ_key_index; |
| 515 | op->src = payload; |
| 516 | op->flags = VNET_CRYPTO_OP_FLAG_HMAC_CHECK; |
| 517 | op->user_data = index; |
| 518 | op->digest = payload + len; |
| 519 | op->digest_len = icv_sz; |
| 520 | op->len = len; |
| 521 | |
| 522 | if (pd->is_chain) |
| 523 | { |
| 524 | /* buffer is chained */ |
| 525 | op->len = pd->current_length; |
| 526 | |
| 527 | /* special case when ICV is splitted and needs to be reassembled |
| 528 | * first -> move it to the last buffer. Also take into account |
| 529 | * that ESN needs to be added after encrypted data and may or |
| 530 | * may not fit in the tail.*/ |
| 531 | if (pd2->lb->current_length < icv_sz) |
| 532 | { |
| 533 | u8 extra_esn = 0; |
| 534 | op->digest = |
PiotrX Kleski | a9585fd | 2020-12-11 15:10:31 +0000 | [diff] [blame] | 535 | esp_move_icv_esn (vm, b, pd, pd2, icv_sz, sa0, |
Fan Zhang | f539578 | 2020-04-29 14:00:03 +0100 | [diff] [blame] | 536 | &extra_esn, &op->len); |
| 537 | |
| 538 | if (extra_esn) |
| 539 | { |
| 540 | /* esn is in the last buffer, that was unlinked from |
| 541 | * the chain */ |
| 542 | op->len = b->current_length; |
| 543 | } |
| 544 | else |
| 545 | { |
| 546 | if (pd2->lb == b) |
| 547 | { |
| 548 | /* we now have a single buffer of crypto data, adjust |
| 549 | * the length (second buffer contains only ICV) */ |
| 550 | *integ_ops = &ptd->integ_ops; |
| 551 | *crypto_ops = &ptd->crypto_ops; |
| 552 | len = b->current_length; |
| 553 | goto out; |
| 554 | } |
| 555 | } |
| 556 | } |
| 557 | else |
| 558 | op->digest = vlib_buffer_get_tail (pd2->lb) - icv_sz; |
| 559 | |
| 560 | op->flags |= VNET_CRYPTO_OP_FLAG_CHAINED_BUFFERS; |
| 561 | op->chunk_index = vec_len (ptd->chunks); |
Neale Ranns | 5b89110 | 2021-06-28 13:31:28 +0000 | [diff] [blame] | 562 | if (esp_decrypt_chain_integ (vm, ptd, pd, pd2, sa0, b, icv_sz, |
Fan Zhang | f539578 | 2020-04-29 14:00:03 +0100 | [diff] [blame] | 563 | payload, pd->current_length, |
| 564 | &op->digest, &op->n_chunks, 0) < 0) |
| 565 | { |
Arthur de Kerhor | ad95b06 | 2022-11-16 19:12:05 +0100 | [diff] [blame] | 566 | esp_decrypt_set_next_index ( |
| 567 | b, node, vm->thread_index, ESP_DECRYPT_ERROR_NO_BUFFERS, 0, |
| 568 | next, ESP_DECRYPT_NEXT_DROP, pd->sa_index); |
Fan Zhang | f539578 | 2020-04-29 14:00:03 +0100 | [diff] [blame] | 569 | return; |
| 570 | } |
| 571 | } |
| 572 | else |
Neale Ranns | 5b89110 | 2021-06-28 13:31:28 +0000 | [diff] [blame] | 573 | esp_insert_esn (vm, sa0, pd, pd2, &op->len, &op->digest, &len, b, |
Fan Zhang | f539578 | 2020-04-29 14:00:03 +0100 | [diff] [blame] | 574 | payload); |
| 575 | out: |
| 576 | vec_add_aligned (*(integ_ops[0]), op, 1, CLIB_CACHE_LINE_BYTES); |
| 577 | } |
| 578 | |
| 579 | payload += esp_sz; |
| 580 | len -= esp_sz; |
| 581 | |
| 582 | if (sa0->crypto_dec_op_id != VNET_CRYPTO_OP_NONE) |
| 583 | { |
| 584 | vnet_crypto_op_init (op, sa0->crypto_dec_op_id); |
| 585 | op->key_index = sa0->crypto_key_index; |
| 586 | op->iv = payload; |
| 587 | |
Benoît Ganne | 490b927 | 2021-01-22 18:03:09 +0100 | [diff] [blame] | 588 | if (ipsec_sa_is_set_IS_CTR (sa0)) |
Fan Zhang | f539578 | 2020-04-29 14:00:03 +0100 | [diff] [blame] | 589 | { |
Benoît Ganne | 490b927 | 2021-01-22 18:03:09 +0100 | [diff] [blame] | 590 | /* construct nonce in a scratch space in front of the IP header */ |
| 591 | esp_ctr_nonce_t *nonce = |
| 592 | (esp_ctr_nonce_t *) (payload - esp_sz - pd->hdr_sz - |
| 593 | sizeof (*nonce)); |
| 594 | if (ipsec_sa_is_set_IS_AEAD (sa0)) |
| 595 | { |
| 596 | /* constuct aad in a scratch space in front of the nonce */ |
| 597 | esp_header_t *esp0 = (esp_header_t *) (payload - esp_sz); |
| 598 | op->aad = (u8 *) nonce - sizeof (esp_aead_t); |
Neale Ranns | 5b89110 | 2021-06-28 13:31:28 +0000 | [diff] [blame] | 599 | op->aad_len = esp_aad_fill (op->aad, esp0, sa0, pd->seq_hi); |
Benoît Ganne | 490b927 | 2021-01-22 18:03:09 +0100 | [diff] [blame] | 600 | op->tag = payload + len; |
| 601 | op->tag_len = 16; |
Benoît Ganne | 84e6658 | 2023-03-10 17:33:03 +0100 | [diff] [blame] | 602 | if (PREDICT_FALSE (ipsec_sa_is_set_IS_NULL_GMAC (sa0))) |
| 603 | { |
| 604 | /* RFC-4543 ENCR_NULL_AUTH_AES_GMAC: IV is part of AAD */ |
| 605 | payload -= iv_sz; |
| 606 | len += iv_sz; |
| 607 | } |
Benoît Ganne | 490b927 | 2021-01-22 18:03:09 +0100 | [diff] [blame] | 608 | } |
| 609 | else |
| 610 | { |
| 611 | nonce->ctr = clib_host_to_net_u32 (1); |
| 612 | } |
| 613 | nonce->salt = sa0->salt; |
| 614 | ASSERT (sizeof (u64) == iv_sz); |
| 615 | nonce->iv = *(u64 *) op->iv; |
| 616 | op->iv = (u8 *) nonce; |
Fan Zhang | f539578 | 2020-04-29 14:00:03 +0100 | [diff] [blame] | 617 | } |
| 618 | op->src = op->dst = payload += iv_sz; |
| 619 | op->len = len - iv_sz; |
| 620 | op->user_data = index; |
| 621 | |
| 622 | if (pd->is_chain && (pd2->lb != b)) |
| 623 | { |
| 624 | /* buffer is chained */ |
| 625 | op->flags |= VNET_CRYPTO_OP_FLAG_CHAINED_BUFFERS; |
| 626 | op->chunk_index = vec_len (ptd->chunks); |
PiotrX Kleski | a9585fd | 2020-12-11 15:10:31 +0000 | [diff] [blame] | 627 | esp_decrypt_chain_crypto (vm, ptd, pd, pd2, sa0, b, icv_sz, |
Fan Zhang | f539578 | 2020-04-29 14:00:03 +0100 | [diff] [blame] | 628 | payload, len - pd->iv_sz + pd->icv_sz, |
| 629 | &op->tag, &op->n_chunks); |
| 630 | } |
| 631 | |
| 632 | vec_add_aligned (*(crypto_ops[0]), op, 1, CLIB_CACHE_LINE_BYTES); |
| 633 | } |
| 634 | } |
| 635 | |
Neale Ranns | fc81134 | 2021-02-26 10:35:33 +0000 | [diff] [blame] | 636 | static_always_inline esp_decrypt_error_t |
| 637 | esp_decrypt_prepare_async_frame (vlib_main_t *vm, vlib_node_runtime_t *node, |
| 638 | ipsec_per_thread_data_t *ptd, |
| 639 | vnet_crypto_async_frame_t *f, ipsec_sa_t *sa0, |
| 640 | u8 *payload, u16 len, u8 icv_sz, u8 iv_sz, |
| 641 | esp_decrypt_packet_data_t *pd, |
| 642 | esp_decrypt_packet_data2_t *pd2, u32 bi, |
| 643 | vlib_buffer_t *b, u16 *next, u16 async_next) |
Fan Zhang | f539578 | 2020-04-29 14:00:03 +0100 | [diff] [blame] | 644 | { |
| 645 | const u8 esp_sz = sizeof (esp_header_t); |
Fan Zhang | f539578 | 2020-04-29 14:00:03 +0100 | [diff] [blame] | 646 | esp_decrypt_packet_data_t *async_pd = &(esp_post_data (b))->decrypt_data; |
| 647 | esp_decrypt_packet_data2_t *async_pd2 = esp_post_data2 (b); |
| 648 | u8 *tag = payload + len, *iv = payload + esp_sz, *aad = 0; |
Benoît Ganne | 5527a78 | 2022-01-18 15:56:41 +0100 | [diff] [blame] | 649 | const u32 key_index = sa0->crypto_key_index; |
Fan Zhang | f539578 | 2020-04-29 14:00:03 +0100 | [diff] [blame] | 650 | u32 crypto_len, integ_len = 0; |
| 651 | i16 crypto_start_offset, integ_start_offset = 0; |
| 652 | u8 flags = 0; |
| 653 | |
| 654 | if (!ipsec_sa_is_set_IS_AEAD (sa0)) |
| 655 | { |
| 656 | /* linked algs */ |
Fan Zhang | f539578 | 2020-04-29 14:00:03 +0100 | [diff] [blame] | 657 | integ_start_offset = payload - b->data; |
| 658 | integ_len = len; |
Benoît Ganne | 48524a9 | 2021-01-22 18:11:37 +0100 | [diff] [blame] | 659 | if (PREDICT_TRUE (sa0->integ_op_id != VNET_CRYPTO_OP_NONE)) |
| 660 | flags |= VNET_CRYPTO_OP_FLAG_HMAC_CHECK; |
Fan Zhang | f539578 | 2020-04-29 14:00:03 +0100 | [diff] [blame] | 661 | |
| 662 | if (pd->is_chain) |
| 663 | { |
| 664 | /* buffer is chained */ |
| 665 | integ_len = pd->current_length; |
| 666 | |
| 667 | /* special case when ICV is splitted and needs to be reassembled |
| 668 | * first -> move it to the last buffer. Also take into account |
| 669 | * that ESN needs to be added after encrypted data and may or |
| 670 | * may not fit in the tail.*/ |
| 671 | if (pd2->lb->current_length < icv_sz) |
| 672 | { |
| 673 | u8 extra_esn = 0; |
PiotrX Kleski | a9585fd | 2020-12-11 15:10:31 +0000 | [diff] [blame] | 674 | tag = esp_move_icv_esn (vm, b, pd, pd2, icv_sz, sa0, |
Fan Zhang | f539578 | 2020-04-29 14:00:03 +0100 | [diff] [blame] | 675 | &extra_esn, &integ_len); |
| 676 | |
| 677 | if (extra_esn) |
| 678 | { |
| 679 | /* esn is in the last buffer, that was unlinked from |
| 680 | * the chain */ |
| 681 | integ_len = b->current_length; |
| 682 | } |
| 683 | else |
| 684 | { |
| 685 | if (pd2->lb == b) |
| 686 | { |
| 687 | /* we now have a single buffer of crypto data, adjust |
| 688 | * the length (second buffer contains only ICV) */ |
| 689 | len = b->current_length; |
| 690 | goto out; |
| 691 | } |
| 692 | } |
| 693 | } |
| 694 | else |
| 695 | tag = vlib_buffer_get_tail (pd2->lb) - icv_sz; |
| 696 | |
| 697 | flags |= VNET_CRYPTO_OP_FLAG_CHAINED_BUFFERS; |
Neale Ranns | 5b89110 | 2021-06-28 13:31:28 +0000 | [diff] [blame] | 698 | if (esp_decrypt_chain_integ (vm, ptd, pd, pd2, sa0, b, icv_sz, |
| 699 | payload, pd->current_length, &tag, 0, |
| 700 | &integ_len) < 0) |
Fan Zhang | f539578 | 2020-04-29 14:00:03 +0100 | [diff] [blame] | 701 | { |
| 702 | /* allocate buffer failed, will not add to frame and drop */ |
Neale Ranns | fc81134 | 2021-02-26 10:35:33 +0000 | [diff] [blame] | 703 | return (ESP_DECRYPT_ERROR_NO_BUFFERS); |
Fan Zhang | f539578 | 2020-04-29 14:00:03 +0100 | [diff] [blame] | 704 | } |
| 705 | } |
| 706 | else |
Neale Ranns | 5b89110 | 2021-06-28 13:31:28 +0000 | [diff] [blame] | 707 | esp_insert_esn (vm, sa0, pd, pd2, &integ_len, &tag, &len, b, payload); |
Fan Zhang | f539578 | 2020-04-29 14:00:03 +0100 | [diff] [blame] | 708 | } |
Fan Zhang | f539578 | 2020-04-29 14:00:03 +0100 | [diff] [blame] | 709 | |
| 710 | out: |
| 711 | /* crypto */ |
| 712 | payload += esp_sz; |
| 713 | len -= esp_sz; |
| 714 | iv = payload; |
| 715 | |
Benoît Ganne | 490b927 | 2021-01-22 18:03:09 +0100 | [diff] [blame] | 716 | if (ipsec_sa_is_set_IS_CTR (sa0)) |
Fan Zhang | f539578 | 2020-04-29 14:00:03 +0100 | [diff] [blame] | 717 | { |
Benoît Ganne | 490b927 | 2021-01-22 18:03:09 +0100 | [diff] [blame] | 718 | /* construct nonce in a scratch space in front of the IP header */ |
| 719 | esp_ctr_nonce_t *nonce = |
| 720 | (esp_ctr_nonce_t *) (payload - esp_sz - pd->hdr_sz - sizeof (*nonce)); |
| 721 | if (ipsec_sa_is_set_IS_AEAD (sa0)) |
| 722 | { |
| 723 | /* constuct aad in a scratch space in front of the nonce */ |
| 724 | esp_header_t *esp0 = (esp_header_t *) (payload - esp_sz); |
| 725 | aad = (u8 *) nonce - sizeof (esp_aead_t); |
Neale Ranns | 5b89110 | 2021-06-28 13:31:28 +0000 | [diff] [blame] | 726 | esp_aad_fill (aad, esp0, sa0, pd->seq_hi); |
Benoît Ganne | 490b927 | 2021-01-22 18:03:09 +0100 | [diff] [blame] | 727 | tag = payload + len; |
Benoît Ganne | 84e6658 | 2023-03-10 17:33:03 +0100 | [diff] [blame] | 728 | if (PREDICT_FALSE (ipsec_sa_is_set_IS_NULL_GMAC (sa0))) |
| 729 | { |
| 730 | /* RFC-4543 ENCR_NULL_AUTH_AES_GMAC: IV is part of AAD */ |
| 731 | payload -= iv_sz; |
| 732 | len += iv_sz; |
| 733 | } |
Benoît Ganne | 490b927 | 2021-01-22 18:03:09 +0100 | [diff] [blame] | 734 | } |
| 735 | else |
| 736 | { |
| 737 | nonce->ctr = clib_host_to_net_u32 (1); |
| 738 | } |
| 739 | nonce->salt = sa0->salt; |
| 740 | ASSERT (sizeof (u64) == iv_sz); |
| 741 | nonce->iv = *(u64 *) iv; |
| 742 | iv = (u8 *) nonce; |
Fan Zhang | f539578 | 2020-04-29 14:00:03 +0100 | [diff] [blame] | 743 | } |
| 744 | |
| 745 | crypto_start_offset = (payload += iv_sz) - b->data; |
| 746 | crypto_len = len - iv_sz; |
| 747 | |
| 748 | if (pd->is_chain && (pd2->lb != b)) |
| 749 | { |
| 750 | /* buffer is chained */ |
| 751 | flags |= VNET_CRYPTO_OP_FLAG_CHAINED_BUFFERS; |
| 752 | |
PiotrX Kleski | a9585fd | 2020-12-11 15:10:31 +0000 | [diff] [blame] | 753 | crypto_len = esp_decrypt_chain_crypto (vm, ptd, pd, pd2, sa0, b, icv_sz, |
Fan Zhang | f539578 | 2020-04-29 14:00:03 +0100 | [diff] [blame] | 754 | payload, |
| 755 | len - pd->iv_sz + pd->icv_sz, |
| 756 | &tag, 0); |
| 757 | } |
| 758 | |
| 759 | *async_pd = *pd; |
| 760 | *async_pd2 = *pd2; |
Fan Zhang | f539578 | 2020-04-29 14:00:03 +0100 | [diff] [blame] | 761 | |
| 762 | /* for AEAD integ_len - crypto_len will be negative, it is ok since it |
| 763 | * is ignored by the engine. */ |
Neale Ranns | fc81134 | 2021-02-26 10:35:33 +0000 | [diff] [blame] | 764 | vnet_crypto_async_add_to_frame ( |
| 765 | vm, f, key_index, crypto_len, integ_len - crypto_len, crypto_start_offset, |
| 766 | integ_start_offset, bi, async_next, iv, tag, aad, flags); |
| 767 | |
| 768 | return (ESP_DECRYPT_ERROR_RX_PKTS); |
Fan Zhang | f539578 | 2020-04-29 14:00:03 +0100 | [diff] [blame] | 769 | } |
| 770 | |
| 771 | static_always_inline void |
Arthur de Kerhor | ad95b06 | 2022-11-16 19:12:05 +0100 | [diff] [blame] | 772 | esp_decrypt_post_crypto (vlib_main_t *vm, vlib_node_runtime_t *node, |
Benoît Ganne | 98ca76a | 2022-04-11 18:51:25 +0200 | [diff] [blame] | 773 | const u16 *next_by_next_header, |
Neale Ranns | e11203e | 2021-09-21 12:34:19 +0000 | [diff] [blame] | 774 | const esp_decrypt_packet_data_t *pd, |
| 775 | const esp_decrypt_packet_data2_t *pd2, |
| 776 | vlib_buffer_t *b, u16 *next, int is_ip6, int is_tun, |
| 777 | int is_async) |
Fan Zhang | f539578 | 2020-04-29 14:00:03 +0100 | [diff] [blame] | 778 | { |
Neale Ranns | c5fe57d | 2021-02-25 16:01:28 +0000 | [diff] [blame] | 779 | ipsec_sa_t *sa0 = ipsec_sa_get (pd->sa_index); |
Fan Zhang | f539578 | 2020-04-29 14:00:03 +0100 | [diff] [blame] | 780 | vlib_buffer_t *lb = b; |
| 781 | const u8 esp_sz = sizeof (esp_header_t); |
| 782 | const u8 tun_flags = IPSEC_SA_FLAG_IS_TUNNEL | IPSEC_SA_FLAG_IS_TUNNEL_V6; |
| 783 | u8 pad_length = 0, next_header = 0; |
| 784 | u16 icv_sz; |
Maxime Peim | 0e2f188 | 2022-12-22 11:26:57 +0000 | [diff] [blame] | 785 | u64 n_lost; |
Fan Zhang | f539578 | 2020-04-29 14:00:03 +0100 | [diff] [blame] | 786 | |
| 787 | /* |
| 788 | * redo the anti-reply check |
| 789 | * in this frame say we have sequence numbers, s, s+1, s+1, s+1 |
| 790 | * and s and s+1 are in the window. When we did the anti-replay |
| 791 | * check above we did so against the state of the window (W), |
| 792 | * after packet s-1. So each of the packets in the sequence will be |
| 793 | * accepted. |
Maxime Peim | 0e2f188 | 2022-12-22 11:26:57 +0000 | [diff] [blame] | 794 | * This time s will be cheked against Ws-1, s+1 checked against Ws |
| 795 | * (i.e. the window state is updated/advanced) |
| 796 | * so this time the successive s+1 packet will be dropped. |
Fan Zhang | f539578 | 2020-04-29 14:00:03 +0100 | [diff] [blame] | 797 | * This is a consequence of batching the decrypts. If the |
Maxime Peim | 0e2f188 | 2022-12-22 11:26:57 +0000 | [diff] [blame] | 798 | * check-decrypt-advance process was done for each packet it would |
Fan Zhang | f539578 | 2020-04-29 14:00:03 +0100 | [diff] [blame] | 799 | * be fine. But we batch the decrypts because it's much more efficient |
| 800 | * to do so in SW and if we offload to HW and the process is async. |
| 801 | * |
| 802 | * You're probably thinking, but this means an attacker can send the |
Maxime Peim | 0e2f188 | 2022-12-22 11:26:57 +0000 | [diff] [blame] | 803 | * above sequence and cause VPP to perform decrypts that will fail, |
Fan Zhang | f539578 | 2020-04-29 14:00:03 +0100 | [diff] [blame] | 804 | * and that's true. But if the attacker can determine s (a valid |
| 805 | * sequence number in the window) which is non-trivial, it can generate |
| 806 | * a sequence s, s+1, s+2, s+3, ... s+n and nothing will prevent any |
| 807 | * implementation, sequential or batching, from decrypting these. |
| 808 | */ |
Maxime Peim | 0e2f188 | 2022-12-22 11:26:57 +0000 | [diff] [blame] | 809 | if (PREDICT_FALSE (ipsec_sa_is_set_ANTI_REPLAY_HUGE (sa0))) |
Fan Zhang | f539578 | 2020-04-29 14:00:03 +0100 | [diff] [blame] | 810 | { |
Maxime Peim | 0e2f188 | 2022-12-22 11:26:57 +0000 | [diff] [blame] | 811 | if (ipsec_sa_anti_replay_and_sn_advance (sa0, pd->seq, pd->seq_hi, true, |
| 812 | NULL, true)) |
| 813 | { |
| 814 | esp_decrypt_set_next_index (b, node, vm->thread_index, |
| 815 | ESP_DECRYPT_ERROR_REPLAY, 0, next, |
| 816 | ESP_DECRYPT_NEXT_DROP, pd->sa_index); |
| 817 | return; |
| 818 | } |
| 819 | n_lost = ipsec_sa_anti_replay_advance (sa0, vm->thread_index, pd->seq, |
| 820 | pd->seq_hi, true); |
Fan Zhang | f539578 | 2020-04-29 14:00:03 +0100 | [diff] [blame] | 821 | } |
Maxime Peim | 0e2f188 | 2022-12-22 11:26:57 +0000 | [diff] [blame] | 822 | else |
| 823 | { |
| 824 | if (ipsec_sa_anti_replay_and_sn_advance (sa0, pd->seq, pd->seq_hi, true, |
| 825 | NULL, false)) |
| 826 | { |
| 827 | esp_decrypt_set_next_index (b, node, vm->thread_index, |
| 828 | ESP_DECRYPT_ERROR_REPLAY, 0, next, |
| 829 | ESP_DECRYPT_NEXT_DROP, pd->sa_index); |
| 830 | return; |
| 831 | } |
| 832 | n_lost = ipsec_sa_anti_replay_advance (sa0, vm->thread_index, pd->seq, |
| 833 | pd->seq_hi, false); |
| 834 | } |
Neale Ranns | e11203e | 2021-09-21 12:34:19 +0000 | [diff] [blame] | 835 | |
Arthur de Kerhor | ad95b06 | 2022-11-16 19:12:05 +0100 | [diff] [blame] | 836 | vlib_prefetch_simple_counter (&ipsec_sa_err_counters[IPSEC_SA_ERROR_LOST], |
| 837 | vm->thread_index, pd->sa_index); |
Fan Zhang | f539578 | 2020-04-29 14:00:03 +0100 | [diff] [blame] | 838 | |
| 839 | if (pd->is_chain) |
| 840 | { |
| 841 | lb = pd2->lb; |
| 842 | icv_sz = pd2->icv_removed ? 0 : pd->icv_sz; |
| 843 | if (pd2->free_buffer_index) |
| 844 | { |
| 845 | vlib_buffer_free_one (vm, pd2->free_buffer_index); |
| 846 | lb->next_buffer = 0; |
| 847 | } |
| 848 | if (lb->current_length < sizeof (esp_footer_t) + icv_sz) |
| 849 | { |
| 850 | /* esp footer is either splitted in two buffers or in the before |
| 851 | * last buffer */ |
| 852 | |
| 853 | vlib_buffer_t *before_last = b, *bp = b; |
| 854 | while (bp->flags & VLIB_BUFFER_NEXT_PRESENT) |
| 855 | { |
| 856 | before_last = bp; |
| 857 | bp = vlib_get_buffer (vm, bp->next_buffer); |
| 858 | } |
| 859 | u8 *bt = vlib_buffer_get_tail (before_last); |
| 860 | |
| 861 | if (lb->current_length == icv_sz) |
| 862 | { |
| 863 | esp_footer_t *f = (esp_footer_t *) (bt - sizeof (*f)); |
| 864 | pad_length = f->pad_length; |
| 865 | next_header = f->next_header; |
| 866 | } |
| 867 | else |
| 868 | { |
| 869 | pad_length = (bt - 1)[0]; |
| 870 | next_header = ((u8 *) vlib_buffer_get_current (lb))[0]; |
| 871 | } |
| 872 | } |
| 873 | else |
| 874 | { |
| 875 | esp_footer_t *f = |
| 876 | (esp_footer_t *) (lb->data + lb->current_data + |
| 877 | lb->current_length - sizeof (esp_footer_t) - |
| 878 | icv_sz); |
| 879 | pad_length = f->pad_length; |
| 880 | next_header = f->next_header; |
| 881 | } |
| 882 | } |
| 883 | else |
| 884 | { |
| 885 | icv_sz = pd->icv_sz; |
| 886 | esp_footer_t *f = |
| 887 | (esp_footer_t *) (lb->data + lb->current_data + lb->current_length - |
| 888 | sizeof (esp_footer_t) - icv_sz); |
| 889 | pad_length = f->pad_length; |
| 890 | next_header = f->next_header; |
| 891 | } |
| 892 | |
| 893 | u16 adv = pd->iv_sz + esp_sz; |
| 894 | u16 tail = sizeof (esp_footer_t) + pad_length + icv_sz; |
| 895 | u16 tail_orig = sizeof (esp_footer_t) + pad_length + pd->icv_sz; |
Frédéric Perrin | a4157ae | 2023-07-14 11:13:42 +0100 | [diff] [blame] | 896 | b->flags &= |
Arthur de Kerhor | 8fce546 | 2023-06-16 09:48:52 +0200 | [diff] [blame] | 897 | ~(VNET_BUFFER_F_L4_CHECKSUM_COMPUTED | VNET_BUFFER_F_L4_CHECKSUM_CORRECT); |
Fan Zhang | f539578 | 2020-04-29 14:00:03 +0100 | [diff] [blame] | 898 | |
| 899 | if ((pd->flags & tun_flags) == 0 && !is_tun) /* transport mode */ |
| 900 | { |
| 901 | u8 udp_sz = (is_ip6 == 0 && pd->flags & IPSEC_SA_FLAG_UDP_ENCAP) ? |
| 902 | sizeof (udp_header_t) : 0; |
| 903 | u16 ip_hdr_sz = pd->hdr_sz - udp_sz; |
| 904 | u8 *old_ip = b->data + pd->current_data - ip_hdr_sz - udp_sz; |
| 905 | u8 *ip = old_ip + adv + udp_sz; |
| 906 | |
| 907 | if (is_ip6 && ip_hdr_sz > 64) |
| 908 | memmove (ip, old_ip, ip_hdr_sz); |
| 909 | else |
| 910 | clib_memcpy_le64 (ip, old_ip, ip_hdr_sz); |
| 911 | |
| 912 | b->current_data = pd->current_data + adv - ip_hdr_sz; |
| 913 | b->current_length += ip_hdr_sz - adv; |
| 914 | esp_remove_tail (vm, b, lb, tail); |
| 915 | |
| 916 | if (is_ip6) |
| 917 | { |
| 918 | ip6_header_t *ip6 = (ip6_header_t *) ip; |
| 919 | u16 len = clib_net_to_host_u16 (ip6->payload_length); |
| 920 | len -= adv + tail_orig; |
| 921 | ip6->payload_length = clib_host_to_net_u16 (len); |
| 922 | ip6->protocol = next_header; |
| 923 | next[0] = ESP_DECRYPT_NEXT_IP6_INPUT; |
| 924 | } |
| 925 | else |
| 926 | { |
| 927 | ip4_header_t *ip4 = (ip4_header_t *) ip; |
| 928 | ip_csum_t sum = ip4->checksum; |
| 929 | u16 len = clib_net_to_host_u16 (ip4->length); |
| 930 | len = clib_host_to_net_u16 (len - adv - tail_orig - udp_sz); |
| 931 | sum = ip_csum_update (sum, ip4->protocol, next_header, |
| 932 | ip4_header_t, protocol); |
| 933 | sum = ip_csum_update (sum, ip4->length, len, ip4_header_t, length); |
| 934 | ip4->checksum = ip_csum_fold (sum); |
| 935 | ip4->protocol = next_header; |
| 936 | ip4->length = len; |
| 937 | next[0] = ESP_DECRYPT_NEXT_IP4_INPUT; |
| 938 | } |
| 939 | } |
| 940 | else |
| 941 | { |
| 942 | if (PREDICT_TRUE (next_header == IP_PROTOCOL_IP_IN_IP)) |
| 943 | { |
| 944 | next[0] = ESP_DECRYPT_NEXT_IP4_INPUT; |
| 945 | b->current_data = pd->current_data + adv; |
| 946 | b->current_length = pd->current_length - adv; |
Arthur de Kerhor | 8fce546 | 2023-06-16 09:48:52 +0200 | [diff] [blame] | 947 | esp_remove_tail_and_tfc_padding (vm, node, pd, b, lb, next, tail, |
| 948 | false); |
Fan Zhang | f539578 | 2020-04-29 14:00:03 +0100 | [diff] [blame] | 949 | } |
| 950 | else if (next_header == IP_PROTOCOL_IPV6) |
| 951 | { |
| 952 | next[0] = ESP_DECRYPT_NEXT_IP6_INPUT; |
| 953 | b->current_data = pd->current_data + adv; |
| 954 | b->current_length = pd->current_length - adv; |
Arthur de Kerhor | 8fce546 | 2023-06-16 09:48:52 +0200 | [diff] [blame] | 955 | esp_remove_tail_and_tfc_padding (vm, node, pd, b, lb, next, tail, |
| 956 | true); |
Fan Zhang | f539578 | 2020-04-29 14:00:03 +0100 | [diff] [blame] | 957 | } |
Neale Ranns | 4a58e49 | 2020-12-21 13:19:10 +0000 | [diff] [blame] | 958 | else if (next_header == IP_PROTOCOL_MPLS_IN_IP) |
| 959 | { |
| 960 | next[0] = ESP_DECRYPT_NEXT_MPLS_INPUT; |
| 961 | b->current_data = pd->current_data + adv; |
| 962 | b->current_length = pd->current_length - adv; |
| 963 | esp_remove_tail (vm, b, lb, tail); |
| 964 | } |
Benoît Ganne | 98ca76a | 2022-04-11 18:51:25 +0200 | [diff] [blame] | 965 | else if (is_tun && next_header == IP_PROTOCOL_GRE) |
Fan Zhang | f539578 | 2020-04-29 14:00:03 +0100 | [diff] [blame] | 966 | { |
Benoît Ganne | 98ca76a | 2022-04-11 18:51:25 +0200 | [diff] [blame] | 967 | gre_header_t *gre; |
| 968 | |
| 969 | b->current_data = pd->current_data + adv; |
| 970 | b->current_length = pd->current_length - adv - tail; |
| 971 | |
| 972 | gre = vlib_buffer_get_current (b); |
| 973 | |
| 974 | vlib_buffer_advance (b, sizeof (*gre)); |
| 975 | |
| 976 | switch (clib_net_to_host_u16 (gre->protocol)) |
Fan Zhang | f539578 | 2020-04-29 14:00:03 +0100 | [diff] [blame] | 977 | { |
Benoît Ganne | 98ca76a | 2022-04-11 18:51:25 +0200 | [diff] [blame] | 978 | case GRE_PROTOCOL_teb: |
| 979 | vnet_update_l2_len (b); |
| 980 | next[0] = ESP_DECRYPT_NEXT_L2_INPUT; |
| 981 | break; |
| 982 | case GRE_PROTOCOL_ip4: |
| 983 | next[0] = ESP_DECRYPT_NEXT_IP4_INPUT; |
| 984 | break; |
| 985 | case GRE_PROTOCOL_ip6: |
| 986 | next[0] = ESP_DECRYPT_NEXT_IP6_INPUT; |
| 987 | break; |
| 988 | default: |
Arthur de Kerhor | ad95b06 | 2022-11-16 19:12:05 +0100 | [diff] [blame] | 989 | esp_decrypt_set_next_index ( |
| 990 | b, node, vm->thread_index, ESP_DECRYPT_ERROR_UNSUP_PAYLOAD, 0, |
| 991 | next, ESP_DECRYPT_NEXT_DROP, pd->sa_index); |
Benoît Ganne | 98ca76a | 2022-04-11 18:51:25 +0200 | [diff] [blame] | 992 | break; |
Fan Zhang | f539578 | 2020-04-29 14:00:03 +0100 | [diff] [blame] | 993 | } |
| 994 | } |
Benoît Ganne | 98ca76a | 2022-04-11 18:51:25 +0200 | [diff] [blame] | 995 | else if ((next[0] = vec_elt (next_by_next_header, next_header)) != |
| 996 | (u16) ~0) |
| 997 | { |
| 998 | b->current_data = pd->current_data + adv; |
| 999 | b->current_length = pd->current_length - adv; |
| 1000 | esp_remove_tail (vm, b, lb, tail); |
| 1001 | } |
| 1002 | else |
| 1003 | { |
Arthur de Kerhor | ad95b06 | 2022-11-16 19:12:05 +0100 | [diff] [blame] | 1004 | esp_decrypt_set_next_index (b, node, vm->thread_index, |
| 1005 | ESP_DECRYPT_ERROR_UNSUP_PAYLOAD, 0, next, |
| 1006 | ESP_DECRYPT_NEXT_DROP, pd->sa_index); |
Benoît Ganne | 98ca76a | 2022-04-11 18:51:25 +0200 | [diff] [blame] | 1007 | return; |
| 1008 | } |
| 1009 | |
Fan Zhang | f539578 | 2020-04-29 14:00:03 +0100 | [diff] [blame] | 1010 | if (is_tun) |
| 1011 | { |
| 1012 | if (ipsec_sa_is_set_IS_PROTECT (sa0)) |
| 1013 | { |
| 1014 | /* |
| 1015 | * There are two encap possibilities |
| 1016 | * 1) the tunnel and ths SA are prodiving encap, i.e. it's |
| 1017 | * MAC | SA-IP | TUN-IP | ESP | PAYLOAD |
| 1018 | * implying the SA is in tunnel mode (on a tunnel interface) |
| 1019 | * 2) only the tunnel provides encap |
| 1020 | * MAC | TUN-IP | ESP | PAYLOAD |
| 1021 | * implying the SA is in transport mode. |
| 1022 | * |
| 1023 | * For 2) we need only strip the tunnel encap and we're good. |
| 1024 | * since the tunnel and crypto ecnap (int the tun=protect |
| 1025 | * object) are the same and we verified above that these match |
| 1026 | * for 1) we need to strip the SA-IP outer headers, to |
| 1027 | * reveal the tunnel IP and then check that this matches |
| 1028 | * the configured tunnel. |
| 1029 | */ |
| 1030 | const ipsec_tun_protect_t *itp; |
| 1031 | |
Neale Ranns | 5b89110 | 2021-06-28 13:31:28 +0000 | [diff] [blame] | 1032 | itp = |
| 1033 | ipsec_tun_protect_get (vnet_buffer (b)->ipsec.protect_index); |
Fan Zhang | f539578 | 2020-04-29 14:00:03 +0100 | [diff] [blame] | 1034 | |
| 1035 | if (PREDICT_TRUE (next_header == IP_PROTOCOL_IP_IN_IP)) |
| 1036 | { |
| 1037 | const ip4_header_t *ip4; |
| 1038 | |
| 1039 | ip4 = vlib_buffer_get_current (b); |
| 1040 | |
| 1041 | if (!ip46_address_is_equal_v4 (&itp->itp_tun.src, |
| 1042 | &ip4->dst_address) || |
| 1043 | !ip46_address_is_equal_v4 (&itp->itp_tun.dst, |
| 1044 | &ip4->src_address)) |
| 1045 | { |
Arthur de Kerhor | ad95b06 | 2022-11-16 19:12:05 +0100 | [diff] [blame] | 1046 | esp_decrypt_set_next_index ( |
| 1047 | b, node, vm->thread_index, |
| 1048 | ESP_DECRYPT_ERROR_TUN_NO_PROTO, 0, next, |
| 1049 | ESP_DECRYPT_NEXT_DROP, pd->sa_index); |
Fan Zhang | f539578 | 2020-04-29 14:00:03 +0100 | [diff] [blame] | 1050 | } |
| 1051 | } |
| 1052 | else if (next_header == IP_PROTOCOL_IPV6) |
| 1053 | { |
| 1054 | const ip6_header_t *ip6; |
| 1055 | |
| 1056 | ip6 = vlib_buffer_get_current (b); |
| 1057 | |
| 1058 | if (!ip46_address_is_equal_v6 (&itp->itp_tun.src, |
| 1059 | &ip6->dst_address) || |
| 1060 | !ip46_address_is_equal_v6 (&itp->itp_tun.dst, |
| 1061 | &ip6->src_address)) |
| 1062 | { |
Arthur de Kerhor | ad95b06 | 2022-11-16 19:12:05 +0100 | [diff] [blame] | 1063 | esp_decrypt_set_next_index ( |
| 1064 | b, node, vm->thread_index, |
| 1065 | ESP_DECRYPT_ERROR_TUN_NO_PROTO, 0, next, |
| 1066 | ESP_DECRYPT_NEXT_DROP, pd->sa_index); |
Fan Zhang | f539578 | 2020-04-29 14:00:03 +0100 | [diff] [blame] | 1067 | } |
| 1068 | } |
| 1069 | } |
| 1070 | } |
| 1071 | } |
Neale Ranns | e11203e | 2021-09-21 12:34:19 +0000 | [diff] [blame] | 1072 | |
| 1073 | if (PREDICT_FALSE (n_lost)) |
Arthur de Kerhor | ad95b06 | 2022-11-16 19:12:05 +0100 | [diff] [blame] | 1074 | vlib_increment_simple_counter (&ipsec_sa_err_counters[IPSEC_SA_ERROR_LOST], |
| 1075 | vm->thread_index, pd->sa_index, n_lost); |
Fan Zhang | f539578 | 2020-04-29 14:00:03 +0100 | [diff] [blame] | 1076 | } |
| 1077 | |
Klement Sekera | be5a5dd | 2018-10-09 16:05:48 +0200 | [diff] [blame] | 1078 | always_inline uword |
Neale Ranns | f16e9a5 | 2021-02-25 19:09:24 +0000 | [diff] [blame] | 1079 | esp_decrypt_inline (vlib_main_t *vm, vlib_node_runtime_t *node, |
| 1080 | vlib_frame_t *from_frame, int is_ip6, int is_tun, |
| 1081 | u16 async_next_node) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1082 | { |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1083 | ipsec_main_t *im = &ipsec_main; |
Benoît Ganne | 98ca76a | 2022-04-11 18:51:25 +0200 | [diff] [blame] | 1084 | const u16 *next_by_next_header = im->next_header_registrations; |
Damjan Marion | b4fff3a | 2019-03-25 15:54:40 +0100 | [diff] [blame] | 1085 | u32 thread_index = vm->thread_index; |
Damjan Marion | b4fff3a | 2019-03-25 15:54:40 +0100 | [diff] [blame] | 1086 | u16 len; |
| 1087 | ipsec_per_thread_data_t *ptd = vec_elt_at_index (im->ptd, thread_index); |
Damjan Marion | c98275f | 2019-03-06 14:05:01 +0100 | [diff] [blame] | 1088 | u32 *from = vlib_frame_vector_args (from_frame); |
Filip Tehlar | efcad1a | 2020-02-04 09:36:04 +0000 | [diff] [blame] | 1089 | u32 n_left = from_frame->n_vectors; |
Damjan Marion | b4fff3a | 2019-03-25 15:54:40 +0100 | [diff] [blame] | 1090 | vlib_buffer_t *bufs[VLIB_FRAME_SIZE], **b = bufs; |
Neale Ranns | f16e9a5 | 2021-02-25 19:09:24 +0000 | [diff] [blame] | 1091 | vlib_buffer_t *sync_bufs[VLIB_FRAME_SIZE]; |
| 1092 | u16 sync_nexts[VLIB_FRAME_SIZE], *sync_next = sync_nexts, n_sync = 0; |
Matthew Smith | 51d56ba | 2021-06-04 09:18:37 -0500 | [diff] [blame] | 1093 | u16 async_nexts[VLIB_FRAME_SIZE], *async_next = async_nexts; |
Damjan Marion | dd298e8 | 2022-10-12 16:02:18 +0200 | [diff] [blame] | 1094 | u16 noop_nexts[VLIB_FRAME_SIZE], n_noop = 0; |
Neale Ranns | f16e9a5 | 2021-02-25 19:09:24 +0000 | [diff] [blame] | 1095 | u32 sync_bi[VLIB_FRAME_SIZE]; |
| 1096 | u32 noop_bi[VLIB_FRAME_SIZE]; |
Damjan Marion | b4fff3a | 2019-03-25 15:54:40 +0100 | [diff] [blame] | 1097 | esp_decrypt_packet_data_t pkt_data[VLIB_FRAME_SIZE], *pd = pkt_data; |
Fan Zhang | f539578 | 2020-04-29 14:00:03 +0100 | [diff] [blame] | 1098 | esp_decrypt_packet_data2_t pkt_data2[VLIB_FRAME_SIZE], *pd2 = pkt_data2; |
Damjan Marion | b4fff3a | 2019-03-25 15:54:40 +0100 | [diff] [blame] | 1099 | esp_decrypt_packet_data_t cpd = { }; |
| 1100 | u32 current_sa_index = ~0, current_sa_bytes = 0, current_sa_pkts = 0; |
| 1101 | const u8 esp_sz = sizeof (esp_header_t); |
| 1102 | ipsec_sa_t *sa0 = 0; |
Maxime Peim | 0e2f188 | 2022-12-22 11:26:57 +0000 | [diff] [blame] | 1103 | bool anti_replay_result; |
Filip Tehlar | e4e8c6b | 2020-02-13 07:49:30 +0000 | [diff] [blame] | 1104 | vnet_crypto_op_t _op, *op = &_op; |
Benoît Ganne | e631ece | 2021-05-27 18:49:42 +0200 | [diff] [blame] | 1105 | vnet_crypto_op_t **crypto_ops; |
| 1106 | vnet_crypto_op_t **integ_ops; |
Fan Zhang | f539578 | 2020-04-29 14:00:03 +0100 | [diff] [blame] | 1107 | int is_async = im->async_mode; |
Neale Ranns | fc81134 | 2021-02-26 10:35:33 +0000 | [diff] [blame] | 1108 | vnet_crypto_async_op_id_t async_op = ~0; |
Neale Ranns | fc81134 | 2021-02-26 10:35:33 +0000 | [diff] [blame] | 1109 | vnet_crypto_async_frame_t *async_frames[VNET_CRYPTO_ASYNC_OP_N_IDS]; |
Neale Ranns | f16e9a5 | 2021-02-25 19:09:24 +0000 | [diff] [blame] | 1110 | esp_decrypt_error_t err; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1111 | |
Damjan Marion | b4fff3a | 2019-03-25 15:54:40 +0100 | [diff] [blame] | 1112 | vlib_get_buffers (vm, from, b, n_left); |
Fan Zhang | f539578 | 2020-04-29 14:00:03 +0100 | [diff] [blame] | 1113 | if (!is_async) |
| 1114 | { |
| 1115 | vec_reset_length (ptd->crypto_ops); |
| 1116 | vec_reset_length (ptd->integ_ops); |
| 1117 | vec_reset_length (ptd->chained_crypto_ops); |
| 1118 | vec_reset_length (ptd->chained_integ_ops); |
| 1119 | } |
Neale Ranns | fc81134 | 2021-02-26 10:35:33 +0000 | [diff] [blame] | 1120 | vec_reset_length (ptd->async_frames); |
Filip Tehlar | efcad1a | 2020-02-04 09:36:04 +0000 | [diff] [blame] | 1121 | vec_reset_length (ptd->chunks); |
Neale Ranns | f16e9a5 | 2021-02-25 19:09:24 +0000 | [diff] [blame] | 1122 | clib_memset (sync_nexts, -1, sizeof (sync_nexts)); |
Neale Ranns | fc81134 | 2021-02-26 10:35:33 +0000 | [diff] [blame] | 1123 | clib_memset (async_frames, 0, sizeof (async_frames)); |
Damjan Marion | b4fff3a | 2019-03-25 15:54:40 +0100 | [diff] [blame] | 1124 | |
| 1125 | while (n_left > 0) |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 1126 | { |
Damjan Marion | b4fff3a | 2019-03-25 15:54:40 +0100 | [diff] [blame] | 1127 | u8 *payload; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1128 | |
Neale Ranns | f16e9a5 | 2021-02-25 19:09:24 +0000 | [diff] [blame] | 1129 | err = ESP_DECRYPT_ERROR_RX_PKTS; |
Damjan Marion | b4fff3a | 2019-03-25 15:54:40 +0100 | [diff] [blame] | 1130 | if (n_left > 2) |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 1131 | { |
Damjan Marion | b4fff3a | 2019-03-25 15:54:40 +0100 | [diff] [blame] | 1132 | u8 *p; |
| 1133 | vlib_prefetch_buffer_header (b[2], LOAD); |
| 1134 | p = vlib_buffer_get_current (b[1]); |
Damjan Marion | af7fb04 | 2021-07-15 11:54:41 +0200 | [diff] [blame] | 1135 | clib_prefetch_load (p); |
Damjan Marion | b4fff3a | 2019-03-25 15:54:40 +0100 | [diff] [blame] | 1136 | p -= CLIB_CACHE_LINE_BYTES; |
Damjan Marion | af7fb04 | 2021-07-15 11:54:41 +0200 | [diff] [blame] | 1137 | clib_prefetch_load (p); |
Damjan Marion | b4fff3a | 2019-03-25 15:54:40 +0100 | [diff] [blame] | 1138 | } |
| 1139 | |
Filip Tehlar | efcad1a | 2020-02-04 09:36:04 +0000 | [diff] [blame] | 1140 | u32 n_bufs = vlib_buffer_chain_linearize (vm, b[0]); |
| 1141 | if (n_bufs == 0) |
Damjan Marion | b4fff3a | 2019-03-25 15:54:40 +0100 | [diff] [blame] | 1142 | { |
Neale Ranns | f16e9a5 | 2021-02-25 19:09:24 +0000 | [diff] [blame] | 1143 | err = ESP_DECRYPT_ERROR_NO_BUFFERS; |
Arthur de Kerhor | ad95b06 | 2022-11-16 19:12:05 +0100 | [diff] [blame] | 1144 | esp_decrypt_set_next_index (b[0], node, thread_index, err, n_noop, |
| 1145 | noop_nexts, ESP_DECRYPT_NEXT_DROP, |
| 1146 | vnet_buffer (b[0])->ipsec.sad_index); |
Damjan Marion | b4fff3a | 2019-03-25 15:54:40 +0100 | [diff] [blame] | 1147 | goto next; |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 1148 | } |
Damjan Marion | c98275f | 2019-03-06 14:05:01 +0100 | [diff] [blame] | 1149 | |
Damjan Marion | b4fff3a | 2019-03-25 15:54:40 +0100 | [diff] [blame] | 1150 | if (vnet_buffer (b[0])->ipsec.sad_index != current_sa_index) |
Damjan Marion | c98275f | 2019-03-06 14:05:01 +0100 | [diff] [blame] | 1151 | { |
Damjan Marion | 867dfdd | 2019-06-05 15:42:54 +0200 | [diff] [blame] | 1152 | if (current_sa_pkts) |
| 1153 | vlib_increment_combined_counter (&ipsec_sa_counters, thread_index, |
Arthur de Kerhor | ad95b06 | 2022-11-16 19:12:05 +0100 | [diff] [blame] | 1154 | current_sa_index, current_sa_pkts, |
Damjan Marion | 867dfdd | 2019-06-05 15:42:54 +0200 | [diff] [blame] | 1155 | current_sa_bytes); |
| 1156 | current_sa_bytes = current_sa_pkts = 0; |
| 1157 | |
Damjan Marion | b4fff3a | 2019-03-25 15:54:40 +0100 | [diff] [blame] | 1158 | current_sa_index = vnet_buffer (b[0])->ipsec.sad_index; |
Arthur de Kerhor | ad95b06 | 2022-11-16 19:12:05 +0100 | [diff] [blame] | 1159 | vlib_prefetch_combined_counter (&ipsec_sa_counters, thread_index, |
| 1160 | current_sa_index); |
Neale Ranns | c5fe57d | 2021-02-25 16:01:28 +0000 | [diff] [blame] | 1161 | sa0 = ipsec_sa_get (current_sa_index); |
Neale Ranns | 123b5eb | 2020-10-16 14:03:55 +0000 | [diff] [blame] | 1162 | |
| 1163 | /* fetch the second cacheline ASAP */ |
Damjan Marion | af7fb04 | 2021-07-15 11:54:41 +0200 | [diff] [blame] | 1164 | clib_prefetch_load (sa0->cacheline1); |
Damjan Marion | 7c22ff7 | 2019-04-04 12:25:44 +0200 | [diff] [blame] | 1165 | cpd.icv_sz = sa0->integ_icv_size; |
Damjan Marion | b4fff3a | 2019-03-25 15:54:40 +0100 | [diff] [blame] | 1166 | cpd.iv_sz = sa0->crypto_iv_size; |
| 1167 | cpd.flags = sa0->flags; |
| 1168 | cpd.sa_index = current_sa_index; |
Neale Ranns | f16e9a5 | 2021-02-25 19:09:24 +0000 | [diff] [blame] | 1169 | is_async = im->async_mode | ipsec_sa_is_set_IS_ASYNC (sa0); |
Neale Ranns | fc81134 | 2021-02-26 10:35:33 +0000 | [diff] [blame] | 1170 | } |
Fan Zhang | f539578 | 2020-04-29 14:00:03 +0100 | [diff] [blame] | 1171 | |
Benoît Ganne | 5527a78 | 2022-01-18 15:56:41 +0100 | [diff] [blame] | 1172 | if (PREDICT_FALSE ((u16) ~0 == sa0->thread_index)) |
Neale Ranns | f62a8c0 | 2019-04-02 08:13:33 +0000 | [diff] [blame] | 1173 | { |
| 1174 | /* this is the first packet to use this SA, claim the SA |
| 1175 | * for this thread. this could happen simultaneously on |
| 1176 | * another thread */ |
Neale Ranns | 1a52d37 | 2021-02-04 11:33:32 +0000 | [diff] [blame] | 1177 | clib_atomic_cmp_and_swap (&sa0->thread_index, ~0, |
Neale Ranns | f62a8c0 | 2019-04-02 08:13:33 +0000 | [diff] [blame] | 1178 | ipsec_sa_assign_thread (thread_index)); |
| 1179 | } |
| 1180 | |
Neale Ranns | 1a52d37 | 2021-02-04 11:33:32 +0000 | [diff] [blame] | 1181 | if (PREDICT_FALSE (thread_index != sa0->thread_index)) |
Neale Ranns | f62a8c0 | 2019-04-02 08:13:33 +0000 | [diff] [blame] | 1182 | { |
Neale Ranns | aa7d766 | 2021-02-10 08:42:49 +0000 | [diff] [blame] | 1183 | vnet_buffer (b[0])->ipsec.thread_index = sa0->thread_index; |
Neale Ranns | f16e9a5 | 2021-02-25 19:09:24 +0000 | [diff] [blame] | 1184 | err = ESP_DECRYPT_ERROR_HANDOFF; |
Arthur de Kerhor | ad95b06 | 2022-11-16 19:12:05 +0100 | [diff] [blame] | 1185 | esp_decrypt_set_next_index (b[0], node, thread_index, err, n_noop, |
| 1186 | noop_nexts, ESP_DECRYPT_NEXT_HANDOFF, |
| 1187 | current_sa_index); |
Neale Ranns | f62a8c0 | 2019-04-02 08:13:33 +0000 | [diff] [blame] | 1188 | goto next; |
| 1189 | } |
| 1190 | |
Damjan Marion | b4fff3a | 2019-03-25 15:54:40 +0100 | [diff] [blame] | 1191 | /* store packet data for next round for easier prefetch */ |
| 1192 | pd->sa_data = cpd.sa_data; |
| 1193 | pd->current_data = b[0]->current_data; |
Damjan Marion | b4fff3a | 2019-03-25 15:54:40 +0100 | [diff] [blame] | 1194 | pd->hdr_sz = pd->current_data - vnet_buffer (b[0])->l3_hdr_offset; |
| 1195 | payload = b[0]->data + pd->current_data; |
Neale Ranns | 6afaae1 | 2019-07-17 15:07:14 +0000 | [diff] [blame] | 1196 | pd->seq = clib_host_to_net_u32 (((esp_header_t *) payload)->seq); |
Fan Zhang | f539578 | 2020-04-29 14:00:03 +0100 | [diff] [blame] | 1197 | pd->is_chain = 0; |
| 1198 | pd2->lb = b[0]; |
| 1199 | pd2->free_buffer_index = 0; |
| 1200 | pd2->icv_removed = 0; |
Filip Tehlar | efcad1a | 2020-02-04 09:36:04 +0000 | [diff] [blame] | 1201 | |
Filip Tehlar | efcad1a | 2020-02-04 09:36:04 +0000 | [diff] [blame] | 1202 | if (n_bufs > 1) |
| 1203 | { |
Fan Zhang | f539578 | 2020-04-29 14:00:03 +0100 | [diff] [blame] | 1204 | pd->is_chain = 1; |
Filip Tehlar | efcad1a | 2020-02-04 09:36:04 +0000 | [diff] [blame] | 1205 | /* find last buffer in the chain */ |
Fan Zhang | f539578 | 2020-04-29 14:00:03 +0100 | [diff] [blame] | 1206 | while (pd2->lb->flags & VLIB_BUFFER_NEXT_PRESENT) |
| 1207 | pd2->lb = vlib_get_buffer (vm, pd2->lb->next_buffer); |
Filip Tehlar | efcad1a | 2020-02-04 09:36:04 +0000 | [diff] [blame] | 1208 | |
| 1209 | crypto_ops = &ptd->chained_crypto_ops; |
| 1210 | integ_ops = &ptd->chained_integ_ops; |
| 1211 | } |
Benoît Ganne | e631ece | 2021-05-27 18:49:42 +0200 | [diff] [blame] | 1212 | else |
| 1213 | { |
| 1214 | crypto_ops = &ptd->crypto_ops; |
| 1215 | integ_ops = &ptd->integ_ops; |
| 1216 | } |
Fan Zhang | f539578 | 2020-04-29 14:00:03 +0100 | [diff] [blame] | 1217 | |
Filip Tehlar | efcad1a | 2020-02-04 09:36:04 +0000 | [diff] [blame] | 1218 | pd->current_length = b[0]->current_length; |
Damjan Marion | b4fff3a | 2019-03-25 15:54:40 +0100 | [diff] [blame] | 1219 | |
Damjan Marion | b4fff3a | 2019-03-25 15:54:40 +0100 | [diff] [blame] | 1220 | /* anti-reply check */ |
Maxime Peim | 0e2f188 | 2022-12-22 11:26:57 +0000 | [diff] [blame] | 1221 | if (PREDICT_FALSE (ipsec_sa_is_set_ANTI_REPLAY_HUGE (sa0))) |
| 1222 | { |
| 1223 | anti_replay_result = ipsec_sa_anti_replay_and_sn_advance ( |
| 1224 | sa0, pd->seq, ~0, false, &pd->seq_hi, true); |
| 1225 | } |
| 1226 | else |
| 1227 | { |
| 1228 | anti_replay_result = ipsec_sa_anti_replay_and_sn_advance ( |
| 1229 | sa0, pd->seq, ~0, false, &pd->seq_hi, false); |
| 1230 | } |
| 1231 | |
| 1232 | if (anti_replay_result) |
Damjan Marion | b4fff3a | 2019-03-25 15:54:40 +0100 | [diff] [blame] | 1233 | { |
Neale Ranns | f16e9a5 | 2021-02-25 19:09:24 +0000 | [diff] [blame] | 1234 | err = ESP_DECRYPT_ERROR_REPLAY; |
Arthur de Kerhor | ad95b06 | 2022-11-16 19:12:05 +0100 | [diff] [blame] | 1235 | esp_decrypt_set_next_index (b[0], node, thread_index, err, n_noop, |
| 1236 | noop_nexts, ESP_DECRYPT_NEXT_DROP, |
| 1237 | current_sa_index); |
Damjan Marion | b4fff3a | 2019-03-25 15:54:40 +0100 | [diff] [blame] | 1238 | goto next; |
| 1239 | } |
| 1240 | |
Damjan Marion | a829b13 | 2019-04-24 23:39:16 +0200 | [diff] [blame] | 1241 | if (pd->current_length < cpd.icv_sz + esp_sz + cpd.iv_sz) |
| 1242 | { |
Neale Ranns | f16e9a5 | 2021-02-25 19:09:24 +0000 | [diff] [blame] | 1243 | err = ESP_DECRYPT_ERROR_RUNT; |
Arthur de Kerhor | ad95b06 | 2022-11-16 19:12:05 +0100 | [diff] [blame] | 1244 | esp_decrypt_set_next_index (b[0], node, thread_index, err, n_noop, |
| 1245 | noop_nexts, ESP_DECRYPT_NEXT_DROP, |
| 1246 | current_sa_index); |
Damjan Marion | a829b13 | 2019-04-24 23:39:16 +0200 | [diff] [blame] | 1247 | goto next; |
| 1248 | } |
| 1249 | |
Damjan Marion | b4fff3a | 2019-03-25 15:54:40 +0100 | [diff] [blame] | 1250 | len = pd->current_length - cpd.icv_sz; |
| 1251 | current_sa_pkts += 1; |
Filip Tehlar | efcad1a | 2020-02-04 09:36:04 +0000 | [diff] [blame] | 1252 | current_sa_bytes += vlib_buffer_length_in_chain (vm, b[0]); |
Damjan Marion | b4fff3a | 2019-03-25 15:54:40 +0100 | [diff] [blame] | 1253 | |
Fan Zhang | f539578 | 2020-04-29 14:00:03 +0100 | [diff] [blame] | 1254 | if (is_async) |
Damjan Marion | b4fff3a | 2019-03-25 15:54:40 +0100 | [diff] [blame] | 1255 | { |
Matthew Smith | 51d56ba | 2021-06-04 09:18:37 -0500 | [diff] [blame] | 1256 | async_op = sa0->crypto_async_dec_op_id; |
| 1257 | |
| 1258 | /* get a frame for this op if we don't yet have one or it's full |
| 1259 | */ |
| 1260 | if (NULL == async_frames[async_op] || |
| 1261 | vnet_crypto_async_frame_is_full (async_frames[async_op])) |
| 1262 | { |
| 1263 | async_frames[async_op] = |
| 1264 | vnet_crypto_async_get_frame (vm, async_op); |
gaoginskx | f441b5d | 2021-06-07 12:07:01 +0100 | [diff] [blame] | 1265 | if (PREDICT_FALSE (!async_frames[async_op])) |
| 1266 | { |
| 1267 | err = ESP_DECRYPT_ERROR_NO_AVAIL_FRAME; |
| 1268 | esp_decrypt_set_next_index ( |
| 1269 | b[0], node, thread_index, err, n_noop, noop_nexts, |
| 1270 | ESP_DECRYPT_NEXT_DROP, current_sa_index); |
| 1271 | goto next; |
| 1272 | } |
| 1273 | |
Matthew Smith | 51d56ba | 2021-06-04 09:18:37 -0500 | [diff] [blame] | 1274 | /* Save the frame to the list we'll submit at the end */ |
| 1275 | vec_add1 (ptd->async_frames, async_frames[async_op]); |
| 1276 | } |
Neale Ranns | fc81134 | 2021-02-26 10:35:33 +0000 | [diff] [blame] | 1277 | |
| 1278 | err = esp_decrypt_prepare_async_frame ( |
| 1279 | vm, node, ptd, async_frames[async_op], sa0, payload, len, |
Neale Ranns | f16e9a5 | 2021-02-25 19:09:24 +0000 | [diff] [blame] | 1280 | cpd.icv_sz, cpd.iv_sz, pd, pd2, from[b - bufs], b[0], async_next, |
| 1281 | async_next_node); |
Neale Ranns | fc81134 | 2021-02-26 10:35:33 +0000 | [diff] [blame] | 1282 | if (ESP_DECRYPT_ERROR_RX_PKTS != err) |
Filip Tehlar | efcad1a | 2020-02-04 09:36:04 +0000 | [diff] [blame] | 1283 | { |
Arthur de Kerhor | ad95b06 | 2022-11-16 19:12:05 +0100 | [diff] [blame] | 1284 | esp_decrypt_set_next_index ( |
| 1285 | b[0], node, thread_index, err, n_noop, noop_nexts, |
| 1286 | ESP_DECRYPT_NEXT_DROP, current_sa_index); |
Filip Tehlar | efcad1a | 2020-02-04 09:36:04 +0000 | [diff] [blame] | 1287 | } |
Damjan Marion | c98275f | 2019-03-06 14:05:01 +0100 | [diff] [blame] | 1288 | } |
Fan Zhang | f539578 | 2020-04-29 14:00:03 +0100 | [diff] [blame] | 1289 | else |
Neale Ranns | f16e9a5 | 2021-02-25 19:09:24 +0000 | [diff] [blame] | 1290 | esp_decrypt_prepare_sync_op ( |
| 1291 | vm, node, ptd, &crypto_ops, &integ_ops, op, sa0, payload, len, |
Neale Ranns | fe2d23f | 2022-11-18 04:24:09 +0000 | [diff] [blame] | 1292 | cpd.icv_sz, cpd.iv_sz, pd, pd2, b[0], sync_next, n_sync); |
Damjan Marion | c98275f | 2019-03-06 14:05:01 +0100 | [diff] [blame] | 1293 | /* next */ |
Damjan Marion | b4fff3a | 2019-03-25 15:54:40 +0100 | [diff] [blame] | 1294 | next: |
Neale Ranns | f16e9a5 | 2021-02-25 19:09:24 +0000 | [diff] [blame] | 1295 | if (ESP_DECRYPT_ERROR_RX_PKTS != err) |
| 1296 | { |
| 1297 | noop_bi[n_noop] = from[b - bufs]; |
| 1298 | n_noop++; |
Neale Ranns | f16e9a5 | 2021-02-25 19:09:24 +0000 | [diff] [blame] | 1299 | } |
| 1300 | else if (!is_async) |
| 1301 | { |
| 1302 | sync_bi[n_sync] = from[b - bufs]; |
| 1303 | sync_bufs[n_sync] = b[0]; |
| 1304 | n_sync++; |
| 1305 | sync_next++; |
| 1306 | pd += 1; |
| 1307 | pd2 += 1; |
| 1308 | } |
| 1309 | else |
Matthew Smith | 51d56ba | 2021-06-04 09:18:37 -0500 | [diff] [blame] | 1310 | async_next++; |
| 1311 | |
Damjan Marion | b4fff3a | 2019-03-25 15:54:40 +0100 | [diff] [blame] | 1312 | n_left -= 1; |
Damjan Marion | b4fff3a | 2019-03-25 15:54:40 +0100 | [diff] [blame] | 1313 | b += 1; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1314 | } |
Damjan Marion | c98275f | 2019-03-06 14:05:01 +0100 | [diff] [blame] | 1315 | |
Neale Ranns | 0295040 | 2019-12-20 00:54:57 +0000 | [diff] [blame] | 1316 | if (PREDICT_TRUE (~0 != current_sa_index)) |
| 1317 | vlib_increment_combined_counter (&ipsec_sa_counters, thread_index, |
| 1318 | current_sa_index, current_sa_pkts, |
| 1319 | current_sa_bytes); |
Klement Sekera | be5a5dd | 2018-10-09 16:05:48 +0200 | [diff] [blame] | 1320 | |
Matthew Smith | 51d56ba | 2021-06-04 09:18:37 -0500 | [diff] [blame] | 1321 | /* submit or free all of the open frames */ |
| 1322 | vnet_crypto_async_frame_t **async_frame; |
Neale Ranns | fc81134 | 2021-02-26 10:35:33 +0000 | [diff] [blame] | 1323 | |
Matthew Smith | 51d56ba | 2021-06-04 09:18:37 -0500 | [diff] [blame] | 1324 | vec_foreach (async_frame, ptd->async_frames) |
| 1325 | { |
| 1326 | /* free frame and move on if no ops were successfully added */ |
| 1327 | if (PREDICT_FALSE (!(*async_frame)->n_elts)) |
Fan Zhang | f539578 | 2020-04-29 14:00:03 +0100 | [diff] [blame] | 1328 | { |
Matthew Smith | 51d56ba | 2021-06-04 09:18:37 -0500 | [diff] [blame] | 1329 | vnet_crypto_async_free_frame (vm, *async_frame); |
| 1330 | continue; |
| 1331 | } |
| 1332 | if (vnet_crypto_async_submit_open_frame (vm, *async_frame) < 0) |
| 1333 | { |
| 1334 | n_noop += esp_async_recycle_failed_submit ( |
| 1335 | vm, *async_frame, node, ESP_DECRYPT_ERROR_CRYPTO_ENGINE_ERROR, |
Arthur de Kerhor | ad95b06 | 2022-11-16 19:12:05 +0100 | [diff] [blame] | 1336 | IPSEC_SA_ERROR_CRYPTO_ENGINE_ERROR, n_noop, noop_bi, noop_nexts, |
Xiaoming Jiang | 0c1454c | 2023-05-05 02:28:20 +0000 | [diff] [blame] | 1337 | ESP_DECRYPT_NEXT_DROP, false); |
Matthew Smith | 51d56ba | 2021-06-04 09:18:37 -0500 | [diff] [blame] | 1338 | vnet_crypto_async_reset_frame (*async_frame); |
| 1339 | vnet_crypto_async_free_frame (vm, *async_frame); |
Fan Zhang | f539578 | 2020-04-29 14:00:03 +0100 | [diff] [blame] | 1340 | } |
Fan Zhang | f539578 | 2020-04-29 14:00:03 +0100 | [diff] [blame] | 1341 | } |
Fan Zhang | f539578 | 2020-04-29 14:00:03 +0100 | [diff] [blame] | 1342 | |
Neale Ranns | f16e9a5 | 2021-02-25 19:09:24 +0000 | [diff] [blame] | 1343 | if (n_sync) |
| 1344 | { |
| 1345 | esp_process_ops (vm, node, ptd->integ_ops, sync_bufs, sync_nexts, |
| 1346 | ESP_DECRYPT_ERROR_INTEG_ERROR); |
| 1347 | esp_process_chained_ops (vm, node, ptd->chained_integ_ops, sync_bufs, |
| 1348 | sync_nexts, ptd->chunks, |
| 1349 | ESP_DECRYPT_ERROR_INTEG_ERROR); |
| 1350 | |
| 1351 | esp_process_ops (vm, node, ptd->crypto_ops, sync_bufs, sync_nexts, |
Fan Zhang | f539578 | 2020-04-29 14:00:03 +0100 | [diff] [blame] | 1352 | ESP_DECRYPT_ERROR_DECRYPTION_FAILED); |
Neale Ranns | f16e9a5 | 2021-02-25 19:09:24 +0000 | [diff] [blame] | 1353 | esp_process_chained_ops (vm, node, ptd->chained_crypto_ops, sync_bufs, |
| 1354 | sync_nexts, ptd->chunks, |
Fan Zhang | f539578 | 2020-04-29 14:00:03 +0100 | [diff] [blame] | 1355 | ESP_DECRYPT_ERROR_DECRYPTION_FAILED); |
| 1356 | } |
Damjan Marion | b4fff3a | 2019-03-25 15:54:40 +0100 | [diff] [blame] | 1357 | |
| 1358 | /* Post decryption ronud - adjust packet data start and length and next |
| 1359 | node */ |
| 1360 | |
Neale Ranns | f16e9a5 | 2021-02-25 19:09:24 +0000 | [diff] [blame] | 1361 | n_left = n_sync; |
| 1362 | sync_next = sync_nexts; |
Damjan Marion | b4fff3a | 2019-03-25 15:54:40 +0100 | [diff] [blame] | 1363 | pd = pkt_data; |
Fan Zhang | f539578 | 2020-04-29 14:00:03 +0100 | [diff] [blame] | 1364 | pd2 = pkt_data2; |
Neale Ranns | f16e9a5 | 2021-02-25 19:09:24 +0000 | [diff] [blame] | 1365 | b = sync_bufs; |
Damjan Marion | b4fff3a | 2019-03-25 15:54:40 +0100 | [diff] [blame] | 1366 | |
| 1367 | while (n_left) |
| 1368 | { |
Damjan Marion | b4fff3a | 2019-03-25 15:54:40 +0100 | [diff] [blame] | 1369 | if (n_left >= 2) |
| 1370 | { |
| 1371 | void *data = b[1]->data + pd[1].current_data; |
| 1372 | |
| 1373 | /* buffer metadata */ |
| 1374 | vlib_prefetch_buffer_header (b[1], LOAD); |
| 1375 | |
| 1376 | /* esp_footer_t */ |
| 1377 | CLIB_PREFETCH (data + pd[1].current_length - pd[1].icv_sz - 2, |
| 1378 | CLIB_CACHE_LINE_BYTES, LOAD); |
| 1379 | |
| 1380 | /* packet headers */ |
| 1381 | CLIB_PREFETCH (data - CLIB_CACHE_LINE_BYTES, |
| 1382 | CLIB_CACHE_LINE_BYTES * 2, LOAD); |
| 1383 | } |
| 1384 | |
Christian Hopps | d570e53 | 2020-08-25 12:40:40 -0400 | [diff] [blame] | 1385 | /* save the sa_index as GRE_teb post_crypto changes L2 opaque */ |
| 1386 | if (PREDICT_FALSE (b[0]->flags & VLIB_BUFFER_IS_TRACED)) |
| 1387 | current_sa_index = vnet_buffer (b[0])->ipsec.sad_index; |
| 1388 | |
Neale Ranns | f16e9a5 | 2021-02-25 19:09:24 +0000 | [diff] [blame] | 1389 | if (sync_next[0] >= ESP_DECRYPT_N_NEXT) |
Benoît Ganne | 98ca76a | 2022-04-11 18:51:25 +0200 | [diff] [blame] | 1390 | esp_decrypt_post_crypto (vm, node, next_by_next_header, pd, pd2, b[0], |
| 1391 | sync_next, is_ip6, is_tun, 0); |
Damjan Marion | b4fff3a | 2019-03-25 15:54:40 +0100 | [diff] [blame] | 1392 | |
Fan Zhang | f539578 | 2020-04-29 14:00:03 +0100 | [diff] [blame] | 1393 | /* trace: */ |
Damjan Marion | b4fff3a | 2019-03-25 15:54:40 +0100 | [diff] [blame] | 1394 | if (PREDICT_FALSE (b[0]->flags & VLIB_BUFFER_IS_TRACED)) |
| 1395 | { |
| 1396 | esp_decrypt_trace_t *tr; |
Damjan Marion | b4fff3a | 2019-03-25 15:54:40 +0100 | [diff] [blame] | 1397 | tr = vlib_add_trace (vm, node, b[0], sizeof (*tr)); |
Neale Ranns | c5fe57d | 2021-02-25 16:01:28 +0000 | [diff] [blame] | 1398 | sa0 = ipsec_sa_get (current_sa_index); |
Damjan Marion | b4fff3a | 2019-03-25 15:54:40 +0100 | [diff] [blame] | 1399 | tr->crypto_alg = sa0->crypto_alg; |
| 1400 | tr->integ_alg = sa0->integ_alg; |
Neale Ranns | 6afaae1 | 2019-07-17 15:07:14 +0000 | [diff] [blame] | 1401 | tr->seq = pd->seq; |
Neale Ranns | 5b89110 | 2021-06-28 13:31:28 +0000 | [diff] [blame] | 1402 | tr->sa_seq = sa0->seq; |
Neale Ranns | 6afaae1 | 2019-07-17 15:07:14 +0000 | [diff] [blame] | 1403 | tr->sa_seq_hi = sa0->seq_hi; |
Neale Ranns | 5b89110 | 2021-06-28 13:31:28 +0000 | [diff] [blame] | 1404 | tr->pkt_seq_hi = pd->seq_hi; |
Damjan Marion | b4fff3a | 2019-03-25 15:54:40 +0100 | [diff] [blame] | 1405 | } |
| 1406 | |
| 1407 | /* next */ |
| 1408 | n_left -= 1; |
Neale Ranns | f16e9a5 | 2021-02-25 19:09:24 +0000 | [diff] [blame] | 1409 | sync_next += 1; |
Damjan Marion | b4fff3a | 2019-03-25 15:54:40 +0100 | [diff] [blame] | 1410 | pd += 1; |
Fan Zhang | f539578 | 2020-04-29 14:00:03 +0100 | [diff] [blame] | 1411 | pd2 += 1; |
Damjan Marion | b4fff3a | 2019-03-25 15:54:40 +0100 | [diff] [blame] | 1412 | b += 1; |
| 1413 | } |
| 1414 | |
Neale Ranns | f16e9a5 | 2021-02-25 19:09:24 +0000 | [diff] [blame] | 1415 | vlib_node_increment_counter (vm, node->node_index, ESP_DECRYPT_ERROR_RX_PKTS, |
| 1416 | from_frame->n_vectors); |
Damjan Marion | b4fff3a | 2019-03-25 15:54:40 +0100 | [diff] [blame] | 1417 | |
Neale Ranns | f16e9a5 | 2021-02-25 19:09:24 +0000 | [diff] [blame] | 1418 | if (n_sync) |
| 1419 | vlib_buffer_enqueue_to_next (vm, node, sync_bi, sync_nexts, n_sync); |
Damjan Marion | b4fff3a | 2019-03-25 15:54:40 +0100 | [diff] [blame] | 1420 | |
Neale Ranns | f16e9a5 | 2021-02-25 19:09:24 +0000 | [diff] [blame] | 1421 | if (n_noop) |
| 1422 | vlib_buffer_enqueue_to_next (vm, node, noop_bi, noop_nexts, n_noop); |
| 1423 | |
| 1424 | return (from_frame->n_vectors); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1425 | } |
| 1426 | |
Fan Zhang | f539578 | 2020-04-29 14:00:03 +0100 | [diff] [blame] | 1427 | always_inline uword |
| 1428 | esp_decrypt_post_inline (vlib_main_t * vm, |
| 1429 | vlib_node_runtime_t * node, |
| 1430 | vlib_frame_t * from_frame, int is_ip6, int is_tun) |
| 1431 | { |
Benoît Ganne | 98ca76a | 2022-04-11 18:51:25 +0200 | [diff] [blame] | 1432 | const ipsec_main_t *im = &ipsec_main; |
| 1433 | const u16 *next_by_next_header = im->next_header_registrations; |
Fan Zhang | f539578 | 2020-04-29 14:00:03 +0100 | [diff] [blame] | 1434 | u32 *from = vlib_frame_vector_args (from_frame); |
| 1435 | u32 n_left = from_frame->n_vectors; |
| 1436 | vlib_buffer_t *bufs[VLIB_FRAME_SIZE], **b = bufs; |
| 1437 | u16 nexts[VLIB_FRAME_SIZE], *next = nexts; |
| 1438 | vlib_get_buffers (vm, from, b, n_left); |
| 1439 | |
| 1440 | while (n_left > 0) |
| 1441 | { |
| 1442 | esp_decrypt_packet_data_t *pd = &(esp_post_data (b[0]))->decrypt_data; |
| 1443 | |
| 1444 | if (n_left > 2) |
| 1445 | { |
| 1446 | vlib_prefetch_buffer_header (b[2], LOAD); |
| 1447 | vlib_prefetch_buffer_header (b[1], LOAD); |
| 1448 | } |
| 1449 | |
| 1450 | if (!pd->is_chain) |
Benoît Ganne | 98ca76a | 2022-04-11 18:51:25 +0200 | [diff] [blame] | 1451 | esp_decrypt_post_crypto (vm, node, next_by_next_header, pd, 0, b[0], |
| 1452 | next, is_ip6, is_tun, 1); |
Fan Zhang | f539578 | 2020-04-29 14:00:03 +0100 | [diff] [blame] | 1453 | else |
| 1454 | { |
| 1455 | esp_decrypt_packet_data2_t *pd2 = esp_post_data2 (b[0]); |
Benoît Ganne | 98ca76a | 2022-04-11 18:51:25 +0200 | [diff] [blame] | 1456 | esp_decrypt_post_crypto (vm, node, next_by_next_header, pd, pd2, |
| 1457 | b[0], next, is_ip6, is_tun, 1); |
Fan Zhang | f539578 | 2020-04-29 14:00:03 +0100 | [diff] [blame] | 1458 | } |
| 1459 | |
| 1460 | /*trace: */ |
| 1461 | if (PREDICT_FALSE (b[0]->flags & VLIB_BUFFER_IS_TRACED)) |
| 1462 | { |
Neale Ranns | c5fe57d | 2021-02-25 16:01:28 +0000 | [diff] [blame] | 1463 | ipsec_sa_t *sa0 = ipsec_sa_get (pd->sa_index); |
Fan Zhang | f539578 | 2020-04-29 14:00:03 +0100 | [diff] [blame] | 1464 | esp_decrypt_trace_t *tr; |
| 1465 | esp_decrypt_packet_data_t *async_pd = |
| 1466 | &(esp_post_data (b[0]))->decrypt_data; |
| 1467 | tr = vlib_add_trace (vm, node, b[0], sizeof (*tr)); |
Neale Ranns | c5fe57d | 2021-02-25 16:01:28 +0000 | [diff] [blame] | 1468 | sa0 = ipsec_sa_get (async_pd->sa_index); |
Fan Zhang | f539578 | 2020-04-29 14:00:03 +0100 | [diff] [blame] | 1469 | |
| 1470 | tr->crypto_alg = sa0->crypto_alg; |
| 1471 | tr->integ_alg = sa0->integ_alg; |
| 1472 | tr->seq = pd->seq; |
Neale Ranns | 5b89110 | 2021-06-28 13:31:28 +0000 | [diff] [blame] | 1473 | tr->sa_seq = sa0->seq; |
Fan Zhang | f539578 | 2020-04-29 14:00:03 +0100 | [diff] [blame] | 1474 | tr->sa_seq_hi = sa0->seq_hi; |
| 1475 | } |
| 1476 | |
| 1477 | n_left--; |
| 1478 | next++; |
| 1479 | b++; |
| 1480 | } |
| 1481 | |
| 1482 | n_left = from_frame->n_vectors; |
| 1483 | vlib_node_increment_counter (vm, node->node_index, |
| 1484 | ESP_DECRYPT_ERROR_RX_POST_PKTS, n_left); |
| 1485 | |
| 1486 | vlib_buffer_enqueue_to_next (vm, node, from, nexts, n_left); |
| 1487 | |
| 1488 | return n_left; |
| 1489 | } |
| 1490 | |
Klement Sekera | b8f3544 | 2018-10-29 13:38:19 +0100 | [diff] [blame] | 1491 | VLIB_NODE_FN (esp4_decrypt_node) (vlib_main_t * vm, |
| 1492 | vlib_node_runtime_t * node, |
| 1493 | vlib_frame_t * from_frame) |
Klement Sekera | be5a5dd | 2018-10-09 16:05:48 +0200 | [diff] [blame] | 1494 | { |
Fan Zhang | f539578 | 2020-04-29 14:00:03 +0100 | [diff] [blame] | 1495 | return esp_decrypt_inline (vm, node, from_frame, 0, 0, |
| 1496 | esp_decrypt_async_next.esp4_post_next); |
| 1497 | } |
| 1498 | |
| 1499 | VLIB_NODE_FN (esp4_decrypt_post_node) (vlib_main_t * vm, |
| 1500 | vlib_node_runtime_t * node, |
| 1501 | vlib_frame_t * from_frame) |
| 1502 | { |
| 1503 | return esp_decrypt_post_inline (vm, node, from_frame, 0, 0); |
Neale Ranns | c87b66c | 2019-02-07 07:26:12 -0800 | [diff] [blame] | 1504 | } |
| 1505 | |
| 1506 | VLIB_NODE_FN (esp4_decrypt_tun_node) (vlib_main_t * vm, |
| 1507 | vlib_node_runtime_t * node, |
| 1508 | vlib_frame_t * from_frame) |
| 1509 | { |
Fan Zhang | f539578 | 2020-04-29 14:00:03 +0100 | [diff] [blame] | 1510 | return esp_decrypt_inline (vm, node, from_frame, 0, 1, |
| 1511 | esp_decrypt_async_next.esp4_tun_post_next); |
| 1512 | } |
| 1513 | |
| 1514 | VLIB_NODE_FN (esp4_decrypt_tun_post_node) (vlib_main_t * vm, |
| 1515 | vlib_node_runtime_t * node, |
| 1516 | vlib_frame_t * from_frame) |
| 1517 | { |
| 1518 | return esp_decrypt_post_inline (vm, node, from_frame, 0, 1); |
Neale Ranns | c87b66c | 2019-02-07 07:26:12 -0800 | [diff] [blame] | 1519 | } |
| 1520 | |
| 1521 | VLIB_NODE_FN (esp6_decrypt_node) (vlib_main_t * vm, |
| 1522 | vlib_node_runtime_t * node, |
| 1523 | vlib_frame_t * from_frame) |
| 1524 | { |
Fan Zhang | f539578 | 2020-04-29 14:00:03 +0100 | [diff] [blame] | 1525 | return esp_decrypt_inline (vm, node, from_frame, 1, 0, |
| 1526 | esp_decrypt_async_next.esp6_post_next); |
| 1527 | } |
| 1528 | |
| 1529 | VLIB_NODE_FN (esp6_decrypt_post_node) (vlib_main_t * vm, |
| 1530 | vlib_node_runtime_t * node, |
| 1531 | vlib_frame_t * from_frame) |
| 1532 | { |
| 1533 | return esp_decrypt_post_inline (vm, node, from_frame, 1, 0); |
Neale Ranns | c87b66c | 2019-02-07 07:26:12 -0800 | [diff] [blame] | 1534 | } |
| 1535 | |
| 1536 | VLIB_NODE_FN (esp6_decrypt_tun_node) (vlib_main_t * vm, |
| 1537 | vlib_node_runtime_t * node, |
| 1538 | vlib_frame_t * from_frame) |
| 1539 | { |
Fan Zhang | f539578 | 2020-04-29 14:00:03 +0100 | [diff] [blame] | 1540 | return esp_decrypt_inline (vm, node, from_frame, 1, 1, |
| 1541 | esp_decrypt_async_next.esp6_tun_post_next); |
Klement Sekera | be5a5dd | 2018-10-09 16:05:48 +0200 | [diff] [blame] | 1542 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1543 | |
Fan Zhang | f539578 | 2020-04-29 14:00:03 +0100 | [diff] [blame] | 1544 | VLIB_NODE_FN (esp6_decrypt_tun_post_node) (vlib_main_t * vm, |
| 1545 | vlib_node_runtime_t * node, |
| 1546 | vlib_frame_t * from_frame) |
| 1547 | { |
| 1548 | return esp_decrypt_post_inline (vm, node, from_frame, 1, 1); |
| 1549 | } |
| 1550 | |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 1551 | /* *INDENT-OFF* */ |
Klement Sekera | be5a5dd | 2018-10-09 16:05:48 +0200 | [diff] [blame] | 1552 | VLIB_REGISTER_NODE (esp4_decrypt_node) = { |
Klement Sekera | be5a5dd | 2018-10-09 16:05:48 +0200 | [diff] [blame] | 1553 | .name = "esp4-decrypt", |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1554 | .vector_size = sizeof (u32), |
| 1555 | .format_trace = format_esp_decrypt_trace, |
| 1556 | .type = VLIB_NODE_TYPE_INTERNAL, |
| 1557 | |
Neale Ranns | 93688d7 | 2022-08-09 03:34:51 +0000 | [diff] [blame] | 1558 | .n_errors = ESP_DECRYPT_N_ERROR, |
| 1559 | .error_counters = esp_decrypt_error_counters, |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1560 | |
| 1561 | .n_next_nodes = ESP_DECRYPT_N_NEXT, |
| 1562 | .next_nodes = { |
Neale Ranns | f62a8c0 | 2019-04-02 08:13:33 +0000 | [diff] [blame] | 1563 | [ESP_DECRYPT_NEXT_DROP] = "ip4-drop", |
| 1564 | [ESP_DECRYPT_NEXT_IP4_INPUT] = "ip4-input-no-checksum", |
| 1565 | [ESP_DECRYPT_NEXT_IP6_INPUT] = "ip6-input", |
Neale Ranns | 4a58e49 | 2020-12-21 13:19:10 +0000 | [diff] [blame] | 1566 | [ESP_DECRYPT_NEXT_MPLS_INPUT] = "mpls-drop", |
Neale Ranns | 568acbb | 2019-12-18 05:54:40 +0000 | [diff] [blame] | 1567 | [ESP_DECRYPT_NEXT_L2_INPUT] = "l2-input", |
Neale Ranns | f62a8c0 | 2019-04-02 08:13:33 +0000 | [diff] [blame] | 1568 | [ESP_DECRYPT_NEXT_HANDOFF] = "esp4-decrypt-handoff", |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1569 | }, |
| 1570 | }; |
| 1571 | |
Fan Zhang | f539578 | 2020-04-29 14:00:03 +0100 | [diff] [blame] | 1572 | VLIB_REGISTER_NODE (esp4_decrypt_post_node) = { |
| 1573 | .name = "esp4-decrypt-post", |
| 1574 | .vector_size = sizeof (u32), |
| 1575 | .format_trace = format_esp_decrypt_trace, |
| 1576 | .type = VLIB_NODE_TYPE_INTERNAL, |
| 1577 | |
Neale Ranns | 93688d7 | 2022-08-09 03:34:51 +0000 | [diff] [blame] | 1578 | .n_errors = ESP_DECRYPT_N_ERROR, |
| 1579 | .error_counters = esp_decrypt_error_counters, |
Fan Zhang | f539578 | 2020-04-29 14:00:03 +0100 | [diff] [blame] | 1580 | |
| 1581 | .sibling_of = "esp4-decrypt", |
| 1582 | }; |
| 1583 | |
Klement Sekera | be5a5dd | 2018-10-09 16:05:48 +0200 | [diff] [blame] | 1584 | VLIB_REGISTER_NODE (esp6_decrypt_node) = { |
Klement Sekera | be5a5dd | 2018-10-09 16:05:48 +0200 | [diff] [blame] | 1585 | .name = "esp6-decrypt", |
| 1586 | .vector_size = sizeof (u32), |
| 1587 | .format_trace = format_esp_decrypt_trace, |
| 1588 | .type = VLIB_NODE_TYPE_INTERNAL, |
| 1589 | |
Neale Ranns | 93688d7 | 2022-08-09 03:34:51 +0000 | [diff] [blame] | 1590 | .n_errors = ESP_DECRYPT_N_ERROR, |
| 1591 | .error_counters = esp_decrypt_error_counters, |
Klement Sekera | be5a5dd | 2018-10-09 16:05:48 +0200 | [diff] [blame] | 1592 | |
| 1593 | .n_next_nodes = ESP_DECRYPT_N_NEXT, |
| 1594 | .next_nodes = { |
Neale Ranns | f62a8c0 | 2019-04-02 08:13:33 +0000 | [diff] [blame] | 1595 | [ESP_DECRYPT_NEXT_DROP] = "ip6-drop", |
| 1596 | [ESP_DECRYPT_NEXT_IP4_INPUT] = "ip4-input-no-checksum", |
| 1597 | [ESP_DECRYPT_NEXT_IP6_INPUT] = "ip6-input", |
Neale Ranns | 4a58e49 | 2020-12-21 13:19:10 +0000 | [diff] [blame] | 1598 | [ESP_DECRYPT_NEXT_MPLS_INPUT] = "mpls-drop", |
Neale Ranns | 568acbb | 2019-12-18 05:54:40 +0000 | [diff] [blame] | 1599 | [ESP_DECRYPT_NEXT_L2_INPUT] = "l2-input", |
Neale Ranns | f62a8c0 | 2019-04-02 08:13:33 +0000 | [diff] [blame] | 1600 | [ESP_DECRYPT_NEXT_HANDOFF]= "esp6-decrypt-handoff", |
Klement Sekera | be5a5dd | 2018-10-09 16:05:48 +0200 | [diff] [blame] | 1601 | }, |
| 1602 | }; |
Neale Ranns | c87b66c | 2019-02-07 07:26:12 -0800 | [diff] [blame] | 1603 | |
Fan Zhang | f539578 | 2020-04-29 14:00:03 +0100 | [diff] [blame] | 1604 | VLIB_REGISTER_NODE (esp6_decrypt_post_node) = { |
| 1605 | .name = "esp6-decrypt-post", |
| 1606 | .vector_size = sizeof (u32), |
| 1607 | .format_trace = format_esp_decrypt_trace, |
| 1608 | .type = VLIB_NODE_TYPE_INTERNAL, |
| 1609 | |
Neale Ranns | 93688d7 | 2022-08-09 03:34:51 +0000 | [diff] [blame] | 1610 | .n_errors = ESP_DECRYPT_N_ERROR, |
| 1611 | .error_counters = esp_decrypt_error_counters, |
Fan Zhang | f539578 | 2020-04-29 14:00:03 +0100 | [diff] [blame] | 1612 | |
| 1613 | .sibling_of = "esp6-decrypt", |
| 1614 | }; |
| 1615 | |
Neale Ranns | c87b66c | 2019-02-07 07:26:12 -0800 | [diff] [blame] | 1616 | VLIB_REGISTER_NODE (esp4_decrypt_tun_node) = { |
| 1617 | .name = "esp4-decrypt-tun", |
| 1618 | .vector_size = sizeof (u32), |
| 1619 | .format_trace = format_esp_decrypt_trace, |
| 1620 | .type = VLIB_NODE_TYPE_INTERNAL, |
Neale Ranns | 93688d7 | 2022-08-09 03:34:51 +0000 | [diff] [blame] | 1621 | .n_errors = ESP_DECRYPT_N_ERROR, |
| 1622 | .error_counters = esp_decrypt_error_counters, |
Neale Ranns | f62a8c0 | 2019-04-02 08:13:33 +0000 | [diff] [blame] | 1623 | .n_next_nodes = ESP_DECRYPT_N_NEXT, |
| 1624 | .next_nodes = { |
| 1625 | [ESP_DECRYPT_NEXT_DROP] = "ip4-drop", |
| 1626 | [ESP_DECRYPT_NEXT_IP4_INPUT] = "ip4-input-no-checksum", |
| 1627 | [ESP_DECRYPT_NEXT_IP6_INPUT] = "ip6-input", |
Neale Ranns | 4a58e49 | 2020-12-21 13:19:10 +0000 | [diff] [blame] | 1628 | [ESP_DECRYPT_NEXT_MPLS_INPUT] = "mpls-input", |
Neale Ranns | 568acbb | 2019-12-18 05:54:40 +0000 | [diff] [blame] | 1629 | [ESP_DECRYPT_NEXT_L2_INPUT] = "l2-input", |
Neale Ranns | 4a56f4e | 2019-12-23 04:10:25 +0000 | [diff] [blame] | 1630 | [ESP_DECRYPT_NEXT_HANDOFF] = "esp4-decrypt-tun-handoff", |
Neale Ranns | f62a8c0 | 2019-04-02 08:13:33 +0000 | [diff] [blame] | 1631 | }, |
Neale Ranns | c87b66c | 2019-02-07 07:26:12 -0800 | [diff] [blame] | 1632 | }; |
| 1633 | |
Fan Zhang | f539578 | 2020-04-29 14:00:03 +0100 | [diff] [blame] | 1634 | VLIB_REGISTER_NODE (esp4_decrypt_tun_post_node) = { |
| 1635 | .name = "esp4-decrypt-tun-post", |
| 1636 | .vector_size = sizeof (u32), |
| 1637 | .format_trace = format_esp_decrypt_trace, |
| 1638 | .type = VLIB_NODE_TYPE_INTERNAL, |
| 1639 | |
Neale Ranns | 93688d7 | 2022-08-09 03:34:51 +0000 | [diff] [blame] | 1640 | .n_errors = ESP_DECRYPT_N_ERROR, |
| 1641 | .error_counters = esp_decrypt_error_counters, |
Fan Zhang | f539578 | 2020-04-29 14:00:03 +0100 | [diff] [blame] | 1642 | |
| 1643 | .sibling_of = "esp4-decrypt-tun", |
| 1644 | }; |
| 1645 | |
Neale Ranns | c87b66c | 2019-02-07 07:26:12 -0800 | [diff] [blame] | 1646 | VLIB_REGISTER_NODE (esp6_decrypt_tun_node) = { |
| 1647 | .name = "esp6-decrypt-tun", |
| 1648 | .vector_size = sizeof (u32), |
| 1649 | .format_trace = format_esp_decrypt_trace, |
| 1650 | .type = VLIB_NODE_TYPE_INTERNAL, |
Neale Ranns | 93688d7 | 2022-08-09 03:34:51 +0000 | [diff] [blame] | 1651 | .n_errors = ESP_DECRYPT_N_ERROR, |
| 1652 | .error_counters = esp_decrypt_error_counters, |
Neale Ranns | f62a8c0 | 2019-04-02 08:13:33 +0000 | [diff] [blame] | 1653 | .n_next_nodes = ESP_DECRYPT_N_NEXT, |
| 1654 | .next_nodes = { |
| 1655 | [ESP_DECRYPT_NEXT_DROP] = "ip6-drop", |
| 1656 | [ESP_DECRYPT_NEXT_IP4_INPUT] = "ip4-input-no-checksum", |
| 1657 | [ESP_DECRYPT_NEXT_IP6_INPUT] = "ip6-input", |
Neale Ranns | 4a58e49 | 2020-12-21 13:19:10 +0000 | [diff] [blame] | 1658 | [ESP_DECRYPT_NEXT_MPLS_INPUT] = "mpls-input", |
Neale Ranns | 568acbb | 2019-12-18 05:54:40 +0000 | [diff] [blame] | 1659 | [ESP_DECRYPT_NEXT_L2_INPUT] = "l2-input", |
Neale Ranns | 4a56f4e | 2019-12-23 04:10:25 +0000 | [diff] [blame] | 1660 | [ESP_DECRYPT_NEXT_HANDOFF]= "esp6-decrypt-tun-handoff", |
Neale Ranns | f62a8c0 | 2019-04-02 08:13:33 +0000 | [diff] [blame] | 1661 | }, |
Neale Ranns | c87b66c | 2019-02-07 07:26:12 -0800 | [diff] [blame] | 1662 | }; |
Fan Zhang | f539578 | 2020-04-29 14:00:03 +0100 | [diff] [blame] | 1663 | |
| 1664 | VLIB_REGISTER_NODE (esp6_decrypt_tun_post_node) = { |
| 1665 | .name = "esp6-decrypt-tun-post", |
| 1666 | .vector_size = sizeof (u32), |
| 1667 | .format_trace = format_esp_decrypt_trace, |
| 1668 | .type = VLIB_NODE_TYPE_INTERNAL, |
| 1669 | |
Neale Ranns | 93688d7 | 2022-08-09 03:34:51 +0000 | [diff] [blame] | 1670 | .n_errors = ESP_DECRYPT_N_ERROR, |
| 1671 | .error_counters = esp_decrypt_error_counters, |
Fan Zhang | f539578 | 2020-04-29 14:00:03 +0100 | [diff] [blame] | 1672 | |
| 1673 | .sibling_of = "esp6-decrypt-tun", |
| 1674 | }; |
Klement Sekera | be5a5dd | 2018-10-09 16:05:48 +0200 | [diff] [blame] | 1675 | /* *INDENT-ON* */ |
| 1676 | |
Neale Ranns | 2d49830 | 2021-02-25 08:38:58 +0000 | [diff] [blame] | 1677 | #ifndef CLIB_MARCH_VARIANT |
| 1678 | |
| 1679 | static clib_error_t * |
| 1680 | esp_decrypt_init (vlib_main_t *vm) |
| 1681 | { |
| 1682 | ipsec_main_t *im = &ipsec_main; |
| 1683 | |
| 1684 | im->esp4_dec_fq_index = |
| 1685 | vlib_frame_queue_main_init (esp4_decrypt_node.index, 0); |
| 1686 | im->esp6_dec_fq_index = |
| 1687 | vlib_frame_queue_main_init (esp6_decrypt_node.index, 0); |
| 1688 | im->esp4_dec_tun_fq_index = |
| 1689 | vlib_frame_queue_main_init (esp4_decrypt_tun_node.index, 0); |
| 1690 | im->esp6_dec_tun_fq_index = |
| 1691 | vlib_frame_queue_main_init (esp6_decrypt_tun_node.index, 0); |
| 1692 | |
| 1693 | return 0; |
| 1694 | } |
| 1695 | |
| 1696 | VLIB_INIT_FUNCTION (esp_decrypt_init); |
| 1697 | |
| 1698 | #endif |
| 1699 | |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 1700 | /* |
| 1701 | * fd.io coding-style-patch-verification: ON |
| 1702 | * |
| 1703 | * Local Variables: |
| 1704 | * eval: (c-set-style "gnu") |
| 1705 | * End: |
| 1706 | */ |