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