Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1 | /* |
| 2 | * esp_encrypt.c : IPSec ESP encrypt 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 | */ |
| 17 | |
| 18 | #include <vnet/vnet.h> |
| 19 | #include <vnet/api_errno.h> |
| 20 | #include <vnet/ip/ip.h> |
Arthur de Kerhor | 2f4586d | 2021-09-22 14:53:24 +0200 | [diff] [blame] | 21 | #include <vnet/interface_output.h> |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 22 | |
Damjan Marion | 91f17dc | 2019-03-18 18:59:25 +0100 | [diff] [blame] | 23 | #include <vnet/crypto/crypto.h> |
| 24 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 25 | #include <vnet/ipsec/ipsec.h> |
Neale Ranns | 2828721 | 2019-12-16 00:53:11 +0000 | [diff] [blame] | 26 | #include <vnet/ipsec/ipsec_tun.h> |
Neale Ranns | 93688d7 | 2022-08-09 03:34:51 +0000 | [diff] [blame] | 27 | #include <vnet/ipsec/ipsec.api_enum.h> |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 28 | #include <vnet/ipsec/esp.h> |
Neale Ranns | 041add7 | 2020-01-02 04:06:10 +0000 | [diff] [blame] | 29 | #include <vnet/tunnel/tunnel_dp.h> |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 30 | |
Neale Ranns | 4a58e49 | 2020-12-21 13:19:10 +0000 | [diff] [blame] | 31 | #define foreach_esp_encrypt_next \ |
| 32 | _ (DROP4, "ip4-drop") \ |
| 33 | _ (DROP6, "ip6-drop") \ |
| 34 | _ (DROP_MPLS, "mpls-drop") \ |
| 35 | _ (HANDOFF4, "handoff4") \ |
| 36 | _ (HANDOFF6, "handoff6") \ |
| 37 | _ (HANDOFF_MPLS, "handoff-mpls") \ |
| 38 | _ (INTERFACE_OUTPUT, "interface-output") |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 39 | |
| 40 | #define _(v, s) ESP_ENCRYPT_NEXT_##v, |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 41 | typedef enum |
| 42 | { |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 43 | foreach_esp_encrypt_next |
| 44 | #undef _ |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 45 | ESP_ENCRYPT_N_NEXT, |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 46 | } esp_encrypt_next_t; |
| 47 | |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 48 | typedef struct |
| 49 | { |
Neale Ranns | 8d7c502 | 2019-02-06 01:41:05 -0800 | [diff] [blame] | 50 | u32 sa_index; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 51 | u32 spi; |
| 52 | u32 seq; |
Neale Ranns | 6afaae1 | 2019-07-17 15:07:14 +0000 | [diff] [blame] | 53 | u32 sa_seq_hi; |
Klement Sekera | 4b089f2 | 2018-04-17 18:04:57 +0200 | [diff] [blame] | 54 | u8 udp_encap; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 55 | ipsec_crypto_alg_t crypto_alg; |
| 56 | ipsec_integ_alg_t integ_alg; |
| 57 | } esp_encrypt_trace_t; |
| 58 | |
Fan Zhang | f539578 | 2020-04-29 14:00:03 +0100 | [diff] [blame] | 59 | typedef struct |
| 60 | { |
| 61 | u32 next_index; |
| 62 | } esp_encrypt_post_trace_t; |
| 63 | |
Neale Ranns | 93688d7 | 2022-08-09 03:34:51 +0000 | [diff] [blame] | 64 | typedef vl_counter_esp_encrypt_enum_t esp_encrypt_error_t; |
| 65 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 66 | /* packet trace format function */ |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 67 | static u8 * |
| 68 | format_esp_encrypt_trace (u8 * s, va_list * args) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 69 | { |
| 70 | CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *); |
| 71 | 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] | 72 | esp_encrypt_trace_t *t = va_arg (*args, esp_encrypt_trace_t *); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 73 | |
Guillaume Solignac | 8f818cc | 2019-05-15 12:02:33 +0200 | [diff] [blame] | 74 | s = |
| 75 | format (s, |
Neale Ranns | 6afaae1 | 2019-07-17 15:07:14 +0000 | [diff] [blame] | 76 | "esp: sa-index %d spi %u (0x%08x) seq %u sa-seq-hi %u crypto %U integrity %U%s", |
| 77 | t->sa_index, t->spi, t->spi, t->seq, t->sa_seq_hi, |
| 78 | format_ipsec_crypto_alg, |
Guillaume Solignac | 8f818cc | 2019-05-15 12:02:33 +0200 | [diff] [blame] | 79 | t->crypto_alg, format_ipsec_integ_alg, t->integ_alg, |
| 80 | t->udp_encap ? " udp-encap-enabled" : ""); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 81 | return s; |
| 82 | } |
| 83 | |
Fan Zhang | f539578 | 2020-04-29 14:00:03 +0100 | [diff] [blame] | 84 | static u8 * |
| 85 | format_esp_post_encrypt_trace (u8 * s, va_list * args) |
| 86 | { |
| 87 | CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *); |
| 88 | CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *); |
| 89 | esp_encrypt_post_trace_t *t = va_arg (*args, esp_encrypt_post_trace_t *); |
| 90 | |
| 91 | s = format (s, "esp-post: next node index %u", t->next_index); |
| 92 | return s; |
| 93 | } |
| 94 | |
Damjan Marion | c59b9a2 | 2019-03-19 15:38:40 +0100 | [diff] [blame] | 95 | /* pad packet in input buffer */ |
| 96 | static_always_inline u8 * |
Neale Ranns | f16e9a5 | 2021-02-25 19:09:24 +0000 | [diff] [blame] | 97 | esp_add_footer_and_icv (vlib_main_t *vm, vlib_buffer_t **last, u8 esp_align, |
Fan Zhang | b8cb223 | 2024-03-12 20:39:12 +0000 | [diff] [blame] | 98 | u8 icv_sz, u16 buffer_data_size, uword total_len) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 99 | { |
Damjan Marion | c59b9a2 | 2019-03-19 15:38:40 +0100 | [diff] [blame] | 100 | static const u8 pad_data[ESP_MAX_BLOCK_SIZE] = { |
| 101 | 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, |
Milan Lenco | 7885c74 | 2020-08-20 13:23:09 +0200 | [diff] [blame] | 102 | 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, 0x00, |
Damjan Marion | c59b9a2 | 2019-03-19 15:38:40 +0100 | [diff] [blame] | 103 | }; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 104 | |
Filip Tehlar | efcad1a | 2020-02-04 09:36:04 +0000 | [diff] [blame] | 105 | u16 min_length = total_len + sizeof (esp_footer_t); |
Christian Hopps | fb7e7ed | 2019-11-03 07:02:15 -0500 | [diff] [blame] | 106 | u16 new_length = round_pow2 (min_length, esp_align); |
Damjan Marion | c59b9a2 | 2019-03-19 15:38:40 +0100 | [diff] [blame] | 107 | u8 pad_bytes = new_length - min_length; |
Filip Tehlar | e4e8c6b | 2020-02-13 07:49:30 +0000 | [diff] [blame] | 108 | esp_footer_t *f = (esp_footer_t *) (vlib_buffer_get_current (last[0]) + |
| 109 | last[0]->current_length + pad_bytes); |
Filip Tehlar | efcad1a | 2020-02-04 09:36:04 +0000 | [diff] [blame] | 110 | u16 tail_sz = sizeof (esp_footer_t) + pad_bytes + icv_sz; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 111 | |
Benoît Ganne | 217ba5a | 2021-06-14 17:23:56 +0200 | [diff] [blame] | 112 | if (last[0]->current_data + last[0]->current_length + tail_sz > |
| 113 | buffer_data_size) |
Filip Tehlar | 8cdb1a0 | 2019-11-18 22:21:37 +0000 | [diff] [blame] | 114 | { |
Filip Tehlar | e4e8c6b | 2020-02-13 07:49:30 +0000 | [diff] [blame] | 115 | u32 tmp_bi = 0; |
| 116 | if (vlib_buffer_alloc (vm, &tmp_bi, 1) != 1) |
| 117 | return 0; |
Filip Tehlar | c2c1bfd | 2020-02-13 07:49:30 +0000 | [diff] [blame] | 118 | |
Filip Tehlar | e4e8c6b | 2020-02-13 07:49:30 +0000 | [diff] [blame] | 119 | vlib_buffer_t *tmp = vlib_get_buffer (vm, tmp_bi); |
| 120 | last[0]->next_buffer = tmp_bi; |
| 121 | last[0]->flags |= VLIB_BUFFER_NEXT_PRESENT; |
| 122 | f = (esp_footer_t *) (vlib_buffer_get_current (tmp) + pad_bytes); |
| 123 | tmp->current_length += tail_sz; |
| 124 | last[0] = tmp; |
| 125 | } |
| 126 | else |
| 127 | last[0]->current_length += tail_sz; |
| 128 | |
| 129 | f->pad_length = pad_bytes; |
Damjan Marion | c59b9a2 | 2019-03-19 15:38:40 +0100 | [diff] [blame] | 130 | if (pad_bytes) |
Benoît Ganne | 4505f01 | 2019-12-07 09:14:27 -0700 | [diff] [blame] | 131 | { |
| 132 | ASSERT (pad_bytes <= ESP_MAX_BLOCK_SIZE); |
| 133 | pad_bytes = clib_min (ESP_MAX_BLOCK_SIZE, pad_bytes); |
| 134 | clib_memcpy_fast ((u8 *) f - pad_bytes, pad_data, pad_bytes); |
| 135 | } |
Damjan Marion | c59b9a2 | 2019-03-19 15:38:40 +0100 | [diff] [blame] | 136 | |
Damjan Marion | c59b9a2 | 2019-03-19 15:38:40 +0100 | [diff] [blame] | 137 | return &f->next_header; |
| 138 | } |
| 139 | |
| 140 | static_always_inline void |
| 141 | esp_update_ip4_hdr (ip4_header_t * ip4, u16 len, int is_transport, int is_udp) |
| 142 | { |
Neale Ranns | 1b582b8 | 2019-04-18 19:49:13 -0700 | [diff] [blame] | 143 | ip_csum_t sum; |
| 144 | u16 old_len; |
| 145 | |
| 146 | len = clib_net_to_host_u16 (len); |
| 147 | old_len = ip4->length; |
Damjan Marion | c59b9a2 | 2019-03-19 15:38:40 +0100 | [diff] [blame] | 148 | |
| 149 | if (is_transport) |
| 150 | { |
| 151 | u8 prot = is_udp ? IP_PROTOCOL_UDP : IP_PROTOCOL_IPSEC_ESP; |
Arthur de Kerhor | 2f4586d | 2021-09-22 14:53:24 +0200 | [diff] [blame] | 152 | sum = ip_csum_update (ip4->checksum, ip4->protocol, prot, ip4_header_t, |
| 153 | protocol); |
Neale Ranns | 1b582b8 | 2019-04-18 19:49:13 -0700 | [diff] [blame] | 154 | ip4->protocol = prot; |
Neale Ranns | 1b582b8 | 2019-04-18 19:49:13 -0700 | [diff] [blame] | 155 | sum = ip_csum_update (sum, old_len, len, ip4_header_t, length); |
| 156 | } |
| 157 | else |
| 158 | sum = ip_csum_update (ip4->checksum, old_len, len, ip4_header_t, length); |
| 159 | |
| 160 | ip4->length = len; |
Damjan Marion | c59b9a2 | 2019-03-19 15:38:40 +0100 | [diff] [blame] | 161 | ip4->checksum = ip_csum_fold (sum); |
| 162 | } |
| 163 | |
| 164 | static_always_inline void |
| 165 | esp_fill_udp_hdr (ipsec_sa_t * sa, udp_header_t * udp, u16 len) |
| 166 | { |
| 167 | clib_memcpy_fast (udp, &sa->udp_hdr, sizeof (udp_header_t)); |
| 168 | udp->length = clib_net_to_host_u16 (len); |
| 169 | } |
| 170 | |
| 171 | static_always_inline u8 |
| 172 | ext_hdr_is_pre_esp (u8 nexthdr) |
| 173 | { |
| 174 | #ifdef CLIB_HAVE_VEC128 |
| 175 | static const u8x16 ext_hdr_types = { |
| 176 | IP_PROTOCOL_IP6_HOP_BY_HOP_OPTIONS, |
| 177 | IP_PROTOCOL_IPV6_ROUTE, |
| 178 | IP_PROTOCOL_IPV6_FRAGMENTATION, |
| 179 | }; |
| 180 | |
| 181 | return !u8x16_is_all_zero (ext_hdr_types == u8x16_splat (nexthdr)); |
| 182 | #else |
Piotr Bronowski | 3a6bc6f | 2023-02-23 09:56:49 +0000 | [diff] [blame] | 183 | return (!(nexthdr ^ IP_PROTOCOL_IP6_HOP_BY_HOP_OPTIONS) || |
| 184 | !(nexthdr ^ IP_PROTOCOL_IPV6_ROUTE) || |
| 185 | !(nexthdr ^ IP_PROTOCOL_IPV6_FRAGMENTATION)); |
Damjan Marion | c59b9a2 | 2019-03-19 15:38:40 +0100 | [diff] [blame] | 186 | #endif |
| 187 | } |
| 188 | |
| 189 | static_always_inline u8 |
Neale Ranns | 0295040 | 2019-12-20 00:54:57 +0000 | [diff] [blame] | 190 | esp_get_ip6_hdr_len (ip6_header_t * ip6, ip6_ext_header_t ** ext_hdr) |
Damjan Marion | c59b9a2 | 2019-03-19 15:38:40 +0100 | [diff] [blame] | 191 | { |
| 192 | /* this code assumes that HbH, route and frag headers will be before |
| 193 | others, if that is not the case, they will end up encrypted */ |
Damjan Marion | c59b9a2 | 2019-03-19 15:38:40 +0100 | [diff] [blame] | 194 | u8 len = sizeof (ip6_header_t); |
| 195 | ip6_ext_header_t *p; |
| 196 | |
| 197 | /* if next packet doesn't have ext header */ |
| 198 | if (ext_hdr_is_pre_esp (ip6->protocol) == 0) |
Neale Ranns | 0295040 | 2019-12-20 00:54:57 +0000 | [diff] [blame] | 199 | { |
| 200 | *ext_hdr = NULL; |
| 201 | return len; |
| 202 | } |
Damjan Marion | c59b9a2 | 2019-03-19 15:38:40 +0100 | [diff] [blame] | 203 | |
Ole Troan | 03092c1 | 2021-11-23 15:55:39 +0100 | [diff] [blame] | 204 | p = ip6_next_header (ip6); |
Damjan Marion | c59b9a2 | 2019-03-19 15:38:40 +0100 | [diff] [blame] | 205 | len += ip6_ext_header_len (p); |
Damjan Marion | c59b9a2 | 2019-03-19 15:38:40 +0100 | [diff] [blame] | 206 | while (ext_hdr_is_pre_esp (p->next_hdr)) |
| 207 | { |
| 208 | len += ip6_ext_header_len (p); |
| 209 | p = ip6_ext_next_header (p); |
| 210 | } |
| 211 | |
Neale Ranns | 0295040 | 2019-12-20 00:54:57 +0000 | [diff] [blame] | 212 | *ext_hdr = p; |
Damjan Marion | c59b9a2 | 2019-03-19 15:38:40 +0100 | [diff] [blame] | 213 | return len; |
| 214 | } |
| 215 | |
Benoît Ganne | 02dfd29 | 2022-01-18 15:56:41 +0100 | [diff] [blame] | 216 | /* IPsec IV generation: IVs requirements differ depending of the |
| 217 | * encryption mode: IVs must be unpredictable for AES-CBC whereas it can |
| 218 | * be predictable but should never be reused with the same key material |
| 219 | * for CTR and GCM. |
Benoît Ganne | 5527a78 | 2022-01-18 15:56:41 +0100 | [diff] [blame] | 220 | * To avoid reusing the same IVs between multiple VPP instances and between |
| 221 | * restarts, we use a properly chosen PRNG to generate IVs. To ensure the IV is |
| 222 | * unpredictable for CBC, it is then encrypted using the same key as the |
| 223 | * message. You can refer to NIST SP800-38a and NIST SP800-38d for more |
| 224 | * details. */ |
Benoît Ganne | 02dfd29 | 2022-01-18 15:56:41 +0100 | [diff] [blame] | 225 | static_always_inline void * |
| 226 | esp_generate_iv (ipsec_sa_t *sa, void *payload, int iv_sz) |
| 227 | { |
| 228 | ASSERT (iv_sz >= sizeof (u64)); |
| 229 | u64 *iv = (u64 *) (payload - iv_sz); |
| 230 | clib_memset_u8 (iv, 0, iv_sz); |
Benoît Ganne | 5527a78 | 2022-01-18 15:56:41 +0100 | [diff] [blame] | 231 | *iv = clib_pcg64i_random_r (&sa->iv_prng); |
Benoît Ganne | 02dfd29 | 2022-01-18 15:56:41 +0100 | [diff] [blame] | 232 | return iv; |
| 233 | } |
| 234 | |
Damjan Marion | c59b9a2 | 2019-03-19 15:38:40 +0100 | [diff] [blame] | 235 | static_always_inline void |
Filip Tehlar | efcad1a | 2020-02-04 09:36:04 +0000 | [diff] [blame] | 236 | esp_process_chained_ops (vlib_main_t * vm, vlib_node_runtime_t * node, |
| 237 | vnet_crypto_op_t * ops, vlib_buffer_t * b[], |
Neale Ranns | b1fd80f | 2020-05-12 13:33:56 +0000 | [diff] [blame] | 238 | u16 * nexts, vnet_crypto_op_chunk_t * chunks, |
| 239 | u16 drop_next) |
Filip Tehlar | efcad1a | 2020-02-04 09:36:04 +0000 | [diff] [blame] | 240 | { |
| 241 | u32 n_fail, n_ops = vec_len (ops); |
| 242 | vnet_crypto_op_t *op = ops; |
| 243 | |
| 244 | if (n_ops == 0) |
| 245 | return; |
| 246 | |
| 247 | n_fail = n_ops - vnet_crypto_process_chained_ops (vm, op, chunks, n_ops); |
| 248 | |
| 249 | while (n_fail) |
| 250 | { |
| 251 | ASSERT (op - ops < n_ops); |
| 252 | |
| 253 | if (op->status != VNET_CRYPTO_OP_STATUS_COMPLETED) |
| 254 | { |
| 255 | u32 bi = op->user_data; |
Arthur de Kerhor | ad95b06 | 2022-11-16 19:12:05 +0100 | [diff] [blame] | 256 | esp_encrypt_set_next_index (b[bi], node, vm->thread_index, |
| 257 | ESP_ENCRYPT_ERROR_CRYPTO_ENGINE_ERROR, |
| 258 | bi, nexts, drop_next, |
| 259 | vnet_buffer (b[bi])->ipsec.sad_index); |
Filip Tehlar | efcad1a | 2020-02-04 09:36:04 +0000 | [diff] [blame] | 260 | n_fail--; |
| 261 | } |
| 262 | op++; |
| 263 | } |
| 264 | } |
| 265 | |
| 266 | static_always_inline void |
Damjan Marion | c59b9a2 | 2019-03-19 15:38:40 +0100 | [diff] [blame] | 267 | esp_process_ops (vlib_main_t * vm, vlib_node_runtime_t * node, |
Neale Ranns | b1fd80f | 2020-05-12 13:33:56 +0000 | [diff] [blame] | 268 | vnet_crypto_op_t * ops, vlib_buffer_t * b[], u16 * nexts, |
| 269 | u16 drop_next) |
Damjan Marion | c59b9a2 | 2019-03-19 15:38:40 +0100 | [diff] [blame] | 270 | { |
| 271 | u32 n_fail, n_ops = vec_len (ops); |
| 272 | vnet_crypto_op_t *op = ops; |
| 273 | |
| 274 | if (n_ops == 0) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 275 | return; |
| 276 | |
Damjan Marion | c59b9a2 | 2019-03-19 15:38:40 +0100 | [diff] [blame] | 277 | n_fail = n_ops - vnet_crypto_process_ops (vm, op, n_ops); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 278 | |
Damjan Marion | c59b9a2 | 2019-03-19 15:38:40 +0100 | [diff] [blame] | 279 | while (n_fail) |
| 280 | { |
| 281 | ASSERT (op - ops < n_ops); |
| 282 | |
| 283 | if (op->status != VNET_CRYPTO_OP_STATUS_COMPLETED) |
| 284 | { |
| 285 | u32 bi = op->user_data; |
Arthur de Kerhor | ad95b06 | 2022-11-16 19:12:05 +0100 | [diff] [blame] | 286 | esp_encrypt_set_next_index (b[bi], node, vm->thread_index, |
| 287 | ESP_ENCRYPT_ERROR_CRYPTO_ENGINE_ERROR, |
| 288 | bi, nexts, drop_next, |
| 289 | vnet_buffer (b[bi])->ipsec.sad_index); |
Damjan Marion | c59b9a2 | 2019-03-19 15:38:40 +0100 | [diff] [blame] | 290 | n_fail--; |
| 291 | } |
| 292 | op++; |
| 293 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 294 | } |
| 295 | |
Fan Zhang | f539578 | 2020-04-29 14:00:03 +0100 | [diff] [blame] | 296 | static_always_inline u32 |
| 297 | esp_encrypt_chain_crypto (vlib_main_t * vm, ipsec_per_thread_data_t * ptd, |
| 298 | ipsec_sa_t * sa0, vlib_buffer_t * b, |
| 299 | vlib_buffer_t * lb, u8 icv_sz, u8 * start, |
| 300 | u32 start_len, u16 * n_ch) |
| 301 | { |
| 302 | vnet_crypto_op_chunk_t *ch; |
| 303 | vlib_buffer_t *cb = b; |
| 304 | u32 n_chunks = 1; |
| 305 | u32 total_len; |
| 306 | vec_add2 (ptd->chunks, ch, 1); |
| 307 | total_len = ch->len = start_len; |
| 308 | ch->src = ch->dst = start; |
| 309 | cb = vlib_get_buffer (vm, cb->next_buffer); |
| 310 | |
| 311 | while (1) |
| 312 | { |
| 313 | vec_add2 (ptd->chunks, ch, 1); |
| 314 | n_chunks += 1; |
| 315 | if (lb == cb) |
| 316 | total_len += ch->len = cb->current_length - icv_sz; |
| 317 | else |
| 318 | total_len += ch->len = cb->current_length; |
| 319 | ch->src = ch->dst = vlib_buffer_get_current (cb); |
| 320 | |
| 321 | if (!(cb->flags & VLIB_BUFFER_NEXT_PRESENT)) |
| 322 | break; |
| 323 | |
| 324 | cb = vlib_get_buffer (vm, cb->next_buffer); |
| 325 | } |
| 326 | |
| 327 | if (n_ch) |
| 328 | *n_ch = n_chunks; |
| 329 | |
| 330 | return total_len; |
| 331 | } |
| 332 | |
| 333 | static_always_inline u32 |
| 334 | esp_encrypt_chain_integ (vlib_main_t * vm, ipsec_per_thread_data_t * ptd, |
| 335 | ipsec_sa_t * sa0, vlib_buffer_t * b, |
| 336 | vlib_buffer_t * lb, u8 icv_sz, u8 * start, |
| 337 | u32 start_len, u8 * digest, u16 * n_ch) |
| 338 | { |
| 339 | vnet_crypto_op_chunk_t *ch; |
| 340 | vlib_buffer_t *cb = b; |
| 341 | u32 n_chunks = 1; |
| 342 | u32 total_len; |
| 343 | vec_add2 (ptd->chunks, ch, 1); |
| 344 | total_len = ch->len = start_len; |
| 345 | ch->src = start; |
| 346 | cb = vlib_get_buffer (vm, cb->next_buffer); |
| 347 | |
| 348 | while (1) |
| 349 | { |
| 350 | vec_add2 (ptd->chunks, ch, 1); |
| 351 | n_chunks += 1; |
| 352 | if (lb == cb) |
| 353 | { |
| 354 | total_len += ch->len = cb->current_length - icv_sz; |
| 355 | if (ipsec_sa_is_set_USE_ESN (sa0)) |
| 356 | { |
| 357 | u32 seq_hi = clib_net_to_host_u32 (sa0->seq_hi); |
| 358 | clib_memcpy_fast (digest, &seq_hi, sizeof (seq_hi)); |
| 359 | ch->len += sizeof (seq_hi); |
| 360 | total_len += sizeof (seq_hi); |
| 361 | } |
| 362 | } |
| 363 | else |
| 364 | total_len += ch->len = cb->current_length; |
| 365 | ch->src = vlib_buffer_get_current (cb); |
| 366 | |
| 367 | if (!(cb->flags & VLIB_BUFFER_NEXT_PRESENT)) |
| 368 | break; |
| 369 | |
| 370 | cb = vlib_get_buffer (vm, cb->next_buffer); |
| 371 | } |
| 372 | |
| 373 | if (n_ch) |
| 374 | *n_ch = n_chunks; |
| 375 | |
| 376 | return total_len; |
| 377 | } |
| 378 | |
| 379 | always_inline void |
Benoît Ganne | 490b927 | 2021-01-22 18:03:09 +0100 | [diff] [blame] | 380 | esp_prepare_sync_op (vlib_main_t *vm, ipsec_per_thread_data_t *ptd, |
| 381 | vnet_crypto_op_t **crypto_ops, |
Neale Ranns | 5b89110 | 2021-06-28 13:31:28 +0000 | [diff] [blame] | 382 | vnet_crypto_op_t **integ_ops, ipsec_sa_t *sa0, u32 seq_hi, |
Neale Ranns | f16e9a5 | 2021-02-25 19:09:24 +0000 | [diff] [blame] | 383 | u8 *payload, u16 payload_len, u8 iv_sz, u8 icv_sz, u32 bi, |
| 384 | vlib_buffer_t **b, vlib_buffer_t *lb, u32 hdr_len, |
| 385 | esp_header_t *esp) |
Fan Zhang | f539578 | 2020-04-29 14:00:03 +0100 | [diff] [blame] | 386 | { |
| 387 | if (sa0->crypto_enc_op_id) |
| 388 | { |
| 389 | vnet_crypto_op_t *op; |
| 390 | vec_add2_aligned (crypto_ops[0], op, 1, CLIB_CACHE_LINE_BYTES); |
| 391 | vnet_crypto_op_init (op, sa0->crypto_enc_op_id); |
Benoît Ganne | 02dfd29 | 2022-01-18 15:56:41 +0100 | [diff] [blame] | 392 | u8 *crypto_start = payload; |
| 393 | /* esp_add_footer_and_icv() in esp_encrypt_inline() makes sure we always |
| 394 | * have enough space for ESP header and footer which includes ICV */ |
| 395 | ASSERT (payload_len > icv_sz); |
| 396 | u16 crypto_len = payload_len - icv_sz; |
Fan Zhang | f539578 | 2020-04-29 14:00:03 +0100 | [diff] [blame] | 397 | |
Benoît Ganne | 02dfd29 | 2022-01-18 15:56:41 +0100 | [diff] [blame] | 398 | /* generate the IV in front of the payload */ |
| 399 | void *pkt_iv = esp_generate_iv (sa0, payload, iv_sz); |
| 400 | |
Fan Zhang | f539578 | 2020-04-29 14:00:03 +0100 | [diff] [blame] | 401 | op->key_index = sa0->crypto_key_index; |
Neale Ranns | f16e9a5 | 2021-02-25 19:09:24 +0000 | [diff] [blame] | 402 | op->user_data = bi; |
Fan Zhang | f539578 | 2020-04-29 14:00:03 +0100 | [diff] [blame] | 403 | |
Benoît Ganne | 490b927 | 2021-01-22 18:03:09 +0100 | [diff] [blame] | 404 | if (ipsec_sa_is_set_IS_CTR (sa0)) |
Fan Zhang | f539578 | 2020-04-29 14:00:03 +0100 | [diff] [blame] | 405 | { |
Benoît Ganne | 490b927 | 2021-01-22 18:03:09 +0100 | [diff] [blame] | 406 | /* construct nonce in a scratch space in front of the IP header */ |
| 407 | esp_ctr_nonce_t *nonce = |
Benoît Ganne | 02dfd29 | 2022-01-18 15:56:41 +0100 | [diff] [blame] | 408 | (esp_ctr_nonce_t *) (pkt_iv - hdr_len - sizeof (*nonce)); |
Benoît Ganne | 490b927 | 2021-01-22 18:03:09 +0100 | [diff] [blame] | 409 | if (ipsec_sa_is_set_IS_AEAD (sa0)) |
| 410 | { |
| 411 | /* constuct aad in a scratch space in front of the nonce */ |
| 412 | op->aad = (u8 *) nonce - sizeof (esp_aead_t); |
Neale Ranns | 5b89110 | 2021-06-28 13:31:28 +0000 | [diff] [blame] | 413 | op->aad_len = esp_aad_fill (op->aad, esp, sa0, seq_hi); |
Benoît Ganne | 02dfd29 | 2022-01-18 15:56:41 +0100 | [diff] [blame] | 414 | op->tag = payload + crypto_len; |
Benoît Ganne | 490b927 | 2021-01-22 18:03:09 +0100 | [diff] [blame] | 415 | op->tag_len = 16; |
Benoît Ganne | 84e6658 | 2023-03-10 17:33:03 +0100 | [diff] [blame] | 416 | if (PREDICT_FALSE (ipsec_sa_is_set_IS_NULL_GMAC (sa0))) |
| 417 | { |
| 418 | /* RFC-4543 ENCR_NULL_AUTH_AES_GMAC: IV is part of AAD */ |
| 419 | crypto_start -= iv_sz; |
| 420 | crypto_len += iv_sz; |
| 421 | } |
Benoît Ganne | 490b927 | 2021-01-22 18:03:09 +0100 | [diff] [blame] | 422 | } |
| 423 | else |
| 424 | { |
| 425 | nonce->ctr = clib_host_to_net_u32 (1); |
| 426 | } |
Fan Zhang | f539578 | 2020-04-29 14:00:03 +0100 | [diff] [blame] | 427 | |
Fan Zhang | f539578 | 2020-04-29 14:00:03 +0100 | [diff] [blame] | 428 | nonce->salt = sa0->salt; |
Benoît Ganne | 02dfd29 | 2022-01-18 15:56:41 +0100 | [diff] [blame] | 429 | nonce->iv = *(u64 *) pkt_iv; |
Fan Zhang | f539578 | 2020-04-29 14:00:03 +0100 | [diff] [blame] | 430 | op->iv = (u8 *) nonce; |
| 431 | } |
| 432 | else |
| 433 | { |
Benoît Ganne | 02dfd29 | 2022-01-18 15:56:41 +0100 | [diff] [blame] | 434 | /* construct zero iv in front of the IP header */ |
| 435 | op->iv = pkt_iv - hdr_len - iv_sz; |
| 436 | clib_memset_u8 (op->iv, 0, iv_sz); |
| 437 | /* include iv field in crypto */ |
| 438 | crypto_start -= iv_sz; |
| 439 | crypto_len += iv_sz; |
Fan Zhang | f539578 | 2020-04-29 14:00:03 +0100 | [diff] [blame] | 440 | } |
| 441 | |
Benoît Ganne | 5527a78 | 2022-01-18 15:56:41 +0100 | [diff] [blame] | 442 | if (PREDICT_FALSE (lb != b[0])) |
Fan Zhang | f539578 | 2020-04-29 14:00:03 +0100 | [diff] [blame] | 443 | { |
| 444 | /* is chained */ |
| 445 | op->flags |= VNET_CRYPTO_OP_FLAG_CHAINED_BUFFERS; |
| 446 | op->chunk_index = vec_len (ptd->chunks); |
| 447 | op->tag = vlib_buffer_get_tail (lb) - icv_sz; |
Benoît Ganne | 02dfd29 | 2022-01-18 15:56:41 +0100 | [diff] [blame] | 448 | esp_encrypt_chain_crypto (vm, ptd, sa0, b[0], lb, icv_sz, |
| 449 | crypto_start, crypto_len + icv_sz, |
| 450 | &op->n_chunks); |
| 451 | } |
| 452 | else |
| 453 | { |
| 454 | /* not chained */ |
| 455 | op->src = op->dst = crypto_start; |
| 456 | op->len = crypto_len; |
Fan Zhang | f539578 | 2020-04-29 14:00:03 +0100 | [diff] [blame] | 457 | } |
| 458 | } |
| 459 | |
| 460 | if (sa0->integ_op_id) |
| 461 | { |
| 462 | vnet_crypto_op_t *op; |
| 463 | vec_add2_aligned (integ_ops[0], op, 1, CLIB_CACHE_LINE_BYTES); |
| 464 | vnet_crypto_op_init (op, sa0->integ_op_id); |
| 465 | op->src = payload - iv_sz - sizeof (esp_header_t); |
| 466 | op->digest = payload + payload_len - icv_sz; |
| 467 | op->key_index = sa0->integ_key_index; |
| 468 | op->digest_len = icv_sz; |
| 469 | op->len = payload_len - icv_sz + iv_sz + sizeof (esp_header_t); |
Neale Ranns | f16e9a5 | 2021-02-25 19:09:24 +0000 | [diff] [blame] | 470 | op->user_data = bi; |
Fan Zhang | f539578 | 2020-04-29 14:00:03 +0100 | [diff] [blame] | 471 | |
| 472 | if (lb != b[0]) |
| 473 | { |
| 474 | /* is chained */ |
| 475 | op->flags |= VNET_CRYPTO_OP_FLAG_CHAINED_BUFFERS; |
| 476 | op->chunk_index = vec_len (ptd->chunks); |
| 477 | op->digest = vlib_buffer_get_tail (lb) - icv_sz; |
| 478 | |
| 479 | esp_encrypt_chain_integ (vm, ptd, sa0, b[0], lb, icv_sz, |
| 480 | payload - iv_sz - sizeof (esp_header_t), |
| 481 | payload_len + iv_sz + |
| 482 | sizeof (esp_header_t), op->digest, |
| 483 | &op->n_chunks); |
| 484 | } |
| 485 | else if (ipsec_sa_is_set_USE_ESN (sa0)) |
| 486 | { |
Neale Ranns | 5b89110 | 2021-06-28 13:31:28 +0000 | [diff] [blame] | 487 | u32 tmp = clib_net_to_host_u32 (seq_hi); |
| 488 | clib_memcpy_fast (op->digest, &tmp, sizeof (seq_hi)); |
Fan Zhang | f539578 | 2020-04-29 14:00:03 +0100 | [diff] [blame] | 489 | op->len += sizeof (seq_hi); |
| 490 | } |
| 491 | } |
| 492 | } |
| 493 | |
Neale Ranns | fc81134 | 2021-02-26 10:35:33 +0000 | [diff] [blame] | 494 | static_always_inline void |
| 495 | esp_prepare_async_frame (vlib_main_t *vm, ipsec_per_thread_data_t *ptd, |
| 496 | vnet_crypto_async_frame_t *async_frame, |
| 497 | ipsec_sa_t *sa, vlib_buffer_t *b, esp_header_t *esp, |
| 498 | u8 *payload, u32 payload_len, u8 iv_sz, u8 icv_sz, |
| 499 | u32 bi, u16 next, u32 hdr_len, u16 async_next, |
| 500 | vlib_buffer_t *lb) |
Fan Zhang | f539578 | 2020-04-29 14:00:03 +0100 | [diff] [blame] | 501 | { |
| 502 | esp_post_data_t *post = esp_post_data (b); |
| 503 | u8 *tag, *iv, *aad = 0; |
| 504 | u8 flag = 0; |
Benoît Ganne | 5527a78 | 2022-01-18 15:56:41 +0100 | [diff] [blame] | 505 | const u32 key_index = sa->crypto_key_index; |
Benoît Ganne | 02dfd29 | 2022-01-18 15:56:41 +0100 | [diff] [blame] | 506 | i16 crypto_start_offset, integ_start_offset; |
Fan Zhang | f539578 | 2020-04-29 14:00:03 +0100 | [diff] [blame] | 507 | u16 crypto_total_len, integ_total_len; |
| 508 | |
Fan Zhang | 18f0e31 | 2020-10-19 13:08:34 +0100 | [diff] [blame] | 509 | post->next_index = next; |
Fan Zhang | f539578 | 2020-04-29 14:00:03 +0100 | [diff] [blame] | 510 | |
| 511 | /* crypto */ |
Benoît Ganne | 02dfd29 | 2022-01-18 15:56:41 +0100 | [diff] [blame] | 512 | crypto_start_offset = integ_start_offset = payload - b->data; |
Fan Zhang | f539578 | 2020-04-29 14:00:03 +0100 | [diff] [blame] | 513 | crypto_total_len = integ_total_len = payload_len - icv_sz; |
| 514 | tag = payload + crypto_total_len; |
| 515 | |
Benoît Ganne | 02dfd29 | 2022-01-18 15:56:41 +0100 | [diff] [blame] | 516 | /* generate the IV in front of the payload */ |
| 517 | void *pkt_iv = esp_generate_iv (sa, payload, iv_sz); |
| 518 | |
Benoît Ganne | 490b927 | 2021-01-22 18:03:09 +0100 | [diff] [blame] | 519 | if (ipsec_sa_is_set_IS_CTR (sa)) |
Fan Zhang | f539578 | 2020-04-29 14:00:03 +0100 | [diff] [blame] | 520 | { |
Benoît Ganne | 490b927 | 2021-01-22 18:03:09 +0100 | [diff] [blame] | 521 | /* construct nonce in a scratch space in front of the IP header */ |
Benoît Ganne | 02dfd29 | 2022-01-18 15:56:41 +0100 | [diff] [blame] | 522 | esp_ctr_nonce_t *nonce = |
| 523 | (esp_ctr_nonce_t *) (pkt_iv - hdr_len - sizeof (*nonce)); |
Benoît Ganne | 490b927 | 2021-01-22 18:03:09 +0100 | [diff] [blame] | 524 | if (ipsec_sa_is_set_IS_AEAD (sa)) |
| 525 | { |
| 526 | /* constuct aad in a scratch space in front of the nonce */ |
| 527 | aad = (u8 *) nonce - sizeof (esp_aead_t); |
Neale Ranns | 5b89110 | 2021-06-28 13:31:28 +0000 | [diff] [blame] | 528 | esp_aad_fill (aad, esp, sa, sa->seq_hi); |
Benoît Ganne | 84e6658 | 2023-03-10 17:33:03 +0100 | [diff] [blame] | 529 | if (PREDICT_FALSE (ipsec_sa_is_set_IS_NULL_GMAC (sa))) |
| 530 | { |
| 531 | /* RFC-4543 ENCR_NULL_AUTH_AES_GMAC: IV is part of AAD */ |
| 532 | crypto_start_offset -= iv_sz; |
| 533 | crypto_total_len += iv_sz; |
| 534 | } |
Benoît Ganne | 490b927 | 2021-01-22 18:03:09 +0100 | [diff] [blame] | 535 | } |
| 536 | else |
| 537 | { |
| 538 | nonce->ctr = clib_host_to_net_u32 (1); |
| 539 | } |
| 540 | |
| 541 | nonce->salt = sa->salt; |
Benoît Ganne | 02dfd29 | 2022-01-18 15:56:41 +0100 | [diff] [blame] | 542 | nonce->iv = *(u64 *) pkt_iv; |
Benoît Ganne | 490b927 | 2021-01-22 18:03:09 +0100 | [diff] [blame] | 543 | iv = (u8 *) nonce; |
Fan Zhang | f539578 | 2020-04-29 14:00:03 +0100 | [diff] [blame] | 544 | } |
Benoît Ganne | 490b927 | 2021-01-22 18:03:09 +0100 | [diff] [blame] | 545 | else |
Fan Zhang | f539578 | 2020-04-29 14:00:03 +0100 | [diff] [blame] | 546 | { |
Benoît Ganne | 02dfd29 | 2022-01-18 15:56:41 +0100 | [diff] [blame] | 547 | /* construct zero iv in front of the IP header */ |
| 548 | iv = pkt_iv - hdr_len - iv_sz; |
| 549 | clib_memset_u8 (iv, 0, iv_sz); |
| 550 | /* include iv field in crypto */ |
| 551 | crypto_start_offset -= iv_sz; |
| 552 | crypto_total_len += iv_sz; |
Fan Zhang | f539578 | 2020-04-29 14:00:03 +0100 | [diff] [blame] | 553 | } |
| 554 | |
Benoît Ganne | 490b927 | 2021-01-22 18:03:09 +0100 | [diff] [blame] | 555 | if (lb != b) |
| 556 | { |
| 557 | /* chain */ |
| 558 | flag |= VNET_CRYPTO_OP_FLAG_CHAINED_BUFFERS; |
| 559 | tag = vlib_buffer_get_tail (lb) - icv_sz; |
Benoît Ganne | 02dfd29 | 2022-01-18 15:56:41 +0100 | [diff] [blame] | 560 | crypto_total_len = esp_encrypt_chain_crypto ( |
| 561 | vm, ptd, sa, b, lb, icv_sz, b->data + crypto_start_offset, |
| 562 | crypto_total_len + icv_sz, 0); |
Benoît Ganne | 490b927 | 2021-01-22 18:03:09 +0100 | [diff] [blame] | 563 | } |
| 564 | |
| 565 | if (sa->integ_op_id) |
| 566 | { |
Benoît Ganne | 02dfd29 | 2022-01-18 15:56:41 +0100 | [diff] [blame] | 567 | integ_start_offset -= iv_sz + sizeof (esp_header_t); |
Benoît Ganne | 490b927 | 2021-01-22 18:03:09 +0100 | [diff] [blame] | 568 | integ_total_len += iv_sz + sizeof (esp_header_t); |
| 569 | |
| 570 | if (b != lb) |
| 571 | { |
| 572 | integ_total_len = esp_encrypt_chain_integ ( |
| 573 | vm, ptd, sa, b, lb, icv_sz, |
| 574 | payload - iv_sz - sizeof (esp_header_t), |
| 575 | payload_len + iv_sz + sizeof (esp_header_t), tag, 0); |
| 576 | } |
| 577 | else if (ipsec_sa_is_set_USE_ESN (sa)) |
| 578 | { |
| 579 | u32 seq_hi = clib_net_to_host_u32 (sa->seq_hi); |
| 580 | clib_memcpy_fast (tag, &seq_hi, sizeof (seq_hi)); |
| 581 | integ_total_len += sizeof (seq_hi); |
| 582 | } |
| 583 | } |
| 584 | |
Neale Ranns | fc81134 | 2021-02-26 10:35:33 +0000 | [diff] [blame] | 585 | /* this always succeeds because we know the frame is not full */ |
| 586 | vnet_crypto_async_add_to_frame (vm, async_frame, key_index, crypto_total_len, |
| 587 | integ_total_len - crypto_total_len, |
| 588 | crypto_start_offset, integ_start_offset, bi, |
| 589 | async_next, iv, tag, aad, flag); |
Fan Zhang | f539578 | 2020-04-29 14:00:03 +0100 | [diff] [blame] | 590 | } |
| 591 | |
Klement Sekera | be5a5dd | 2018-10-09 16:05:48 +0200 | [diff] [blame] | 592 | always_inline uword |
Neale Ranns | 4a58e49 | 2020-12-21 13:19:10 +0000 | [diff] [blame] | 593 | esp_encrypt_inline (vlib_main_t *vm, vlib_node_runtime_t *node, |
| 594 | vlib_frame_t *frame, vnet_link_t lt, int is_tun, |
Neale Ranns | f16e9a5 | 2021-02-25 19:09:24 +0000 | [diff] [blame] | 595 | u16 async_next_node) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 596 | { |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 597 | ipsec_main_t *im = &ipsec_main; |
Damjan Marion | c59b9a2 | 2019-03-19 15:38:40 +0100 | [diff] [blame] | 598 | ipsec_per_thread_data_t *ptd = vec_elt_at_index (im->ptd, vm->thread_index); |
| 599 | u32 *from = vlib_frame_vector_args (frame); |
| 600 | u32 n_left = frame->n_vectors; |
| 601 | vlib_buffer_t *bufs[VLIB_FRAME_SIZE], **b = bufs; |
Damjan Marion | c59b9a2 | 2019-03-19 15:38:40 +0100 | [diff] [blame] | 602 | u32 thread_index = vm->thread_index; |
| 603 | u16 buffer_data_size = vlib_buffer_get_default_data_size (vm); |
| 604 | u32 current_sa_index = ~0, current_sa_packets = 0; |
| 605 | u32 current_sa_bytes = 0, spi = 0; |
Christian Hopps | fb7e7ed | 2019-11-03 07:02:15 -0500 | [diff] [blame] | 606 | u8 esp_align = 4, iv_sz = 0, icv_sz = 0; |
Damjan Marion | c59b9a2 | 2019-03-19 15:38:40 +0100 | [diff] [blame] | 607 | ipsec_sa_t *sa0 = 0; |
Matthew Smith | ff71939 | 2024-02-12 18:39:21 +0000 | [diff] [blame] | 608 | u8 sa_drop_no_crypto = 0; |
Filip Tehlar | efcad1a | 2020-02-04 09:36:04 +0000 | [diff] [blame] | 609 | vlib_buffer_t *lb; |
| 610 | vnet_crypto_op_t **crypto_ops = &ptd->crypto_ops; |
| 611 | vnet_crypto_op_t **integ_ops = &ptd->integ_ops; |
Neale Ranns | fc81134 | 2021-02-26 10:35:33 +0000 | [diff] [blame] | 612 | vnet_crypto_async_frame_t *async_frames[VNET_CRYPTO_ASYNC_OP_N_IDS]; |
Fan Zhang | f539578 | 2020-04-29 14:00:03 +0100 | [diff] [blame] | 613 | int is_async = im->async_mode; |
Neale Ranns | fc81134 | 2021-02-26 10:35:33 +0000 | [diff] [blame] | 614 | vnet_crypto_async_op_id_t async_op = ~0; |
Neale Ranns | 4a58e49 | 2020-12-21 13:19:10 +0000 | [diff] [blame] | 615 | u16 drop_next = |
| 616 | (lt == VNET_LINK_IP6 ? ESP_ENCRYPT_NEXT_DROP6 : |
| 617 | (lt == VNET_LINK_IP4 ? ESP_ENCRYPT_NEXT_DROP4 : |
| 618 | ESP_ENCRYPT_NEXT_DROP_MPLS)); |
| 619 | u16 handoff_next = (lt == VNET_LINK_IP6 ? |
| 620 | ESP_ENCRYPT_NEXT_HANDOFF6 : |
| 621 | (lt == VNET_LINK_IP4 ? ESP_ENCRYPT_NEXT_HANDOFF4 : |
| 622 | ESP_ENCRYPT_NEXT_HANDOFF_MPLS)); |
Neale Ranns | f16e9a5 | 2021-02-25 19:09:24 +0000 | [diff] [blame] | 623 | vlib_buffer_t *sync_bufs[VLIB_FRAME_SIZE]; |
| 624 | u16 sync_nexts[VLIB_FRAME_SIZE], *sync_next = sync_nexts, n_sync = 0; |
Damjan Marion | dd298e8 | 2022-10-12 16:02:18 +0200 | [diff] [blame] | 625 | u16 n_async = 0; |
| 626 | u16 noop_nexts[VLIB_FRAME_SIZE], n_noop = 0; |
Neale Ranns | f16e9a5 | 2021-02-25 19:09:24 +0000 | [diff] [blame] | 627 | u32 sync_bi[VLIB_FRAME_SIZE]; |
| 628 | u32 noop_bi[VLIB_FRAME_SIZE]; |
| 629 | esp_encrypt_error_t err; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 630 | |
Damjan Marion | c59b9a2 | 2019-03-19 15:38:40 +0100 | [diff] [blame] | 631 | vlib_get_buffers (vm, from, b, n_left); |
Neale Ranns | f16e9a5 | 2021-02-25 19:09:24 +0000 | [diff] [blame] | 632 | |
| 633 | vec_reset_length (ptd->crypto_ops); |
| 634 | vec_reset_length (ptd->integ_ops); |
| 635 | vec_reset_length (ptd->chained_crypto_ops); |
| 636 | vec_reset_length (ptd->chained_integ_ops); |
Neale Ranns | fc81134 | 2021-02-26 10:35:33 +0000 | [diff] [blame] | 637 | vec_reset_length (ptd->async_frames); |
Filip Tehlar | efcad1a | 2020-02-04 09:36:04 +0000 | [diff] [blame] | 638 | vec_reset_length (ptd->chunks); |
Neale Ranns | fc81134 | 2021-02-26 10:35:33 +0000 | [diff] [blame] | 639 | clib_memset (async_frames, 0, sizeof (async_frames)); |
Damjan Marion | c59b9a2 | 2019-03-19 15:38:40 +0100 | [diff] [blame] | 640 | |
| 641 | while (n_left > 0) |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 642 | { |
Zhiyong Yang | 1cff643 | 2019-04-30 05:33:53 -0400 | [diff] [blame] | 643 | u32 sa_index0; |
Damjan Marion | c59b9a2 | 2019-03-19 15:38:40 +0100 | [diff] [blame] | 644 | dpo_id_t *dpo; |
| 645 | esp_header_t *esp; |
| 646 | u8 *payload, *next_hdr_ptr; |
Filip Tehlar | efcad1a | 2020-02-04 09:36:04 +0000 | [diff] [blame] | 647 | u16 payload_len, payload_len_total, n_bufs; |
Neale Ranns | 4ec36c5 | 2020-03-31 09:21:29 -0400 | [diff] [blame] | 648 | u32 hdr_len; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 649 | |
Neale Ranns | f16e9a5 | 2021-02-25 19:09:24 +0000 | [diff] [blame] | 650 | err = ESP_ENCRYPT_ERROR_RX_PKTS; |
| 651 | |
Damjan Marion | c59b9a2 | 2019-03-19 15:38:40 +0100 | [diff] [blame] | 652 | if (n_left > 2) |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 653 | { |
Damjan Marion | c59b9a2 | 2019-03-19 15:38:40 +0100 | [diff] [blame] | 654 | u8 *p; |
| 655 | vlib_prefetch_buffer_header (b[2], LOAD); |
| 656 | p = vlib_buffer_get_current (b[1]); |
Damjan Marion | af7fb04 | 2021-07-15 11:54:41 +0200 | [diff] [blame] | 657 | clib_prefetch_load (p); |
Damjan Marion | c59b9a2 | 2019-03-19 15:38:40 +0100 | [diff] [blame] | 658 | p -= CLIB_CACHE_LINE_BYTES; |
Damjan Marion | af7fb04 | 2021-07-15 11:54:41 +0200 | [diff] [blame] | 659 | clib_prefetch_load (p); |
Neale Ranns | 123b5eb | 2020-10-16 14:03:55 +0000 | [diff] [blame] | 660 | /* speculate that the trailer goes in the first buffer */ |
| 661 | CLIB_PREFETCH (vlib_buffer_get_tail (b[1]), |
| 662 | CLIB_CACHE_LINE_BYTES, LOAD); |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 663 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 664 | |
Arthur de Kerhor | 2f4586d | 2021-09-22 14:53:24 +0200 | [diff] [blame] | 665 | vnet_calc_checksums_inline (vm, b[0], b[0]->flags & VNET_BUFFER_F_IS_IP4, |
| 666 | b[0]->flags & VNET_BUFFER_F_IS_IP6); |
| 667 | vnet_calc_outer_checksums_inline (vm, b[0]); |
| 668 | |
Neale Ranns | 25edf14 | 2019-03-22 08:12:48 +0000 | [diff] [blame] | 669 | if (is_tun) |
| 670 | { |
| 671 | /* we are on a ipsec tunnel's feature arc */ |
Neale Ranns | 2828721 | 2019-12-16 00:53:11 +0000 | [diff] [blame] | 672 | vnet_buffer (b[0])->ipsec.sad_index = |
| 673 | sa_index0 = ipsec_tun_protect_get_sa_out |
| 674 | (vnet_buffer (b[0])->ip.adj_index[VLIB_TX]); |
Neale Ranns | 6fdcc3d | 2021-10-08 07:30:47 +0000 | [diff] [blame] | 675 | |
| 676 | if (PREDICT_FALSE (INDEX_INVALID == sa_index0)) |
| 677 | { |
| 678 | err = ESP_ENCRYPT_ERROR_NO_PROTECTION; |
Arthur de Kerhor | ad95b06 | 2022-11-16 19:12:05 +0100 | [diff] [blame] | 679 | noop_nexts[n_noop] = drop_next; |
| 680 | b[0]->error = node->errors[err]; |
Neale Ranns | 6fdcc3d | 2021-10-08 07:30:47 +0000 | [diff] [blame] | 681 | goto trace; |
| 682 | } |
Neale Ranns | 25edf14 | 2019-03-22 08:12:48 +0000 | [diff] [blame] | 683 | } |
| 684 | else |
| 685 | sa_index0 = vnet_buffer (b[0])->ipsec.sad_index; |
| 686 | |
| 687 | if (sa_index0 != current_sa_index) |
Damjan Marion | c59b9a2 | 2019-03-19 15:38:40 +0100 | [diff] [blame] | 688 | { |
Damjan Marion | 21f265f | 2019-06-05 15:45:50 +0200 | [diff] [blame] | 689 | if (current_sa_packets) |
Arthur de Kerhor | ad95b06 | 2022-11-16 19:12:05 +0100 | [diff] [blame] | 690 | vlib_increment_combined_counter ( |
| 691 | &ipsec_sa_counters, thread_index, current_sa_index, |
| 692 | current_sa_packets, current_sa_bytes); |
Damjan Marion | 21f265f | 2019-06-05 15:45:50 +0200 | [diff] [blame] | 693 | current_sa_packets = current_sa_bytes = 0; |
| 694 | |
Neale Ranns | c5fe57d | 2021-02-25 16:01:28 +0000 | [diff] [blame] | 695 | sa0 = ipsec_sa_get (sa_index0); |
Matthew Smith | dac9e56 | 2023-11-16 02:27:29 +0000 | [diff] [blame] | 696 | current_sa_index = sa_index0; |
Neale Ranns | 123b5eb | 2020-10-16 14:03:55 +0000 | [diff] [blame] | 697 | |
Matthew Smith | ff71939 | 2024-02-12 18:39:21 +0000 | [diff] [blame] | 698 | sa_drop_no_crypto = ((sa0->crypto_alg == IPSEC_CRYPTO_ALG_NONE && |
| 699 | sa0->integ_alg == IPSEC_INTEG_ALG_NONE) && |
| 700 | !ipsec_sa_is_set_NO_ALGO_NO_DROP (sa0)); |
| 701 | |
Arthur de Kerhor | ad95b06 | 2022-11-16 19:12:05 +0100 | [diff] [blame] | 702 | vlib_prefetch_combined_counter (&ipsec_sa_counters, thread_index, |
| 703 | current_sa_index); |
| 704 | |
Neale Ranns | 123b5eb | 2020-10-16 14:03:55 +0000 | [diff] [blame] | 705 | /* fetch the second cacheline ASAP */ |
Damjan Marion | af7fb04 | 2021-07-15 11:54:41 +0200 | [diff] [blame] | 706 | clib_prefetch_load (sa0->cacheline1); |
Neale Ranns | 123b5eb | 2020-10-16 14:03:55 +0000 | [diff] [blame] | 707 | |
Damjan Marion | c59b9a2 | 2019-03-19 15:38:40 +0100 | [diff] [blame] | 708 | spi = clib_net_to_host_u32 (sa0->spi); |
Christian Hopps | fb7e7ed | 2019-11-03 07:02:15 -0500 | [diff] [blame] | 709 | esp_align = sa0->esp_block_align; |
Damjan Marion | 7c22ff7 | 2019-04-04 12:25:44 +0200 | [diff] [blame] | 710 | icv_sz = sa0->integ_icv_size; |
Damjan Marion | c59b9a2 | 2019-03-19 15:38:40 +0100 | [diff] [blame] | 711 | iv_sz = sa0->crypto_iv_size; |
Neale Ranns | f16e9a5 | 2021-02-25 19:09:24 +0000 | [diff] [blame] | 712 | is_async = im->async_mode | ipsec_sa_is_set_IS_ASYNC (sa0); |
Neale Ranns | fc81134 | 2021-02-26 10:35:33 +0000 | [diff] [blame] | 713 | } |
Fan Zhang | f539578 | 2020-04-29 14:00:03 +0100 | [diff] [blame] | 714 | |
Matthew Smith | ff71939 | 2024-02-12 18:39:21 +0000 | [diff] [blame] | 715 | if (PREDICT_FALSE (sa_drop_no_crypto != 0)) |
| 716 | { |
| 717 | err = ESP_ENCRYPT_ERROR_NO_ENCRYPTION; |
| 718 | esp_encrypt_set_next_index (b[0], node, thread_index, err, n_noop, |
| 719 | noop_nexts, drop_next, sa_index0); |
| 720 | goto trace; |
| 721 | } |
| 722 | |
Benoît Ganne | 5527a78 | 2022-01-18 15:56:41 +0100 | [diff] [blame] | 723 | if (PREDICT_FALSE ((u16) ~0 == sa0->thread_index)) |
Neale Ranns | f62a8c0 | 2019-04-02 08:13:33 +0000 | [diff] [blame] | 724 | { |
| 725 | /* this is the first packet to use this SA, claim the SA |
| 726 | * for this thread. this could happen simultaneously on |
| 727 | * another thread */ |
Neale Ranns | 1a52d37 | 2021-02-04 11:33:32 +0000 | [diff] [blame] | 728 | clib_atomic_cmp_and_swap (&sa0->thread_index, ~0, |
Neale Ranns | f62a8c0 | 2019-04-02 08:13:33 +0000 | [diff] [blame] | 729 | ipsec_sa_assign_thread (thread_index)); |
| 730 | } |
| 731 | |
Neale Ranns | 1a52d37 | 2021-02-04 11:33:32 +0000 | [diff] [blame] | 732 | if (PREDICT_FALSE (thread_index != sa0->thread_index)) |
Neale Ranns | f62a8c0 | 2019-04-02 08:13:33 +0000 | [diff] [blame] | 733 | { |
Neale Ranns | aa7d766 | 2021-02-10 08:42:49 +0000 | [diff] [blame] | 734 | vnet_buffer (b[0])->ipsec.thread_index = sa0->thread_index; |
Neale Ranns | f16e9a5 | 2021-02-25 19:09:24 +0000 | [diff] [blame] | 735 | err = ESP_ENCRYPT_ERROR_HANDOFF; |
Arthur de Kerhor | ad95b06 | 2022-11-16 19:12:05 +0100 | [diff] [blame] | 736 | esp_encrypt_set_next_index (b[0], node, thread_index, err, n_noop, |
| 737 | noop_nexts, handoff_next, |
| 738 | current_sa_index); |
Neale Ranns | f62a8c0 | 2019-04-02 08:13:33 +0000 | [diff] [blame] | 739 | goto trace; |
| 740 | } |
| 741 | |
Filip Tehlar | efcad1a | 2020-02-04 09:36:04 +0000 | [diff] [blame] | 742 | lb = b[0]; |
| 743 | n_bufs = vlib_buffer_chain_linearize (vm, b[0]); |
| 744 | if (n_bufs == 0) |
Damjan Marion | c59b9a2 | 2019-03-19 15:38:40 +0100 | [diff] [blame] | 745 | { |
Neale Ranns | f16e9a5 | 2021-02-25 19:09:24 +0000 | [diff] [blame] | 746 | err = ESP_ENCRYPT_ERROR_NO_BUFFERS; |
Arthur de Kerhor | ad95b06 | 2022-11-16 19:12:05 +0100 | [diff] [blame] | 747 | esp_encrypt_set_next_index (b[0], node, thread_index, err, n_noop, |
| 748 | noop_nexts, drop_next, current_sa_index); |
Damjan Marion | c59b9a2 | 2019-03-19 15:38:40 +0100 | [diff] [blame] | 749 | goto trace; |
| 750 | } |
| 751 | |
Filip Tehlar | efcad1a | 2020-02-04 09:36:04 +0000 | [diff] [blame] | 752 | if (n_bufs > 1) |
| 753 | { |
Filip Tehlar | efcad1a | 2020-02-04 09:36:04 +0000 | [diff] [blame] | 754 | /* find last buffer in the chain */ |
| 755 | while (lb->flags & VLIB_BUFFER_NEXT_PRESENT) |
| 756 | lb = vlib_get_buffer (vm, lb->next_buffer); |
| 757 | } |
Filip Tehlar | efcad1a | 2020-02-04 09:36:04 +0000 | [diff] [blame] | 758 | |
Damjan Marion | c59b9a2 | 2019-03-19 15:38:40 +0100 | [diff] [blame] | 759 | if (PREDICT_FALSE (esp_seq_advance (sa0))) |
| 760 | { |
Neale Ranns | f16e9a5 | 2021-02-25 19:09:24 +0000 | [diff] [blame] | 761 | err = ESP_ENCRYPT_ERROR_SEQ_CYCLED; |
Arthur de Kerhor | ad95b06 | 2022-11-16 19:12:05 +0100 | [diff] [blame] | 762 | esp_encrypt_set_next_index (b[0], node, thread_index, err, n_noop, |
| 763 | noop_nexts, drop_next, current_sa_index); |
Damjan Marion | c59b9a2 | 2019-03-19 15:38:40 +0100 | [diff] [blame] | 764 | goto trace; |
| 765 | } |
| 766 | |
| 767 | /* space for IV */ |
| 768 | hdr_len = iv_sz; |
| 769 | |
Damjan Marion | d709cbc | 2019-03-26 13:16:42 +0100 | [diff] [blame] | 770 | if (ipsec_sa_is_set_IS_TUNNEL (sa0)) |
Damjan Marion | c59b9a2 | 2019-03-19 15:38:40 +0100 | [diff] [blame] | 771 | { |
| 772 | payload = vlib_buffer_get_current (b[0]); |
Neale Ranns | f16e9a5 | 2021-02-25 19:09:24 +0000 | [diff] [blame] | 773 | next_hdr_ptr = esp_add_footer_and_icv ( |
Fan Zhang | b8cb223 | 2024-03-12 20:39:12 +0000 | [diff] [blame] | 774 | vm, &lb, esp_align, icv_sz, buffer_data_size, |
Neale Ranns | f16e9a5 | 2021-02-25 19:09:24 +0000 | [diff] [blame] | 775 | vlib_buffer_length_in_chain (vm, b[0])); |
Filip Tehlar | 8cdb1a0 | 2019-11-18 22:21:37 +0000 | [diff] [blame] | 776 | if (!next_hdr_ptr) |
Filip Tehlar | e4e8c6b | 2020-02-13 07:49:30 +0000 | [diff] [blame] | 777 | { |
Neale Ranns | f16e9a5 | 2021-02-25 19:09:24 +0000 | [diff] [blame] | 778 | err = ESP_ENCRYPT_ERROR_NO_BUFFERS; |
Arthur de Kerhor | ad95b06 | 2022-11-16 19:12:05 +0100 | [diff] [blame] | 779 | esp_encrypt_set_next_index (b[0], node, thread_index, err, |
| 780 | n_noop, noop_nexts, drop_next, |
| 781 | current_sa_index); |
Filip Tehlar | e4e8c6b | 2020-02-13 07:49:30 +0000 | [diff] [blame] | 782 | goto trace; |
| 783 | } |
Filip Tehlar | efcad1a | 2020-02-04 09:36:04 +0000 | [diff] [blame] | 784 | b[0]->flags &= ~VLIB_BUFFER_TOTAL_LENGTH_VALID; |
Filip Tehlar | 8cdb1a0 | 2019-11-18 22:21:37 +0000 | [diff] [blame] | 785 | payload_len = b[0]->current_length; |
Filip Tehlar | efcad1a | 2020-02-04 09:36:04 +0000 | [diff] [blame] | 786 | payload_len_total = vlib_buffer_length_in_chain (vm, b[0]); |
Damjan Marion | c59b9a2 | 2019-03-19 15:38:40 +0100 | [diff] [blame] | 787 | |
| 788 | /* ESP header */ |
| 789 | hdr_len += sizeof (*esp); |
| 790 | esp = (esp_header_t *) (payload - hdr_len); |
| 791 | |
| 792 | /* optional UDP header */ |
Damjan Marion | d709cbc | 2019-03-26 13:16:42 +0100 | [diff] [blame] | 793 | if (ipsec_sa_is_set_UDP_ENCAP (sa0)) |
Damjan Marion | c98275f | 2019-03-06 14:05:01 +0100 | [diff] [blame] | 794 | { |
Damjan Marion | c59b9a2 | 2019-03-19 15:38:40 +0100 | [diff] [blame] | 795 | hdr_len += sizeof (udp_header_t); |
| 796 | esp_fill_udp_hdr (sa0, (udp_header_t *) (payload - hdr_len), |
Filip Tehlar | efcad1a | 2020-02-04 09:36:04 +0000 | [diff] [blame] | 797 | payload_len_total + hdr_len); |
Damjan Marion | c59b9a2 | 2019-03-19 15:38:40 +0100 | [diff] [blame] | 798 | } |
| 799 | |
| 800 | /* IP header */ |
Damjan Marion | d709cbc | 2019-03-26 13:16:42 +0100 | [diff] [blame] | 801 | if (ipsec_sa_is_set_IS_TUNNEL_V6 (sa0)) |
Damjan Marion | c59b9a2 | 2019-03-19 15:38:40 +0100 | [diff] [blame] | 802 | { |
| 803 | ip6_header_t *ip6; |
| 804 | u16 len = sizeof (ip6_header_t); |
| 805 | hdr_len += len; |
| 806 | ip6 = (ip6_header_t *) (payload - hdr_len); |
Neale Ranns | 041add7 | 2020-01-02 04:06:10 +0000 | [diff] [blame] | 807 | clib_memcpy_fast (ip6, &sa0->ip6_hdr, sizeof (ip6_header_t)); |
| 808 | |
Neale Ranns | 4a58e49 | 2020-12-21 13:19:10 +0000 | [diff] [blame] | 809 | if (VNET_LINK_IP6 == lt) |
Neale Ranns | 041add7 | 2020-01-02 04:06:10 +0000 | [diff] [blame] | 810 | { |
| 811 | *next_hdr_ptr = IP_PROTOCOL_IPV6; |
| 812 | tunnel_encap_fixup_6o6 (sa0->tunnel_flags, |
| 813 | (const ip6_header_t *) payload, |
| 814 | ip6); |
| 815 | } |
Neale Ranns | 4a58e49 | 2020-12-21 13:19:10 +0000 | [diff] [blame] | 816 | else if (VNET_LINK_IP4 == lt) |
Neale Ranns | 041add7 | 2020-01-02 04:06:10 +0000 | [diff] [blame] | 817 | { |
| 818 | *next_hdr_ptr = IP_PROTOCOL_IP_IN_IP; |
Neale Ranns | a91cb45 | 2021-02-04 11:02:52 +0000 | [diff] [blame] | 819 | tunnel_encap_fixup_4o6 (sa0->tunnel_flags, b[0], |
| 820 | (const ip4_header_t *) payload, ip6); |
Neale Ranns | 041add7 | 2020-01-02 04:06:10 +0000 | [diff] [blame] | 821 | } |
Neale Ranns | 4a58e49 | 2020-12-21 13:19:10 +0000 | [diff] [blame] | 822 | else if (VNET_LINK_MPLS == lt) |
| 823 | { |
| 824 | *next_hdr_ptr = IP_PROTOCOL_MPLS_IN_IP; |
| 825 | tunnel_encap_fixup_mplso6 ( |
Neale Ranns | a91cb45 | 2021-02-04 11:02:52 +0000 | [diff] [blame] | 826 | sa0->tunnel_flags, b[0], |
| 827 | (const mpls_unicast_header_t *) payload, ip6); |
Neale Ranns | 4a58e49 | 2020-12-21 13:19:10 +0000 | [diff] [blame] | 828 | } |
| 829 | else |
| 830 | ASSERT (0); |
| 831 | |
Filip Tehlar | efcad1a | 2020-02-04 09:36:04 +0000 | [diff] [blame] | 832 | len = payload_len_total + hdr_len - len; |
Damjan Marion | c59b9a2 | 2019-03-19 15:38:40 +0100 | [diff] [blame] | 833 | ip6->payload_length = clib_net_to_host_u16 (len); |
Neale Ranns | 45d6d83 | 2021-01-19 13:38:47 +0000 | [diff] [blame] | 834 | b[0]->flags |= VNET_BUFFER_F_LOCALLY_ORIGINATED; |
Damjan Marion | c98275f | 2019-03-06 14:05:01 +0100 | [diff] [blame] | 835 | } |
| 836 | else |
| 837 | { |
Damjan Marion | c59b9a2 | 2019-03-19 15:38:40 +0100 | [diff] [blame] | 838 | ip4_header_t *ip4; |
| 839 | u16 len = sizeof (ip4_header_t); |
| 840 | hdr_len += len; |
| 841 | ip4 = (ip4_header_t *) (payload - hdr_len); |
Neale Ranns | 041add7 | 2020-01-02 04:06:10 +0000 | [diff] [blame] | 842 | clib_memcpy_fast (ip4, &sa0->ip4_hdr, sizeof (ip4_header_t)); |
| 843 | |
Neale Ranns | 4a58e49 | 2020-12-21 13:19:10 +0000 | [diff] [blame] | 844 | if (VNET_LINK_IP6 == lt) |
Neale Ranns | 041add7 | 2020-01-02 04:06:10 +0000 | [diff] [blame] | 845 | { |
| 846 | *next_hdr_ptr = IP_PROTOCOL_IPV6; |
| 847 | tunnel_encap_fixup_6o4_w_chksum (sa0->tunnel_flags, |
| 848 | (const ip6_header_t *) |
| 849 | payload, ip4); |
| 850 | } |
Neale Ranns | 4a58e49 | 2020-12-21 13:19:10 +0000 | [diff] [blame] | 851 | else if (VNET_LINK_IP4 == lt) |
Neale Ranns | 041add7 | 2020-01-02 04:06:10 +0000 | [diff] [blame] | 852 | { |
| 853 | *next_hdr_ptr = IP_PROTOCOL_IP_IN_IP; |
| 854 | tunnel_encap_fixup_4o4_w_chksum (sa0->tunnel_flags, |
| 855 | (const ip4_header_t *) |
| 856 | payload, ip4); |
| 857 | } |
Neale Ranns | 4a58e49 | 2020-12-21 13:19:10 +0000 | [diff] [blame] | 858 | else if (VNET_LINK_MPLS == lt) |
| 859 | { |
| 860 | *next_hdr_ptr = IP_PROTOCOL_MPLS_IN_IP; |
| 861 | tunnel_encap_fixup_mplso4_w_chksum ( |
| 862 | sa0->tunnel_flags, (const mpls_unicast_header_t *) payload, |
| 863 | ip4); |
| 864 | } |
| 865 | else |
| 866 | ASSERT (0); |
| 867 | |
Filip Tehlar | efcad1a | 2020-02-04 09:36:04 +0000 | [diff] [blame] | 868 | len = payload_len_total + hdr_len; |
Damjan Marion | c59b9a2 | 2019-03-19 15:38:40 +0100 | [diff] [blame] | 869 | esp_update_ip4_hdr (ip4, len, /* is_transport */ 0, 0); |
Damjan Marion | c98275f | 2019-03-06 14:05:01 +0100 | [diff] [blame] | 870 | } |
Damjan Marion | c98275f | 2019-03-06 14:05:01 +0100 | [diff] [blame] | 871 | |
Neale Ranns | 72f2a3a | 2019-06-17 15:43:38 +0000 | [diff] [blame] | 872 | dpo = &sa0->dpo; |
Neale Ranns | 25edf14 | 2019-03-22 08:12:48 +0000 | [diff] [blame] | 873 | if (!is_tun) |
| 874 | { |
Neale Ranns | f16e9a5 | 2021-02-25 19:09:24 +0000 | [diff] [blame] | 875 | sync_next[0] = dpo->dpoi_next_node; |
Neale Ranns | 25edf14 | 2019-03-22 08:12:48 +0000 | [diff] [blame] | 876 | vnet_buffer (b[0])->ip.adj_index[VLIB_TX] = dpo->dpoi_index; |
| 877 | } |
Neale Ranns | 4ec36c5 | 2020-03-31 09:21:29 -0400 | [diff] [blame] | 878 | else |
Neale Ranns | f16e9a5 | 2021-02-25 19:09:24 +0000 | [diff] [blame] | 879 | sync_next[0] = ESP_ENCRYPT_NEXT_INTERFACE_OUTPUT; |
Neale Ranns | 9ec846c | 2021-02-09 14:04:02 +0000 | [diff] [blame] | 880 | b[0]->flags |= VNET_BUFFER_F_LOCALLY_ORIGINATED; |
Damjan Marion | c98275f | 2019-03-06 14:05:01 +0100 | [diff] [blame] | 881 | } |
Damjan Marion | c59b9a2 | 2019-03-19 15:38:40 +0100 | [diff] [blame] | 882 | else /* transport mode */ |
Damjan Marion | c98275f | 2019-03-06 14:05:01 +0100 | [diff] [blame] | 883 | { |
Ole Troan | 03092c1 | 2021-11-23 15:55:39 +0100 | [diff] [blame] | 884 | u8 *l2_hdr, l2_len, *ip_hdr; |
| 885 | u16 ip_len; |
Neale Ranns | 0295040 | 2019-12-20 00:54:57 +0000 | [diff] [blame] | 886 | ip6_ext_header_t *ext_hdr; |
Damjan Marion | c59b9a2 | 2019-03-19 15:38:40 +0100 | [diff] [blame] | 887 | udp_header_t *udp = 0; |
Alexander Chernavin | b0d2eda | 2020-03-25 10:56:52 -0400 | [diff] [blame] | 888 | u16 udp_len = 0; |
Damjan Marion | c59b9a2 | 2019-03-19 15:38:40 +0100 | [diff] [blame] | 889 | u8 *old_ip_hdr = vlib_buffer_get_current (b[0]); |
Damjan Marion | c98275f | 2019-03-06 14:05:01 +0100 | [diff] [blame] | 890 | |
Ole Troan | 03092c1 | 2021-11-23 15:55:39 +0100 | [diff] [blame] | 891 | /* |
| 892 | * Get extension header chain length. It might be longer than the |
| 893 | * buffer's pre_data area. |
| 894 | */ |
Neale Ranns | 4a58e49 | 2020-12-21 13:19:10 +0000 | [diff] [blame] | 895 | ip_len = |
| 896 | (VNET_LINK_IP6 == lt ? |
| 897 | esp_get_ip6_hdr_len ((ip6_header_t *) old_ip_hdr, &ext_hdr) : |
| 898 | ip4_header_bytes ((ip4_header_t *) old_ip_hdr)); |
Ole Troan | 03092c1 | 2021-11-23 15:55:39 +0100 | [diff] [blame] | 899 | if ((old_ip_hdr - ip_len) < &b[0]->pre_data[0]) |
| 900 | { |
| 901 | err = ESP_ENCRYPT_ERROR_NO_BUFFERS; |
Arthur de Kerhor | ad95b06 | 2022-11-16 19:12:05 +0100 | [diff] [blame] | 902 | esp_encrypt_set_next_index (b[0], node, thread_index, err, |
| 903 | n_noop, noop_nexts, drop_next, |
| 904 | current_sa_index); |
Ole Troan | 03092c1 | 2021-11-23 15:55:39 +0100 | [diff] [blame] | 905 | goto trace; |
| 906 | } |
Damjan Marion | c98275f | 2019-03-06 14:05:01 +0100 | [diff] [blame] | 907 | |
Damjan Marion | c59b9a2 | 2019-03-19 15:38:40 +0100 | [diff] [blame] | 908 | vlib_buffer_advance (b[0], ip_len); |
| 909 | payload = vlib_buffer_get_current (b[0]); |
Neale Ranns | f16e9a5 | 2021-02-25 19:09:24 +0000 | [diff] [blame] | 910 | next_hdr_ptr = esp_add_footer_and_icv ( |
Fan Zhang | b8cb223 | 2024-03-12 20:39:12 +0000 | [diff] [blame] | 911 | vm, &lb, esp_align, icv_sz, buffer_data_size, |
Neale Ranns | f16e9a5 | 2021-02-25 19:09:24 +0000 | [diff] [blame] | 912 | vlib_buffer_length_in_chain (vm, b[0])); |
Filip Tehlar | 8cdb1a0 | 2019-11-18 22:21:37 +0000 | [diff] [blame] | 913 | if (!next_hdr_ptr) |
Fan Zhang | 18f0e31 | 2020-10-19 13:08:34 +0100 | [diff] [blame] | 914 | { |
Neale Ranns | f16e9a5 | 2021-02-25 19:09:24 +0000 | [diff] [blame] | 915 | err = ESP_ENCRYPT_ERROR_NO_BUFFERS; |
Arthur de Kerhor | ad95b06 | 2022-11-16 19:12:05 +0100 | [diff] [blame] | 916 | esp_encrypt_set_next_index (b[0], node, thread_index, err, |
| 917 | n_noop, noop_nexts, drop_next, |
| 918 | current_sa_index); |
Fan Zhang | 18f0e31 | 2020-10-19 13:08:34 +0100 | [diff] [blame] | 919 | goto trace; |
| 920 | } |
Filip Tehlar | efcad1a | 2020-02-04 09:36:04 +0000 | [diff] [blame] | 921 | |
| 922 | b[0]->flags &= ~VLIB_BUFFER_TOTAL_LENGTH_VALID; |
Filip Tehlar | 8cdb1a0 | 2019-11-18 22:21:37 +0000 | [diff] [blame] | 923 | payload_len = b[0]->current_length; |
Filip Tehlar | efcad1a | 2020-02-04 09:36:04 +0000 | [diff] [blame] | 924 | payload_len_total = vlib_buffer_length_in_chain (vm, b[0]); |
Damjan Marion | c59b9a2 | 2019-03-19 15:38:40 +0100 | [diff] [blame] | 925 | |
| 926 | /* ESP header */ |
| 927 | hdr_len += sizeof (*esp); |
| 928 | esp = (esp_header_t *) (payload - hdr_len); |
| 929 | |
| 930 | /* optional UDP header */ |
Damjan Marion | d709cbc | 2019-03-26 13:16:42 +0100 | [diff] [blame] | 931 | if (ipsec_sa_is_set_UDP_ENCAP (sa0)) |
Damjan Marion | c98275f | 2019-03-06 14:05:01 +0100 | [diff] [blame] | 932 | { |
Damjan Marion | c59b9a2 | 2019-03-19 15:38:40 +0100 | [diff] [blame] | 933 | hdr_len += sizeof (udp_header_t); |
| 934 | udp = (udp_header_t *) (payload - hdr_len); |
Damjan Marion | c98275f | 2019-03-06 14:05:01 +0100 | [diff] [blame] | 935 | } |
| 936 | |
Damjan Marion | c59b9a2 | 2019-03-19 15:38:40 +0100 | [diff] [blame] | 937 | /* IP header */ |
| 938 | hdr_len += ip_len; |
| 939 | ip_hdr = payload - hdr_len; |
| 940 | |
| 941 | /* L2 header */ |
Neale Ranns | c87b66c | 2019-02-07 07:26:12 -0800 | [diff] [blame] | 942 | if (!is_tun) |
| 943 | { |
| 944 | l2_len = vnet_buffer (b[0])->ip.save_rewrite_length; |
| 945 | hdr_len += l2_len; |
| 946 | l2_hdr = payload - hdr_len; |
Damjan Marion | c59b9a2 | 2019-03-19 15:38:40 +0100 | [diff] [blame] | 947 | |
Neale Ranns | c87b66c | 2019-02-07 07:26:12 -0800 | [diff] [blame] | 948 | /* copy l2 and ip header */ |
| 949 | clib_memcpy_le32 (l2_hdr, old_ip_hdr - l2_len, l2_len); |
| 950 | } |
| 951 | else |
| 952 | l2_len = 0; |
| 953 | |
Matthew Smith | 6f1eb48 | 2022-08-09 22:19:38 +0000 | [diff] [blame] | 954 | u16 len; |
| 955 | len = payload_len_total + hdr_len - l2_len; |
| 956 | |
Neale Ranns | 4a58e49 | 2020-12-21 13:19:10 +0000 | [diff] [blame] | 957 | if (VNET_LINK_IP6 == lt) |
Damjan Marion | c59b9a2 | 2019-03-19 15:38:40 +0100 | [diff] [blame] | 958 | { |
Neale Ranns | 0295040 | 2019-12-20 00:54:57 +0000 | [diff] [blame] | 959 | ip6_header_t *ip6 = (ip6_header_t *) (old_ip_hdr); |
| 960 | if (PREDICT_TRUE (NULL == ext_hdr)) |
| 961 | { |
| 962 | *next_hdr_ptr = ip6->protocol; |
Matthew Smith | 6f1eb48 | 2022-08-09 22:19:38 +0000 | [diff] [blame] | 963 | ip6->protocol = |
| 964 | (udp) ? IP_PROTOCOL_UDP : IP_PROTOCOL_IPSEC_ESP; |
Neale Ranns | 0295040 | 2019-12-20 00:54:57 +0000 | [diff] [blame] | 965 | } |
| 966 | else |
| 967 | { |
| 968 | *next_hdr_ptr = ext_hdr->next_hdr; |
Matthew Smith | 6f1eb48 | 2022-08-09 22:19:38 +0000 | [diff] [blame] | 969 | ext_hdr->next_hdr = |
| 970 | (udp) ? IP_PROTOCOL_UDP : IP_PROTOCOL_IPSEC_ESP; |
Neale Ranns | 0295040 | 2019-12-20 00:54:57 +0000 | [diff] [blame] | 971 | } |
Neale Ranns | d207fd7 | 2019-04-18 17:18:12 -0700 | [diff] [blame] | 972 | ip6->payload_length = |
Matthew Smith | 6f1eb48 | 2022-08-09 22:19:38 +0000 | [diff] [blame] | 973 | clib_host_to_net_u16 (len - sizeof (ip6_header_t)); |
Damjan Marion | c59b9a2 | 2019-03-19 15:38:40 +0100 | [diff] [blame] | 974 | } |
Neale Ranns | 4a58e49 | 2020-12-21 13:19:10 +0000 | [diff] [blame] | 975 | else if (VNET_LINK_IP4 == lt) |
Damjan Marion | c98275f | 2019-03-06 14:05:01 +0100 | [diff] [blame] | 976 | { |
Neale Ranns | 0295040 | 2019-12-20 00:54:57 +0000 | [diff] [blame] | 977 | ip4_header_t *ip4 = (ip4_header_t *) (old_ip_hdr); |
Damjan Marion | c59b9a2 | 2019-03-19 15:38:40 +0100 | [diff] [blame] | 978 | *next_hdr_ptr = ip4->protocol; |
Matthew Smith | 6f1eb48 | 2022-08-09 22:19:38 +0000 | [diff] [blame] | 979 | esp_update_ip4_hdr (ip4, len, /* is_transport */ 1, |
| 980 | (udp != NULL)); |
Damjan Marion | c98275f | 2019-03-06 14:05:01 +0100 | [diff] [blame] | 981 | } |
Damjan Marion | c98275f | 2019-03-06 14:05:01 +0100 | [diff] [blame] | 982 | |
Neale Ranns | 0295040 | 2019-12-20 00:54:57 +0000 | [diff] [blame] | 983 | clib_memcpy_le64 (ip_hdr, old_ip_hdr, ip_len); |
| 984 | |
Alexander Chernavin | b0d2eda | 2020-03-25 10:56:52 -0400 | [diff] [blame] | 985 | if (udp) |
| 986 | { |
Matthew Smith | 6f1eb48 | 2022-08-09 22:19:38 +0000 | [diff] [blame] | 987 | udp_len = len - ip_len; |
Alexander Chernavin | b0d2eda | 2020-03-25 10:56:52 -0400 | [diff] [blame] | 988 | esp_fill_udp_hdr (sa0, udp, udp_len); |
| 989 | } |
| 990 | |
Neale Ranns | f16e9a5 | 2021-02-25 19:09:24 +0000 | [diff] [blame] | 991 | sync_next[0] = ESP_ENCRYPT_NEXT_INTERFACE_OUTPUT; |
Damjan Marion | c98275f | 2019-03-06 14:05:01 +0100 | [diff] [blame] | 992 | } |
| 993 | |
PiotrX Kleski | fdca4dd | 2020-05-05 14:14:22 +0200 | [diff] [blame] | 994 | if (lb != b[0]) |
| 995 | { |
| 996 | crypto_ops = &ptd->chained_crypto_ops; |
| 997 | integ_ops = &ptd->chained_integ_ops; |
| 998 | } |
| 999 | else |
| 1000 | { |
| 1001 | crypto_ops = &ptd->crypto_ops; |
| 1002 | integ_ops = &ptd->integ_ops; |
| 1003 | } |
| 1004 | |
Damjan Marion | c59b9a2 | 2019-03-19 15:38:40 +0100 | [diff] [blame] | 1005 | esp->spi = spi; |
| 1006 | esp->seq = clib_net_to_host_u32 (sa0->seq); |
Damjan Marion | c98275f | 2019-03-06 14:05:01 +0100 | [diff] [blame] | 1007 | |
Fan Zhang | f539578 | 2020-04-29 14:00:03 +0100 | [diff] [blame] | 1008 | if (is_async) |
Matthew Smith | 51d56ba | 2021-06-04 09:18:37 -0500 | [diff] [blame] | 1009 | { |
| 1010 | async_op = sa0->crypto_async_enc_op_id; |
| 1011 | |
| 1012 | /* get a frame for this op if we don't yet have one or it's full |
| 1013 | */ |
| 1014 | if (NULL == async_frames[async_op] || |
| 1015 | vnet_crypto_async_frame_is_full (async_frames[async_op])) |
| 1016 | { |
| 1017 | async_frames[async_op] = |
| 1018 | vnet_crypto_async_get_frame (vm, async_op); |
gaoginskx | f441b5d | 2021-06-07 12:07:01 +0100 | [diff] [blame] | 1019 | |
| 1020 | if (PREDICT_FALSE (!async_frames[async_op])) |
| 1021 | { |
| 1022 | err = ESP_ENCRYPT_ERROR_NO_AVAIL_FRAME; |
| 1023 | esp_encrypt_set_next_index (b[0], node, thread_index, err, |
| 1024 | n_noop, noop_nexts, drop_next, |
| 1025 | current_sa_index); |
| 1026 | goto trace; |
| 1027 | } |
| 1028 | |
Matthew Smith | 51d56ba | 2021-06-04 09:18:37 -0500 | [diff] [blame] | 1029 | /* Save the frame to the list we'll submit at the end */ |
| 1030 | vec_add1 (ptd->async_frames, async_frames[async_op]); |
| 1031 | } |
| 1032 | |
| 1033 | esp_prepare_async_frame (vm, ptd, async_frames[async_op], sa0, b[0], |
| 1034 | esp, payload, payload_len, iv_sz, icv_sz, |
| 1035 | from[b - bufs], sync_next[0], hdr_len, |
| 1036 | async_next_node, lb); |
| 1037 | } |
Fan Zhang | f539578 | 2020-04-29 14:00:03 +0100 | [diff] [blame] | 1038 | else |
Neale Ranns | 5b89110 | 2021-06-28 13:31:28 +0000 | [diff] [blame] | 1039 | esp_prepare_sync_op (vm, ptd, crypto_ops, integ_ops, sa0, sa0->seq_hi, |
| 1040 | payload, payload_len, iv_sz, icv_sz, n_sync, b, |
| 1041 | lb, hdr_len, esp); |
Damjan Marion | c98275f | 2019-03-06 14:05:01 +0100 | [diff] [blame] | 1042 | |
Damjan Marion | c59b9a2 | 2019-03-19 15:38:40 +0100 | [diff] [blame] | 1043 | vlib_buffer_advance (b[0], 0LL - hdr_len); |
Damjan Marion | c98275f | 2019-03-06 14:05:01 +0100 | [diff] [blame] | 1044 | |
Damjan Marion | c59b9a2 | 2019-03-19 15:38:40 +0100 | [diff] [blame] | 1045 | current_sa_packets += 1; |
Filip Tehlar | efcad1a | 2020-02-04 09:36:04 +0000 | [diff] [blame] | 1046 | current_sa_bytes += payload_len_total; |
Damjan Marion | c59b9a2 | 2019-03-19 15:38:40 +0100 | [diff] [blame] | 1047 | |
| 1048 | trace: |
| 1049 | if (PREDICT_FALSE (b[0]->flags & VLIB_BUFFER_IS_TRACED)) |
Damjan Marion | c98275f | 2019-03-06 14:05:01 +0100 | [diff] [blame] | 1050 | { |
Damjan Marion | c59b9a2 | 2019-03-19 15:38:40 +0100 | [diff] [blame] | 1051 | esp_encrypt_trace_t *tr = vlib_add_trace (vm, node, b[0], |
| 1052 | sizeof (*tr)); |
Neale Ranns | 6fdcc3d | 2021-10-08 07:30:47 +0000 | [diff] [blame] | 1053 | if (INDEX_INVALID == sa_index0) |
| 1054 | clib_memset_u8 (tr, 0xff, sizeof (*tr)); |
| 1055 | else |
| 1056 | { |
| 1057 | tr->sa_index = sa_index0; |
| 1058 | tr->spi = sa0->spi; |
Neale Ranns | 6fdcc3d | 2021-10-08 07:30:47 +0000 | [diff] [blame] | 1059 | tr->seq = sa0->seq; |
| 1060 | tr->sa_seq_hi = sa0->seq_hi; |
| 1061 | tr->udp_encap = ipsec_sa_is_set_UDP_ENCAP (sa0); |
| 1062 | tr->crypto_alg = sa0->crypto_alg; |
| 1063 | tr->integ_alg = sa0->integ_alg; |
| 1064 | } |
Damjan Marion | c98275f | 2019-03-06 14:05:01 +0100 | [diff] [blame] | 1065 | } |
Neale Ranns | f16e9a5 | 2021-02-25 19:09:24 +0000 | [diff] [blame] | 1066 | |
Damjan Marion | c98275f | 2019-03-06 14:05:01 +0100 | [diff] [blame] | 1067 | /* next */ |
Neale Ranns | f16e9a5 | 2021-02-25 19:09:24 +0000 | [diff] [blame] | 1068 | if (ESP_ENCRYPT_ERROR_RX_PKTS != err) |
| 1069 | { |
| 1070 | noop_bi[n_noop] = from[b - bufs]; |
| 1071 | n_noop++; |
Neale Ranns | f16e9a5 | 2021-02-25 19:09:24 +0000 | [diff] [blame] | 1072 | } |
| 1073 | else if (!is_async) |
| 1074 | { |
| 1075 | sync_bi[n_sync] = from[b - bufs]; |
| 1076 | sync_bufs[n_sync] = b[0]; |
| 1077 | n_sync++; |
| 1078 | sync_next++; |
| 1079 | } |
| 1080 | else |
| 1081 | { |
| 1082 | n_async++; |
Neale Ranns | f16e9a5 | 2021-02-25 19:09:24 +0000 | [diff] [blame] | 1083 | } |
Damjan Marion | c59b9a2 | 2019-03-19 15:38:40 +0100 | [diff] [blame] | 1084 | n_left -= 1; |
Damjan Marion | c59b9a2 | 2019-03-19 15:38:40 +0100 | [diff] [blame] | 1085 | b += 1; |
Damjan Marion | c98275f | 2019-03-06 14:05:01 +0100 | [diff] [blame] | 1086 | } |
| 1087 | |
Neale Ranns | 6fdcc3d | 2021-10-08 07:30:47 +0000 | [diff] [blame] | 1088 | if (INDEX_INVALID != current_sa_index) |
| 1089 | vlib_increment_combined_counter (&ipsec_sa_counters, thread_index, |
| 1090 | current_sa_index, current_sa_packets, |
| 1091 | current_sa_bytes); |
Neale Ranns | f16e9a5 | 2021-02-25 19:09:24 +0000 | [diff] [blame] | 1092 | if (n_sync) |
Fan Zhang | f539578 | 2020-04-29 14:00:03 +0100 | [diff] [blame] | 1093 | { |
Neale Ranns | f16e9a5 | 2021-02-25 19:09:24 +0000 | [diff] [blame] | 1094 | esp_process_ops (vm, node, ptd->crypto_ops, sync_bufs, sync_nexts, |
| 1095 | drop_next); |
| 1096 | esp_process_chained_ops (vm, node, ptd->chained_crypto_ops, sync_bufs, |
| 1097 | sync_nexts, ptd->chunks, drop_next); |
Filip Tehlar | efcad1a | 2020-02-04 09:36:04 +0000 | [diff] [blame] | 1098 | |
Neale Ranns | f16e9a5 | 2021-02-25 19:09:24 +0000 | [diff] [blame] | 1099 | esp_process_ops (vm, node, ptd->integ_ops, sync_bufs, sync_nexts, |
| 1100 | drop_next); |
| 1101 | esp_process_chained_ops (vm, node, ptd->chained_integ_ops, sync_bufs, |
| 1102 | sync_nexts, ptd->chunks, drop_next); |
Neale Ranns | fc81134 | 2021-02-26 10:35:33 +0000 | [diff] [blame] | 1103 | |
Neale Ranns | f16e9a5 | 2021-02-25 19:09:24 +0000 | [diff] [blame] | 1104 | vlib_buffer_enqueue_to_next (vm, node, sync_bi, sync_nexts, n_sync); |
Fan Zhang | f539578 | 2020-04-29 14:00:03 +0100 | [diff] [blame] | 1105 | } |
Neale Ranns | f16e9a5 | 2021-02-25 19:09:24 +0000 | [diff] [blame] | 1106 | if (n_async) |
Fan Zhang | f539578 | 2020-04-29 14:00:03 +0100 | [diff] [blame] | 1107 | { |
Neale Ranns | fc81134 | 2021-02-26 10:35:33 +0000 | [diff] [blame] | 1108 | /* submit all of the open frames */ |
| 1109 | vnet_crypto_async_frame_t **async_frame; |
| 1110 | |
| 1111 | vec_foreach (async_frame, ptd->async_frames) |
Fan Zhang | 18f0e31 | 2020-10-19 13:08:34 +0100 | [diff] [blame] | 1112 | { |
Neale Ranns | fc81134 | 2021-02-26 10:35:33 +0000 | [diff] [blame] | 1113 | if (vnet_crypto_async_submit_open_frame (vm, *async_frame) < 0) |
| 1114 | { |
Neale Ranns | f16e9a5 | 2021-02-25 19:09:24 +0000 | [diff] [blame] | 1115 | n_noop += esp_async_recycle_failed_submit ( |
| 1116 | vm, *async_frame, node, ESP_ENCRYPT_ERROR_CRYPTO_ENGINE_ERROR, |
Arthur de Kerhor | ad95b06 | 2022-11-16 19:12:05 +0100 | [diff] [blame] | 1117 | IPSEC_SA_ERROR_CRYPTO_ENGINE_ERROR, n_noop, noop_bi, |
Xiaoming Jiang | 0c1454c | 2023-05-05 02:28:20 +0000 | [diff] [blame] | 1118 | noop_nexts, drop_next, true); |
Neale Ranns | fc81134 | 2021-02-26 10:35:33 +0000 | [diff] [blame] | 1119 | vnet_crypto_async_reset_frame (*async_frame); |
| 1120 | vnet_crypto_async_free_frame (vm, *async_frame); |
| 1121 | } |
Fan Zhang | 18f0e31 | 2020-10-19 13:08:34 +0100 | [diff] [blame] | 1122 | } |
Fan Zhang | f539578 | 2020-04-29 14:00:03 +0100 | [diff] [blame] | 1123 | } |
Neale Ranns | f16e9a5 | 2021-02-25 19:09:24 +0000 | [diff] [blame] | 1124 | if (n_noop) |
| 1125 | vlib_buffer_enqueue_to_next (vm, node, noop_bi, noop_nexts, n_noop); |
| 1126 | |
| 1127 | vlib_node_increment_counter (vm, node->node_index, ESP_ENCRYPT_ERROR_RX_PKTS, |
| 1128 | frame->n_vectors); |
Damjan Marion | c59b9a2 | 2019-03-19 15:38:40 +0100 | [diff] [blame] | 1129 | |
Damjan Marion | c59b9a2 | 2019-03-19 15:38:40 +0100 | [diff] [blame] | 1130 | return frame->n_vectors; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1131 | } |
| 1132 | |
Fan Zhang | f539578 | 2020-04-29 14:00:03 +0100 | [diff] [blame] | 1133 | always_inline uword |
| 1134 | esp_encrypt_post_inline (vlib_main_t * vm, vlib_node_runtime_t * node, |
| 1135 | vlib_frame_t * frame) |
| 1136 | { |
| 1137 | vlib_buffer_t *bufs[VLIB_FRAME_SIZE], **b = bufs; |
| 1138 | u16 nexts[VLIB_FRAME_SIZE], *next = nexts; |
| 1139 | u32 *from = vlib_frame_vector_args (frame); |
| 1140 | u32 n_left = frame->n_vectors; |
| 1141 | |
| 1142 | vlib_get_buffers (vm, from, b, n_left); |
| 1143 | |
| 1144 | if (n_left >= 4) |
| 1145 | { |
| 1146 | vlib_prefetch_buffer_header (b[0], LOAD); |
| 1147 | vlib_prefetch_buffer_header (b[1], LOAD); |
| 1148 | vlib_prefetch_buffer_header (b[2], LOAD); |
| 1149 | vlib_prefetch_buffer_header (b[3], LOAD); |
| 1150 | } |
| 1151 | |
| 1152 | while (n_left > 8) |
| 1153 | { |
| 1154 | vlib_prefetch_buffer_header (b[4], LOAD); |
| 1155 | vlib_prefetch_buffer_header (b[5], LOAD); |
| 1156 | vlib_prefetch_buffer_header (b[6], LOAD); |
| 1157 | vlib_prefetch_buffer_header (b[7], LOAD); |
| 1158 | |
| 1159 | next[0] = (esp_post_data (b[0]))->next_index; |
| 1160 | next[1] = (esp_post_data (b[1]))->next_index; |
| 1161 | next[2] = (esp_post_data (b[2]))->next_index; |
| 1162 | next[3] = (esp_post_data (b[3]))->next_index; |
| 1163 | |
| 1164 | if (PREDICT_FALSE (node->flags & VLIB_NODE_FLAG_TRACE)) |
| 1165 | { |
| 1166 | if (b[0]->flags & VLIB_BUFFER_IS_TRACED) |
| 1167 | { |
| 1168 | esp_encrypt_post_trace_t *tr = vlib_add_trace (vm, node, b[0], |
| 1169 | sizeof (*tr)); |
| 1170 | tr->next_index = next[0]; |
| 1171 | } |
| 1172 | if (b[1]->flags & VLIB_BUFFER_IS_TRACED) |
| 1173 | { |
| 1174 | esp_encrypt_post_trace_t *tr = vlib_add_trace (vm, node, b[1], |
| 1175 | sizeof (*tr)); |
| 1176 | tr->next_index = next[1]; |
| 1177 | } |
| 1178 | if (b[2]->flags & VLIB_BUFFER_IS_TRACED) |
| 1179 | { |
| 1180 | esp_encrypt_post_trace_t *tr = vlib_add_trace (vm, node, b[2], |
| 1181 | sizeof (*tr)); |
| 1182 | tr->next_index = next[2]; |
| 1183 | } |
| 1184 | if (b[3]->flags & VLIB_BUFFER_IS_TRACED) |
| 1185 | { |
| 1186 | esp_encrypt_post_trace_t *tr = vlib_add_trace (vm, node, b[3], |
| 1187 | sizeof (*tr)); |
| 1188 | tr->next_index = next[3]; |
| 1189 | } |
| 1190 | } |
| 1191 | |
| 1192 | b += 4; |
| 1193 | next += 4; |
| 1194 | n_left -= 4; |
| 1195 | } |
| 1196 | |
| 1197 | while (n_left > 0) |
| 1198 | { |
| 1199 | next[0] = (esp_post_data (b[0]))->next_index; |
| 1200 | if (PREDICT_FALSE (b[0]->flags & VLIB_BUFFER_IS_TRACED)) |
| 1201 | { |
| 1202 | esp_encrypt_post_trace_t *tr = vlib_add_trace (vm, node, b[0], |
| 1203 | sizeof (*tr)); |
| 1204 | tr->next_index = next[0]; |
| 1205 | } |
| 1206 | |
| 1207 | b += 1; |
| 1208 | next += 1; |
| 1209 | n_left -= 1; |
| 1210 | } |
| 1211 | |
| 1212 | vlib_node_increment_counter (vm, node->node_index, |
| 1213 | ESP_ENCRYPT_ERROR_POST_RX_PKTS, |
| 1214 | frame->n_vectors); |
| 1215 | vlib_buffer_enqueue_to_next (vm, node, from, nexts, frame->n_vectors); |
| 1216 | return frame->n_vectors; |
| 1217 | } |
| 1218 | |
Klement Sekera | b8f3544 | 2018-10-29 13:38:19 +0100 | [diff] [blame] | 1219 | VLIB_NODE_FN (esp4_encrypt_node) (vlib_main_t * vm, |
| 1220 | vlib_node_runtime_t * node, |
| 1221 | vlib_frame_t * from_frame) |
Klement Sekera | be5a5dd | 2018-10-09 16:05:48 +0200 | [diff] [blame] | 1222 | { |
Neale Ranns | 4a58e49 | 2020-12-21 13:19:10 +0000 | [diff] [blame] | 1223 | return esp_encrypt_inline (vm, node, from_frame, VNET_LINK_IP4, 0, |
Fan Zhang | f539578 | 2020-04-29 14:00:03 +0100 | [diff] [blame] | 1224 | esp_encrypt_async_next.esp4_post_next); |
Klement Sekera | be5a5dd | 2018-10-09 16:05:48 +0200 | [diff] [blame] | 1225 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1226 | |
Klement Sekera | be5a5dd | 2018-10-09 16:05:48 +0200 | [diff] [blame] | 1227 | VLIB_REGISTER_NODE (esp4_encrypt_node) = { |
Klement Sekera | be5a5dd | 2018-10-09 16:05:48 +0200 | [diff] [blame] | 1228 | .name = "esp4-encrypt", |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1229 | .vector_size = sizeof (u32), |
| 1230 | .format_trace = format_esp_encrypt_trace, |
| 1231 | .type = VLIB_NODE_TYPE_INTERNAL, |
| 1232 | |
Neale Ranns | 93688d7 | 2022-08-09 03:34:51 +0000 | [diff] [blame] | 1233 | .n_errors = ESP_ENCRYPT_N_ERROR, |
| 1234 | .error_counters = esp_encrypt_error_counters, |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1235 | |
| 1236 | .n_next_nodes = ESP_ENCRYPT_N_NEXT, |
Neale Ranns | 4a58e49 | 2020-12-21 13:19:10 +0000 | [diff] [blame] | 1237 | .next_nodes = { [ESP_ENCRYPT_NEXT_DROP4] = "ip4-drop", |
| 1238 | [ESP_ENCRYPT_NEXT_DROP6] = "ip6-drop", |
| 1239 | [ESP_ENCRYPT_NEXT_DROP_MPLS] = "mpls-drop", |
| 1240 | [ESP_ENCRYPT_NEXT_HANDOFF4] = "esp4-encrypt-handoff", |
| 1241 | [ESP_ENCRYPT_NEXT_HANDOFF6] = "esp6-encrypt-handoff", |
| 1242 | [ESP_ENCRYPT_NEXT_HANDOFF_MPLS] = "error-drop", |
| 1243 | [ESP_ENCRYPT_NEXT_INTERFACE_OUTPUT] = "interface-output" }, |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1244 | }; |
| 1245 | |
Fan Zhang | f539578 | 2020-04-29 14:00:03 +0100 | [diff] [blame] | 1246 | VLIB_NODE_FN (esp4_encrypt_post_node) (vlib_main_t * vm, |
| 1247 | vlib_node_runtime_t * node, |
| 1248 | vlib_frame_t * from_frame) |
| 1249 | { |
| 1250 | return esp_encrypt_post_inline (vm, node, from_frame); |
| 1251 | } |
| 1252 | |
Fan Zhang | f539578 | 2020-04-29 14:00:03 +0100 | [diff] [blame] | 1253 | VLIB_REGISTER_NODE (esp4_encrypt_post_node) = { |
| 1254 | .name = "esp4-encrypt-post", |
| 1255 | .vector_size = sizeof (u32), |
| 1256 | .format_trace = format_esp_post_encrypt_trace, |
| 1257 | .type = VLIB_NODE_TYPE_INTERNAL, |
| 1258 | .sibling_of = "esp4-encrypt", |
| 1259 | |
Neale Ranns | 93688d7 | 2022-08-09 03:34:51 +0000 | [diff] [blame] | 1260 | .n_errors = ESP_ENCRYPT_N_ERROR, |
| 1261 | .error_counters = esp_encrypt_error_counters, |
Fan Zhang | f539578 | 2020-04-29 14:00:03 +0100 | [diff] [blame] | 1262 | }; |
Fan Zhang | f539578 | 2020-04-29 14:00:03 +0100 | [diff] [blame] | 1263 | |
Klement Sekera | b8f3544 | 2018-10-29 13:38:19 +0100 | [diff] [blame] | 1264 | VLIB_NODE_FN (esp6_encrypt_node) (vlib_main_t * vm, |
| 1265 | vlib_node_runtime_t * node, |
| 1266 | vlib_frame_t * from_frame) |
Klement Sekera | be5a5dd | 2018-10-09 16:05:48 +0200 | [diff] [blame] | 1267 | { |
Neale Ranns | 4a58e49 | 2020-12-21 13:19:10 +0000 | [diff] [blame] | 1268 | return esp_encrypt_inline (vm, node, from_frame, VNET_LINK_IP6, 0, |
Fan Zhang | f539578 | 2020-04-29 14:00:03 +0100 | [diff] [blame] | 1269 | esp_encrypt_async_next.esp6_post_next); |
Klement Sekera | be5a5dd | 2018-10-09 16:05:48 +0200 | [diff] [blame] | 1270 | } |
| 1271 | |
Klement Sekera | be5a5dd | 2018-10-09 16:05:48 +0200 | [diff] [blame] | 1272 | VLIB_REGISTER_NODE (esp6_encrypt_node) = { |
Klement Sekera | be5a5dd | 2018-10-09 16:05:48 +0200 | [diff] [blame] | 1273 | .name = "esp6-encrypt", |
| 1274 | .vector_size = sizeof (u32), |
| 1275 | .format_trace = format_esp_encrypt_trace, |
| 1276 | .type = VLIB_NODE_TYPE_INTERNAL, |
Neale Ranns | b1fd80f | 2020-05-12 13:33:56 +0000 | [diff] [blame] | 1277 | .sibling_of = "esp4-encrypt", |
Klement Sekera | be5a5dd | 2018-10-09 16:05:48 +0200 | [diff] [blame] | 1278 | |
Neale Ranns | 93688d7 | 2022-08-09 03:34:51 +0000 | [diff] [blame] | 1279 | .n_errors = ESP_ENCRYPT_N_ERROR, |
| 1280 | .error_counters = esp_encrypt_error_counters, |
Klement Sekera | be5a5dd | 2018-10-09 16:05:48 +0200 | [diff] [blame] | 1281 | }; |
Klement Sekera | be5a5dd | 2018-10-09 16:05:48 +0200 | [diff] [blame] | 1282 | |
Fan Zhang | f539578 | 2020-04-29 14:00:03 +0100 | [diff] [blame] | 1283 | VLIB_NODE_FN (esp6_encrypt_post_node) (vlib_main_t * vm, |
| 1284 | vlib_node_runtime_t * node, |
| 1285 | vlib_frame_t * from_frame) |
| 1286 | { |
| 1287 | return esp_encrypt_post_inline (vm, node, from_frame); |
| 1288 | } |
| 1289 | |
Fan Zhang | f539578 | 2020-04-29 14:00:03 +0100 | [diff] [blame] | 1290 | VLIB_REGISTER_NODE (esp6_encrypt_post_node) = { |
| 1291 | .name = "esp6-encrypt-post", |
| 1292 | .vector_size = sizeof (u32), |
| 1293 | .format_trace = format_esp_post_encrypt_trace, |
| 1294 | .type = VLIB_NODE_TYPE_INTERNAL, |
Neale Ranns | b1fd80f | 2020-05-12 13:33:56 +0000 | [diff] [blame] | 1295 | .sibling_of = "esp4-encrypt", |
Fan Zhang | f539578 | 2020-04-29 14:00:03 +0100 | [diff] [blame] | 1296 | |
Neale Ranns | 93688d7 | 2022-08-09 03:34:51 +0000 | [diff] [blame] | 1297 | .n_errors = ESP_ENCRYPT_N_ERROR, |
| 1298 | .error_counters = esp_encrypt_error_counters, |
Fan Zhang | f539578 | 2020-04-29 14:00:03 +0100 | [diff] [blame] | 1299 | }; |
Fan Zhang | f539578 | 2020-04-29 14:00:03 +0100 | [diff] [blame] | 1300 | |
Neale Ranns | 25edf14 | 2019-03-22 08:12:48 +0000 | [diff] [blame] | 1301 | VLIB_NODE_FN (esp4_encrypt_tun_node) (vlib_main_t * vm, |
| 1302 | vlib_node_runtime_t * node, |
| 1303 | vlib_frame_t * from_frame) |
| 1304 | { |
Neale Ranns | 4a58e49 | 2020-12-21 13:19:10 +0000 | [diff] [blame] | 1305 | return esp_encrypt_inline (vm, node, from_frame, VNET_LINK_IP4, 1, |
Fan Zhang | f539578 | 2020-04-29 14:00:03 +0100 | [diff] [blame] | 1306 | esp_encrypt_async_next.esp4_tun_post_next); |
Neale Ranns | 25edf14 | 2019-03-22 08:12:48 +0000 | [diff] [blame] | 1307 | } |
| 1308 | |
Neale Ranns | 25edf14 | 2019-03-22 08:12:48 +0000 | [diff] [blame] | 1309 | VLIB_REGISTER_NODE (esp4_encrypt_tun_node) = { |
| 1310 | .name = "esp4-encrypt-tun", |
| 1311 | .vector_size = sizeof (u32), |
| 1312 | .format_trace = format_esp_encrypt_trace, |
| 1313 | .type = VLIB_NODE_TYPE_INTERNAL, |
| 1314 | |
Neale Ranns | 93688d7 | 2022-08-09 03:34:51 +0000 | [diff] [blame] | 1315 | .n_errors = ESP_ENCRYPT_N_ERROR, |
| 1316 | .error_counters = esp_encrypt_error_counters, |
Neale Ranns | d7603d9 | 2019-03-28 08:56:10 +0000 | [diff] [blame] | 1317 | |
Neale Ranns | f62a8c0 | 2019-04-02 08:13:33 +0000 | [diff] [blame] | 1318 | .n_next_nodes = ESP_ENCRYPT_N_NEXT, |
Neale Ranns | d7603d9 | 2019-03-28 08:56:10 +0000 | [diff] [blame] | 1319 | .next_nodes = { |
Neale Ranns | b1fd80f | 2020-05-12 13:33:56 +0000 | [diff] [blame] | 1320 | [ESP_ENCRYPT_NEXT_DROP4] = "ip4-drop", |
| 1321 | [ESP_ENCRYPT_NEXT_DROP6] = "ip6-drop", |
Neale Ranns | 4a58e49 | 2020-12-21 13:19:10 +0000 | [diff] [blame] | 1322 | [ESP_ENCRYPT_NEXT_DROP_MPLS] = "mpls-drop", |
Neale Ranns | b1fd80f | 2020-05-12 13:33:56 +0000 | [diff] [blame] | 1323 | [ESP_ENCRYPT_NEXT_HANDOFF4] = "esp4-encrypt-tun-handoff", |
Neale Ranns | 4a58e49 | 2020-12-21 13:19:10 +0000 | [diff] [blame] | 1324 | [ESP_ENCRYPT_NEXT_HANDOFF6] = "esp6-encrypt-tun-handoff", |
| 1325 | [ESP_ENCRYPT_NEXT_HANDOFF_MPLS] = "esp-mpls-encrypt-tun-handoff", |
Neale Ranns | 4ec36c5 | 2020-03-31 09:21:29 -0400 | [diff] [blame] | 1326 | [ESP_ENCRYPT_NEXT_INTERFACE_OUTPUT] = "adj-midchain-tx", |
Neale Ranns | d7603d9 | 2019-03-28 08:56:10 +0000 | [diff] [blame] | 1327 | }, |
Neale Ranns | 25edf14 | 2019-03-22 08:12:48 +0000 | [diff] [blame] | 1328 | }; |
| 1329 | |
Fan Zhang | f539578 | 2020-04-29 14:00:03 +0100 | [diff] [blame] | 1330 | VLIB_NODE_FN (esp4_encrypt_tun_post_node) (vlib_main_t * vm, |
| 1331 | vlib_node_runtime_t * node, |
| 1332 | vlib_frame_t * from_frame) |
| 1333 | { |
| 1334 | return esp_encrypt_post_inline (vm, node, from_frame); |
| 1335 | } |
| 1336 | |
Fan Zhang | f539578 | 2020-04-29 14:00:03 +0100 | [diff] [blame] | 1337 | VLIB_REGISTER_NODE (esp4_encrypt_tun_post_node) = { |
| 1338 | .name = "esp4-encrypt-tun-post", |
| 1339 | .vector_size = sizeof (u32), |
| 1340 | .format_trace = format_esp_post_encrypt_trace, |
| 1341 | .type = VLIB_NODE_TYPE_INTERNAL, |
| 1342 | .sibling_of = "esp4-encrypt-tun", |
| 1343 | |
Neale Ranns | 93688d7 | 2022-08-09 03:34:51 +0000 | [diff] [blame] | 1344 | .n_errors = ESP_ENCRYPT_N_ERROR, |
| 1345 | .error_counters = esp_encrypt_error_counters, |
Fan Zhang | f539578 | 2020-04-29 14:00:03 +0100 | [diff] [blame] | 1346 | }; |
Neale Ranns | 25edf14 | 2019-03-22 08:12:48 +0000 | [diff] [blame] | 1347 | |
| 1348 | VLIB_NODE_FN (esp6_encrypt_tun_node) (vlib_main_t * vm, |
| 1349 | vlib_node_runtime_t * node, |
| 1350 | vlib_frame_t * from_frame) |
| 1351 | { |
Neale Ranns | 4a58e49 | 2020-12-21 13:19:10 +0000 | [diff] [blame] | 1352 | return esp_encrypt_inline (vm, node, from_frame, VNET_LINK_IP6, 1, |
Fan Zhang | f539578 | 2020-04-29 14:00:03 +0100 | [diff] [blame] | 1353 | esp_encrypt_async_next.esp6_tun_post_next); |
Neale Ranns | 25edf14 | 2019-03-22 08:12:48 +0000 | [diff] [blame] | 1354 | } |
| 1355 | |
Neale Ranns | 25edf14 | 2019-03-22 08:12:48 +0000 | [diff] [blame] | 1356 | VLIB_REGISTER_NODE (esp6_encrypt_tun_node) = { |
| 1357 | .name = "esp6-encrypt-tun", |
| 1358 | .vector_size = sizeof (u32), |
| 1359 | .format_trace = format_esp_encrypt_trace, |
| 1360 | .type = VLIB_NODE_TYPE_INTERNAL, |
| 1361 | |
Neale Ranns | 93688d7 | 2022-08-09 03:34:51 +0000 | [diff] [blame] | 1362 | .n_errors = ESP_ENCRYPT_N_ERROR, |
| 1363 | .error_counters = esp_encrypt_error_counters, |
Neale Ranns | d7603d9 | 2019-03-28 08:56:10 +0000 | [diff] [blame] | 1364 | |
Neale Ranns | f62a8c0 | 2019-04-02 08:13:33 +0000 | [diff] [blame] | 1365 | .n_next_nodes = ESP_ENCRYPT_N_NEXT, |
Neale Ranns | d7603d9 | 2019-03-28 08:56:10 +0000 | [diff] [blame] | 1366 | .next_nodes = { |
Neale Ranns | b1fd80f | 2020-05-12 13:33:56 +0000 | [diff] [blame] | 1367 | [ESP_ENCRYPT_NEXT_DROP4] = "ip4-drop", |
| 1368 | [ESP_ENCRYPT_NEXT_DROP6] = "ip6-drop", |
Neale Ranns | 4a58e49 | 2020-12-21 13:19:10 +0000 | [diff] [blame] | 1369 | [ESP_ENCRYPT_NEXT_DROP_MPLS] = "mpls-drop", |
| 1370 | [ESP_ENCRYPT_NEXT_HANDOFF4] = "esp4-encrypt-tun-handoff", |
Neale Ranns | b1fd80f | 2020-05-12 13:33:56 +0000 | [diff] [blame] | 1371 | [ESP_ENCRYPT_NEXT_HANDOFF6] = "esp6-encrypt-tun-handoff", |
Neale Ranns | 4a58e49 | 2020-12-21 13:19:10 +0000 | [diff] [blame] | 1372 | [ESP_ENCRYPT_NEXT_HANDOFF_MPLS] = "esp-mpls-encrypt-tun-handoff", |
Neale Ranns | 4ec36c5 | 2020-03-31 09:21:29 -0400 | [diff] [blame] | 1373 | [ESP_ENCRYPT_NEXT_INTERFACE_OUTPUT] = "adj-midchain-tx", |
Neale Ranns | d7603d9 | 2019-03-28 08:56:10 +0000 | [diff] [blame] | 1374 | }, |
Neale Ranns | 25edf14 | 2019-03-22 08:12:48 +0000 | [diff] [blame] | 1375 | }; |
| 1376 | |
Neale Ranns | 25edf14 | 2019-03-22 08:12:48 +0000 | [diff] [blame] | 1377 | |
Fan Zhang | f539578 | 2020-04-29 14:00:03 +0100 | [diff] [blame] | 1378 | VLIB_NODE_FN (esp6_encrypt_tun_post_node) (vlib_main_t * vm, |
| 1379 | vlib_node_runtime_t * node, |
| 1380 | vlib_frame_t * from_frame) |
| 1381 | { |
| 1382 | return esp_encrypt_post_inline (vm, node, from_frame); |
| 1383 | } |
| 1384 | |
Fan Zhang | f539578 | 2020-04-29 14:00:03 +0100 | [diff] [blame] | 1385 | VLIB_REGISTER_NODE (esp6_encrypt_tun_post_node) = { |
| 1386 | .name = "esp6-encrypt-tun-post", |
| 1387 | .vector_size = sizeof (u32), |
| 1388 | .format_trace = format_esp_post_encrypt_trace, |
| 1389 | .type = VLIB_NODE_TYPE_INTERNAL, |
Neale Ranns | 4a58e49 | 2020-12-21 13:19:10 +0000 | [diff] [blame] | 1390 | .sibling_of = "esp-mpls-encrypt-tun", |
Fan Zhang | f539578 | 2020-04-29 14:00:03 +0100 | [diff] [blame] | 1391 | |
Neale Ranns | 93688d7 | 2022-08-09 03:34:51 +0000 | [diff] [blame] | 1392 | .n_errors = ESP_ENCRYPT_N_ERROR, |
| 1393 | .error_counters = esp_encrypt_error_counters, |
Fan Zhang | f539578 | 2020-04-29 14:00:03 +0100 | [diff] [blame] | 1394 | }; |
Fan Zhang | f539578 | 2020-04-29 14:00:03 +0100 | [diff] [blame] | 1395 | |
Neale Ranns | 4a58e49 | 2020-12-21 13:19:10 +0000 | [diff] [blame] | 1396 | VLIB_NODE_FN (esp_mpls_encrypt_tun_node) |
| 1397 | (vlib_main_t *vm, vlib_node_runtime_t *node, vlib_frame_t *from_frame) |
| 1398 | { |
| 1399 | return esp_encrypt_inline (vm, node, from_frame, VNET_LINK_MPLS, 1, |
| 1400 | esp_encrypt_async_next.esp_mpls_tun_post_next); |
| 1401 | } |
| 1402 | |
| 1403 | VLIB_REGISTER_NODE (esp_mpls_encrypt_tun_node) = { |
| 1404 | .name = "esp-mpls-encrypt-tun", |
| 1405 | .vector_size = sizeof (u32), |
| 1406 | .format_trace = format_esp_encrypt_trace, |
| 1407 | .type = VLIB_NODE_TYPE_INTERNAL, |
| 1408 | |
Neale Ranns | 93688d7 | 2022-08-09 03:34:51 +0000 | [diff] [blame] | 1409 | .n_errors = ESP_ENCRYPT_N_ERROR, |
| 1410 | .error_counters = esp_encrypt_error_counters, |
Neale Ranns | 4a58e49 | 2020-12-21 13:19:10 +0000 | [diff] [blame] | 1411 | |
| 1412 | .n_next_nodes = ESP_ENCRYPT_N_NEXT, |
| 1413 | .next_nodes = { |
| 1414 | [ESP_ENCRYPT_NEXT_DROP4] = "ip4-drop", |
| 1415 | [ESP_ENCRYPT_NEXT_DROP6] = "ip6-drop", |
| 1416 | [ESP_ENCRYPT_NEXT_DROP_MPLS] = "mpls-drop", |
| 1417 | [ESP_ENCRYPT_NEXT_HANDOFF4] = "esp4-encrypt-tun-handoff", |
| 1418 | [ESP_ENCRYPT_NEXT_HANDOFF6] = "esp6-encrypt-tun-handoff", |
| 1419 | [ESP_ENCRYPT_NEXT_HANDOFF_MPLS] = "esp-mpls-encrypt-tun-handoff", |
| 1420 | [ESP_ENCRYPT_NEXT_INTERFACE_OUTPUT] = "adj-midchain-tx", |
| 1421 | }, |
| 1422 | }; |
| 1423 | |
| 1424 | VLIB_NODE_FN (esp_mpls_encrypt_tun_post_node) |
| 1425 | (vlib_main_t *vm, vlib_node_runtime_t *node, vlib_frame_t *from_frame) |
| 1426 | { |
| 1427 | return esp_encrypt_post_inline (vm, node, from_frame); |
| 1428 | } |
| 1429 | |
| 1430 | VLIB_REGISTER_NODE (esp_mpls_encrypt_tun_post_node) = { |
| 1431 | .name = "esp-mpls-encrypt-tun-post", |
| 1432 | .vector_size = sizeof (u32), |
| 1433 | .format_trace = format_esp_post_encrypt_trace, |
| 1434 | .type = VLIB_NODE_TYPE_INTERNAL, |
| 1435 | .sibling_of = "esp-mpls-encrypt-tun", |
| 1436 | |
Neale Ranns | 93688d7 | 2022-08-09 03:34:51 +0000 | [diff] [blame] | 1437 | .n_errors = ESP_ENCRYPT_N_ERROR, |
| 1438 | .error_counters = esp_encrypt_error_counters, |
Neale Ranns | 4a58e49 | 2020-12-21 13:19:10 +0000 | [diff] [blame] | 1439 | }; |
| 1440 | |
Neale Ranns | 2d49830 | 2021-02-25 08:38:58 +0000 | [diff] [blame] | 1441 | #ifndef CLIB_MARCH_VARIANT |
| 1442 | |
| 1443 | static clib_error_t * |
| 1444 | esp_encrypt_init (vlib_main_t *vm) |
| 1445 | { |
| 1446 | ipsec_main_t *im = &ipsec_main; |
| 1447 | |
Dau Do | ea92116 | 2024-04-29 03:00:55 +0000 | [diff] [blame] | 1448 | im->esp4_enc_fq_index = vlib_frame_queue_main_init (esp4_encrypt_node.index, |
| 1449 | im->handoff_queue_size); |
| 1450 | im->esp6_enc_fq_index = vlib_frame_queue_main_init (esp6_encrypt_node.index, |
| 1451 | im->handoff_queue_size); |
| 1452 | im->esp4_enc_tun_fq_index = vlib_frame_queue_main_init ( |
| 1453 | esp4_encrypt_tun_node.index, im->handoff_queue_size); |
| 1454 | im->esp6_enc_tun_fq_index = vlib_frame_queue_main_init ( |
| 1455 | esp6_encrypt_tun_node.index, im->handoff_queue_size); |
| 1456 | im->esp_mpls_enc_tun_fq_index = vlib_frame_queue_main_init ( |
| 1457 | esp_mpls_encrypt_tun_node.index, im->handoff_queue_size); |
Neale Ranns | 2d49830 | 2021-02-25 08:38:58 +0000 | [diff] [blame] | 1458 | |
| 1459 | return 0; |
| 1460 | } |
| 1461 | |
| 1462 | VLIB_INIT_FUNCTION (esp_encrypt_init); |
| 1463 | |
| 1464 | #endif |
| 1465 | |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 1466 | /* |
| 1467 | * fd.io coding-style-patch-verification: ON |
| 1468 | * |
| 1469 | * Local Variables: |
| 1470 | * eval: (c-set-style "gnu") |
| 1471 | * End: |
| 1472 | */ |