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 | |
Jeff Shaw | 26574dc | 2024-06-17 16:01:20 -0700 | [diff] [blame] | 592 | /* Per RFC6935 section 5, the UDP checksum must be computed when originating |
| 593 | * an IPv6 UDP packet. The default behavior may be overridden when conditions |
| 594 | * defined by RFC6936 are satisfied. This implementation does not satisfy all |
| 595 | * the conditions so the checksum must be computed. |
| 596 | */ |
| 597 | static_always_inline void |
| 598 | set_ip6_udp_cksum_offload (vlib_buffer_t *b, i16 l3_hdr_offset, |
| 599 | i16 l4_hdr_offset) |
| 600 | { |
| 601 | vnet_buffer (b)->l3_hdr_offset = l3_hdr_offset; |
| 602 | vnet_buffer (b)->l4_hdr_offset = l4_hdr_offset; |
| 603 | vnet_buffer_offload_flags_set (b, VNET_BUFFER_OFFLOAD_F_UDP_CKSUM); |
| 604 | b->flags |= (VNET_BUFFER_F_IS_IP6 | VNET_BUFFER_F_L3_HDR_OFFSET_VALID | |
| 605 | VNET_BUFFER_F_L4_HDR_OFFSET_VALID); |
| 606 | } |
| 607 | |
Klement Sekera | be5a5dd | 2018-10-09 16:05:48 +0200 | [diff] [blame] | 608 | always_inline uword |
Neale Ranns | 4a58e49 | 2020-12-21 13:19:10 +0000 | [diff] [blame] | 609 | esp_encrypt_inline (vlib_main_t *vm, vlib_node_runtime_t *node, |
| 610 | vlib_frame_t *frame, vnet_link_t lt, int is_tun, |
Neale Ranns | f16e9a5 | 2021-02-25 19:09:24 +0000 | [diff] [blame] | 611 | u16 async_next_node) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 612 | { |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 613 | ipsec_main_t *im = &ipsec_main; |
Damjan Marion | c59b9a2 | 2019-03-19 15:38:40 +0100 | [diff] [blame] | 614 | ipsec_per_thread_data_t *ptd = vec_elt_at_index (im->ptd, vm->thread_index); |
| 615 | u32 *from = vlib_frame_vector_args (frame); |
| 616 | u32 n_left = frame->n_vectors; |
| 617 | vlib_buffer_t *bufs[VLIB_FRAME_SIZE], **b = bufs; |
Damjan Marion | c59b9a2 | 2019-03-19 15:38:40 +0100 | [diff] [blame] | 618 | u32 thread_index = vm->thread_index; |
| 619 | u16 buffer_data_size = vlib_buffer_get_default_data_size (vm); |
| 620 | u32 current_sa_index = ~0, current_sa_packets = 0; |
| 621 | u32 current_sa_bytes = 0, spi = 0; |
Christian Hopps | fb7e7ed | 2019-11-03 07:02:15 -0500 | [diff] [blame] | 622 | u8 esp_align = 4, iv_sz = 0, icv_sz = 0; |
Damjan Marion | c59b9a2 | 2019-03-19 15:38:40 +0100 | [diff] [blame] | 623 | ipsec_sa_t *sa0 = 0; |
Matthew Smith | ff71939 | 2024-02-12 18:39:21 +0000 | [diff] [blame] | 624 | u8 sa_drop_no_crypto = 0; |
Filip Tehlar | efcad1a | 2020-02-04 09:36:04 +0000 | [diff] [blame] | 625 | vlib_buffer_t *lb; |
| 626 | vnet_crypto_op_t **crypto_ops = &ptd->crypto_ops; |
| 627 | vnet_crypto_op_t **integ_ops = &ptd->integ_ops; |
Neale Ranns | fc81134 | 2021-02-26 10:35:33 +0000 | [diff] [blame] | 628 | 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] | 629 | int is_async = im->async_mode; |
Neale Ranns | fc81134 | 2021-02-26 10:35:33 +0000 | [diff] [blame] | 630 | vnet_crypto_async_op_id_t async_op = ~0; |
Neale Ranns | 4a58e49 | 2020-12-21 13:19:10 +0000 | [diff] [blame] | 631 | u16 drop_next = |
| 632 | (lt == VNET_LINK_IP6 ? ESP_ENCRYPT_NEXT_DROP6 : |
| 633 | (lt == VNET_LINK_IP4 ? ESP_ENCRYPT_NEXT_DROP4 : |
| 634 | ESP_ENCRYPT_NEXT_DROP_MPLS)); |
| 635 | u16 handoff_next = (lt == VNET_LINK_IP6 ? |
| 636 | ESP_ENCRYPT_NEXT_HANDOFF6 : |
| 637 | (lt == VNET_LINK_IP4 ? ESP_ENCRYPT_NEXT_HANDOFF4 : |
| 638 | ESP_ENCRYPT_NEXT_HANDOFF_MPLS)); |
Neale Ranns | f16e9a5 | 2021-02-25 19:09:24 +0000 | [diff] [blame] | 639 | vlib_buffer_t *sync_bufs[VLIB_FRAME_SIZE]; |
| 640 | 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] | 641 | u16 n_async = 0; |
| 642 | u16 noop_nexts[VLIB_FRAME_SIZE], n_noop = 0; |
Neale Ranns | f16e9a5 | 2021-02-25 19:09:24 +0000 | [diff] [blame] | 643 | u32 sync_bi[VLIB_FRAME_SIZE]; |
| 644 | u32 noop_bi[VLIB_FRAME_SIZE]; |
| 645 | esp_encrypt_error_t err; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 646 | |
Damjan Marion | c59b9a2 | 2019-03-19 15:38:40 +0100 | [diff] [blame] | 647 | vlib_get_buffers (vm, from, b, n_left); |
Neale Ranns | f16e9a5 | 2021-02-25 19:09:24 +0000 | [diff] [blame] | 648 | |
| 649 | vec_reset_length (ptd->crypto_ops); |
| 650 | vec_reset_length (ptd->integ_ops); |
| 651 | vec_reset_length (ptd->chained_crypto_ops); |
| 652 | vec_reset_length (ptd->chained_integ_ops); |
Neale Ranns | fc81134 | 2021-02-26 10:35:33 +0000 | [diff] [blame] | 653 | vec_reset_length (ptd->async_frames); |
Filip Tehlar | efcad1a | 2020-02-04 09:36:04 +0000 | [diff] [blame] | 654 | vec_reset_length (ptd->chunks); |
Neale Ranns | fc81134 | 2021-02-26 10:35:33 +0000 | [diff] [blame] | 655 | clib_memset (async_frames, 0, sizeof (async_frames)); |
Damjan Marion | c59b9a2 | 2019-03-19 15:38:40 +0100 | [diff] [blame] | 656 | |
| 657 | while (n_left > 0) |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 658 | { |
Zhiyong Yang | 1cff643 | 2019-04-30 05:33:53 -0400 | [diff] [blame] | 659 | u32 sa_index0; |
Damjan Marion | c59b9a2 | 2019-03-19 15:38:40 +0100 | [diff] [blame] | 660 | dpo_id_t *dpo; |
| 661 | esp_header_t *esp; |
| 662 | u8 *payload, *next_hdr_ptr; |
Filip Tehlar | efcad1a | 2020-02-04 09:36:04 +0000 | [diff] [blame] | 663 | u16 payload_len, payload_len_total, n_bufs; |
Neale Ranns | 4ec36c5 | 2020-03-31 09:21:29 -0400 | [diff] [blame] | 664 | u32 hdr_len; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 665 | |
Neale Ranns | f16e9a5 | 2021-02-25 19:09:24 +0000 | [diff] [blame] | 666 | err = ESP_ENCRYPT_ERROR_RX_PKTS; |
| 667 | |
Damjan Marion | c59b9a2 | 2019-03-19 15:38:40 +0100 | [diff] [blame] | 668 | if (n_left > 2) |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 669 | { |
Damjan Marion | c59b9a2 | 2019-03-19 15:38:40 +0100 | [diff] [blame] | 670 | u8 *p; |
| 671 | vlib_prefetch_buffer_header (b[2], LOAD); |
| 672 | p = vlib_buffer_get_current (b[1]); |
Damjan Marion | af7fb04 | 2021-07-15 11:54:41 +0200 | [diff] [blame] | 673 | clib_prefetch_load (p); |
Damjan Marion | c59b9a2 | 2019-03-19 15:38:40 +0100 | [diff] [blame] | 674 | p -= CLIB_CACHE_LINE_BYTES; |
Damjan Marion | af7fb04 | 2021-07-15 11:54:41 +0200 | [diff] [blame] | 675 | clib_prefetch_load (p); |
Neale Ranns | 123b5eb | 2020-10-16 14:03:55 +0000 | [diff] [blame] | 676 | /* speculate that the trailer goes in the first buffer */ |
| 677 | CLIB_PREFETCH (vlib_buffer_get_tail (b[1]), |
| 678 | CLIB_CACHE_LINE_BYTES, LOAD); |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 679 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 680 | |
Arthur de Kerhor | 2f4586d | 2021-09-22 14:53:24 +0200 | [diff] [blame] | 681 | vnet_calc_checksums_inline (vm, b[0], b[0]->flags & VNET_BUFFER_F_IS_IP4, |
| 682 | b[0]->flags & VNET_BUFFER_F_IS_IP6); |
| 683 | vnet_calc_outer_checksums_inline (vm, b[0]); |
| 684 | |
Neale Ranns | 25edf14 | 2019-03-22 08:12:48 +0000 | [diff] [blame] | 685 | if (is_tun) |
| 686 | { |
| 687 | /* we are on a ipsec tunnel's feature arc */ |
Neale Ranns | 2828721 | 2019-12-16 00:53:11 +0000 | [diff] [blame] | 688 | vnet_buffer (b[0])->ipsec.sad_index = |
| 689 | sa_index0 = ipsec_tun_protect_get_sa_out |
| 690 | (vnet_buffer (b[0])->ip.adj_index[VLIB_TX]); |
Neale Ranns | 6fdcc3d | 2021-10-08 07:30:47 +0000 | [diff] [blame] | 691 | |
| 692 | if (PREDICT_FALSE (INDEX_INVALID == sa_index0)) |
| 693 | { |
| 694 | err = ESP_ENCRYPT_ERROR_NO_PROTECTION; |
Arthur de Kerhor | ad95b06 | 2022-11-16 19:12:05 +0100 | [diff] [blame] | 695 | noop_nexts[n_noop] = drop_next; |
| 696 | b[0]->error = node->errors[err]; |
Neale Ranns | 6fdcc3d | 2021-10-08 07:30:47 +0000 | [diff] [blame] | 697 | goto trace; |
| 698 | } |
Neale Ranns | 25edf14 | 2019-03-22 08:12:48 +0000 | [diff] [blame] | 699 | } |
| 700 | else |
| 701 | sa_index0 = vnet_buffer (b[0])->ipsec.sad_index; |
| 702 | |
| 703 | if (sa_index0 != current_sa_index) |
Damjan Marion | c59b9a2 | 2019-03-19 15:38:40 +0100 | [diff] [blame] | 704 | { |
Damjan Marion | 21f265f | 2019-06-05 15:45:50 +0200 | [diff] [blame] | 705 | if (current_sa_packets) |
Arthur de Kerhor | ad95b06 | 2022-11-16 19:12:05 +0100 | [diff] [blame] | 706 | vlib_increment_combined_counter ( |
| 707 | &ipsec_sa_counters, thread_index, current_sa_index, |
| 708 | current_sa_packets, current_sa_bytes); |
Damjan Marion | 21f265f | 2019-06-05 15:45:50 +0200 | [diff] [blame] | 709 | current_sa_packets = current_sa_bytes = 0; |
| 710 | |
Neale Ranns | c5fe57d | 2021-02-25 16:01:28 +0000 | [diff] [blame] | 711 | sa0 = ipsec_sa_get (sa_index0); |
Matthew Smith | dac9e56 | 2023-11-16 02:27:29 +0000 | [diff] [blame] | 712 | current_sa_index = sa_index0; |
Neale Ranns | 123b5eb | 2020-10-16 14:03:55 +0000 | [diff] [blame] | 713 | |
Matthew Smith | ff71939 | 2024-02-12 18:39:21 +0000 | [diff] [blame] | 714 | sa_drop_no_crypto = ((sa0->crypto_alg == IPSEC_CRYPTO_ALG_NONE && |
| 715 | sa0->integ_alg == IPSEC_INTEG_ALG_NONE) && |
| 716 | !ipsec_sa_is_set_NO_ALGO_NO_DROP (sa0)); |
| 717 | |
Arthur de Kerhor | ad95b06 | 2022-11-16 19:12:05 +0100 | [diff] [blame] | 718 | vlib_prefetch_combined_counter (&ipsec_sa_counters, thread_index, |
| 719 | current_sa_index); |
| 720 | |
Neale Ranns | 123b5eb | 2020-10-16 14:03:55 +0000 | [diff] [blame] | 721 | /* fetch the second cacheline ASAP */ |
Damjan Marion | af7fb04 | 2021-07-15 11:54:41 +0200 | [diff] [blame] | 722 | clib_prefetch_load (sa0->cacheline1); |
Neale Ranns | 123b5eb | 2020-10-16 14:03:55 +0000 | [diff] [blame] | 723 | |
Damjan Marion | c59b9a2 | 2019-03-19 15:38:40 +0100 | [diff] [blame] | 724 | spi = clib_net_to_host_u32 (sa0->spi); |
Christian Hopps | fb7e7ed | 2019-11-03 07:02:15 -0500 | [diff] [blame] | 725 | esp_align = sa0->esp_block_align; |
Damjan Marion | 7c22ff7 | 2019-04-04 12:25:44 +0200 | [diff] [blame] | 726 | icv_sz = sa0->integ_icv_size; |
Damjan Marion | c59b9a2 | 2019-03-19 15:38:40 +0100 | [diff] [blame] | 727 | iv_sz = sa0->crypto_iv_size; |
Neale Ranns | f16e9a5 | 2021-02-25 19:09:24 +0000 | [diff] [blame] | 728 | is_async = im->async_mode | ipsec_sa_is_set_IS_ASYNC (sa0); |
Neale Ranns | fc81134 | 2021-02-26 10:35:33 +0000 | [diff] [blame] | 729 | } |
Fan Zhang | f539578 | 2020-04-29 14:00:03 +0100 | [diff] [blame] | 730 | |
Matthew Smith | ff71939 | 2024-02-12 18:39:21 +0000 | [diff] [blame] | 731 | if (PREDICT_FALSE (sa_drop_no_crypto != 0)) |
| 732 | { |
| 733 | err = ESP_ENCRYPT_ERROR_NO_ENCRYPTION; |
| 734 | esp_encrypt_set_next_index (b[0], node, thread_index, err, n_noop, |
| 735 | noop_nexts, drop_next, sa_index0); |
| 736 | goto trace; |
| 737 | } |
| 738 | |
Benoît Ganne | 5527a78 | 2022-01-18 15:56:41 +0100 | [diff] [blame] | 739 | if (PREDICT_FALSE ((u16) ~0 == sa0->thread_index)) |
Neale Ranns | f62a8c0 | 2019-04-02 08:13:33 +0000 | [diff] [blame] | 740 | { |
| 741 | /* this is the first packet to use this SA, claim the SA |
| 742 | * for this thread. this could happen simultaneously on |
| 743 | * another thread */ |
Neale Ranns | 1a52d37 | 2021-02-04 11:33:32 +0000 | [diff] [blame] | 744 | clib_atomic_cmp_and_swap (&sa0->thread_index, ~0, |
Neale Ranns | f62a8c0 | 2019-04-02 08:13:33 +0000 | [diff] [blame] | 745 | ipsec_sa_assign_thread (thread_index)); |
| 746 | } |
| 747 | |
Neale Ranns | 1a52d37 | 2021-02-04 11:33:32 +0000 | [diff] [blame] | 748 | if (PREDICT_FALSE (thread_index != sa0->thread_index)) |
Neale Ranns | f62a8c0 | 2019-04-02 08:13:33 +0000 | [diff] [blame] | 749 | { |
Neale Ranns | aa7d766 | 2021-02-10 08:42:49 +0000 | [diff] [blame] | 750 | vnet_buffer (b[0])->ipsec.thread_index = sa0->thread_index; |
Neale Ranns | f16e9a5 | 2021-02-25 19:09:24 +0000 | [diff] [blame] | 751 | err = ESP_ENCRYPT_ERROR_HANDOFF; |
Arthur de Kerhor | ad95b06 | 2022-11-16 19:12:05 +0100 | [diff] [blame] | 752 | esp_encrypt_set_next_index (b[0], node, thread_index, err, n_noop, |
| 753 | noop_nexts, handoff_next, |
| 754 | current_sa_index); |
Neale Ranns | f62a8c0 | 2019-04-02 08:13:33 +0000 | [diff] [blame] | 755 | goto trace; |
| 756 | } |
| 757 | |
Filip Tehlar | efcad1a | 2020-02-04 09:36:04 +0000 | [diff] [blame] | 758 | lb = b[0]; |
| 759 | n_bufs = vlib_buffer_chain_linearize (vm, b[0]); |
| 760 | if (n_bufs == 0) |
Damjan Marion | c59b9a2 | 2019-03-19 15:38:40 +0100 | [diff] [blame] | 761 | { |
Neale Ranns | f16e9a5 | 2021-02-25 19:09:24 +0000 | [diff] [blame] | 762 | err = ESP_ENCRYPT_ERROR_NO_BUFFERS; |
Arthur de Kerhor | ad95b06 | 2022-11-16 19:12:05 +0100 | [diff] [blame] | 763 | esp_encrypt_set_next_index (b[0], node, thread_index, err, n_noop, |
| 764 | noop_nexts, drop_next, current_sa_index); |
Damjan Marion | c59b9a2 | 2019-03-19 15:38:40 +0100 | [diff] [blame] | 765 | goto trace; |
| 766 | } |
| 767 | |
Filip Tehlar | efcad1a | 2020-02-04 09:36:04 +0000 | [diff] [blame] | 768 | if (n_bufs > 1) |
| 769 | { |
Filip Tehlar | efcad1a | 2020-02-04 09:36:04 +0000 | [diff] [blame] | 770 | /* find last buffer in the chain */ |
| 771 | while (lb->flags & VLIB_BUFFER_NEXT_PRESENT) |
| 772 | lb = vlib_get_buffer (vm, lb->next_buffer); |
| 773 | } |
Filip Tehlar | efcad1a | 2020-02-04 09:36:04 +0000 | [diff] [blame] | 774 | |
Damjan Marion | c59b9a2 | 2019-03-19 15:38:40 +0100 | [diff] [blame] | 775 | if (PREDICT_FALSE (esp_seq_advance (sa0))) |
| 776 | { |
Neale Ranns | f16e9a5 | 2021-02-25 19:09:24 +0000 | [diff] [blame] | 777 | err = ESP_ENCRYPT_ERROR_SEQ_CYCLED; |
Arthur de Kerhor | ad95b06 | 2022-11-16 19:12:05 +0100 | [diff] [blame] | 778 | esp_encrypt_set_next_index (b[0], node, thread_index, err, n_noop, |
| 779 | noop_nexts, drop_next, current_sa_index); |
Damjan Marion | c59b9a2 | 2019-03-19 15:38:40 +0100 | [diff] [blame] | 780 | goto trace; |
| 781 | } |
| 782 | |
| 783 | /* space for IV */ |
| 784 | hdr_len = iv_sz; |
| 785 | |
Damjan Marion | d709cbc | 2019-03-26 13:16:42 +0100 | [diff] [blame] | 786 | if (ipsec_sa_is_set_IS_TUNNEL (sa0)) |
Damjan Marion | c59b9a2 | 2019-03-19 15:38:40 +0100 | [diff] [blame] | 787 | { |
| 788 | payload = vlib_buffer_get_current (b[0]); |
Neale Ranns | f16e9a5 | 2021-02-25 19:09:24 +0000 | [diff] [blame] | 789 | next_hdr_ptr = esp_add_footer_and_icv ( |
Fan Zhang | b8cb223 | 2024-03-12 20:39:12 +0000 | [diff] [blame] | 790 | vm, &lb, esp_align, icv_sz, buffer_data_size, |
Neale Ranns | f16e9a5 | 2021-02-25 19:09:24 +0000 | [diff] [blame] | 791 | vlib_buffer_length_in_chain (vm, b[0])); |
Filip Tehlar | 8cdb1a0 | 2019-11-18 22:21:37 +0000 | [diff] [blame] | 792 | if (!next_hdr_ptr) |
Filip Tehlar | e4e8c6b | 2020-02-13 07:49:30 +0000 | [diff] [blame] | 793 | { |
Neale Ranns | f16e9a5 | 2021-02-25 19:09:24 +0000 | [diff] [blame] | 794 | err = ESP_ENCRYPT_ERROR_NO_BUFFERS; |
Arthur de Kerhor | ad95b06 | 2022-11-16 19:12:05 +0100 | [diff] [blame] | 795 | esp_encrypt_set_next_index (b[0], node, thread_index, err, |
| 796 | n_noop, noop_nexts, drop_next, |
| 797 | current_sa_index); |
Filip Tehlar | e4e8c6b | 2020-02-13 07:49:30 +0000 | [diff] [blame] | 798 | goto trace; |
| 799 | } |
Filip Tehlar | efcad1a | 2020-02-04 09:36:04 +0000 | [diff] [blame] | 800 | b[0]->flags &= ~VLIB_BUFFER_TOTAL_LENGTH_VALID; |
Filip Tehlar | 8cdb1a0 | 2019-11-18 22:21:37 +0000 | [diff] [blame] | 801 | payload_len = b[0]->current_length; |
Filip Tehlar | efcad1a | 2020-02-04 09:36:04 +0000 | [diff] [blame] | 802 | payload_len_total = vlib_buffer_length_in_chain (vm, b[0]); |
Damjan Marion | c59b9a2 | 2019-03-19 15:38:40 +0100 | [diff] [blame] | 803 | |
| 804 | /* ESP header */ |
| 805 | hdr_len += sizeof (*esp); |
| 806 | esp = (esp_header_t *) (payload - hdr_len); |
| 807 | |
| 808 | /* optional UDP header */ |
Damjan Marion | d709cbc | 2019-03-26 13:16:42 +0100 | [diff] [blame] | 809 | if (ipsec_sa_is_set_UDP_ENCAP (sa0)) |
Damjan Marion | c98275f | 2019-03-06 14:05:01 +0100 | [diff] [blame] | 810 | { |
Damjan Marion | c59b9a2 | 2019-03-19 15:38:40 +0100 | [diff] [blame] | 811 | hdr_len += sizeof (udp_header_t); |
| 812 | esp_fill_udp_hdr (sa0, (udp_header_t *) (payload - hdr_len), |
Filip Tehlar | efcad1a | 2020-02-04 09:36:04 +0000 | [diff] [blame] | 813 | payload_len_total + hdr_len); |
Damjan Marion | c59b9a2 | 2019-03-19 15:38:40 +0100 | [diff] [blame] | 814 | } |
| 815 | |
| 816 | /* IP header */ |
Damjan Marion | d709cbc | 2019-03-26 13:16:42 +0100 | [diff] [blame] | 817 | if (ipsec_sa_is_set_IS_TUNNEL_V6 (sa0)) |
Damjan Marion | c59b9a2 | 2019-03-19 15:38:40 +0100 | [diff] [blame] | 818 | { |
| 819 | ip6_header_t *ip6; |
| 820 | u16 len = sizeof (ip6_header_t); |
| 821 | hdr_len += len; |
| 822 | ip6 = (ip6_header_t *) (payload - hdr_len); |
Neale Ranns | 041add7 | 2020-01-02 04:06:10 +0000 | [diff] [blame] | 823 | clib_memcpy_fast (ip6, &sa0->ip6_hdr, sizeof (ip6_header_t)); |
| 824 | |
Neale Ranns | 4a58e49 | 2020-12-21 13:19:10 +0000 | [diff] [blame] | 825 | if (VNET_LINK_IP6 == lt) |
Neale Ranns | 041add7 | 2020-01-02 04:06:10 +0000 | [diff] [blame] | 826 | { |
| 827 | *next_hdr_ptr = IP_PROTOCOL_IPV6; |
| 828 | tunnel_encap_fixup_6o6 (sa0->tunnel_flags, |
| 829 | (const ip6_header_t *) payload, |
| 830 | ip6); |
| 831 | } |
Neale Ranns | 4a58e49 | 2020-12-21 13:19:10 +0000 | [diff] [blame] | 832 | else if (VNET_LINK_IP4 == lt) |
Neale Ranns | 041add7 | 2020-01-02 04:06:10 +0000 | [diff] [blame] | 833 | { |
| 834 | *next_hdr_ptr = IP_PROTOCOL_IP_IN_IP; |
Neale Ranns | a91cb45 | 2021-02-04 11:02:52 +0000 | [diff] [blame] | 835 | tunnel_encap_fixup_4o6 (sa0->tunnel_flags, b[0], |
| 836 | (const ip4_header_t *) payload, ip6); |
Neale Ranns | 041add7 | 2020-01-02 04:06:10 +0000 | [diff] [blame] | 837 | } |
Neale Ranns | 4a58e49 | 2020-12-21 13:19:10 +0000 | [diff] [blame] | 838 | else if (VNET_LINK_MPLS == lt) |
| 839 | { |
| 840 | *next_hdr_ptr = IP_PROTOCOL_MPLS_IN_IP; |
| 841 | tunnel_encap_fixup_mplso6 ( |
Neale Ranns | a91cb45 | 2021-02-04 11:02:52 +0000 | [diff] [blame] | 842 | sa0->tunnel_flags, b[0], |
| 843 | (const mpls_unicast_header_t *) payload, ip6); |
Neale Ranns | 4a58e49 | 2020-12-21 13:19:10 +0000 | [diff] [blame] | 844 | } |
| 845 | else |
| 846 | ASSERT (0); |
| 847 | |
Filip Tehlar | efcad1a | 2020-02-04 09:36:04 +0000 | [diff] [blame] | 848 | len = payload_len_total + hdr_len - len; |
Damjan Marion | c59b9a2 | 2019-03-19 15:38:40 +0100 | [diff] [blame] | 849 | ip6->payload_length = clib_net_to_host_u16 (len); |
Neale Ranns | 45d6d83 | 2021-01-19 13:38:47 +0000 | [diff] [blame] | 850 | b[0]->flags |= VNET_BUFFER_F_LOCALLY_ORIGINATED; |
Damjan Marion | c98275f | 2019-03-06 14:05:01 +0100 | [diff] [blame] | 851 | } |
| 852 | else |
| 853 | { |
Damjan Marion | c59b9a2 | 2019-03-19 15:38:40 +0100 | [diff] [blame] | 854 | ip4_header_t *ip4; |
| 855 | u16 len = sizeof (ip4_header_t); |
| 856 | hdr_len += len; |
| 857 | ip4 = (ip4_header_t *) (payload - hdr_len); |
Neale Ranns | 041add7 | 2020-01-02 04:06:10 +0000 | [diff] [blame] | 858 | clib_memcpy_fast (ip4, &sa0->ip4_hdr, sizeof (ip4_header_t)); |
| 859 | |
Neale Ranns | 4a58e49 | 2020-12-21 13:19:10 +0000 | [diff] [blame] | 860 | if (VNET_LINK_IP6 == lt) |
Neale Ranns | 041add7 | 2020-01-02 04:06:10 +0000 | [diff] [blame] | 861 | { |
| 862 | *next_hdr_ptr = IP_PROTOCOL_IPV6; |
| 863 | tunnel_encap_fixup_6o4_w_chksum (sa0->tunnel_flags, |
| 864 | (const ip6_header_t *) |
| 865 | payload, ip4); |
| 866 | } |
Neale Ranns | 4a58e49 | 2020-12-21 13:19:10 +0000 | [diff] [blame] | 867 | else if (VNET_LINK_IP4 == lt) |
Neale Ranns | 041add7 | 2020-01-02 04:06:10 +0000 | [diff] [blame] | 868 | { |
| 869 | *next_hdr_ptr = IP_PROTOCOL_IP_IN_IP; |
| 870 | tunnel_encap_fixup_4o4_w_chksum (sa0->tunnel_flags, |
| 871 | (const ip4_header_t *) |
| 872 | payload, ip4); |
| 873 | } |
Neale Ranns | 4a58e49 | 2020-12-21 13:19:10 +0000 | [diff] [blame] | 874 | else if (VNET_LINK_MPLS == lt) |
| 875 | { |
| 876 | *next_hdr_ptr = IP_PROTOCOL_MPLS_IN_IP; |
| 877 | tunnel_encap_fixup_mplso4_w_chksum ( |
| 878 | sa0->tunnel_flags, (const mpls_unicast_header_t *) payload, |
| 879 | ip4); |
| 880 | } |
| 881 | else |
| 882 | ASSERT (0); |
| 883 | |
Filip Tehlar | efcad1a | 2020-02-04 09:36:04 +0000 | [diff] [blame] | 884 | len = payload_len_total + hdr_len; |
Damjan Marion | c59b9a2 | 2019-03-19 15:38:40 +0100 | [diff] [blame] | 885 | esp_update_ip4_hdr (ip4, len, /* is_transport */ 0, 0); |
Damjan Marion | c98275f | 2019-03-06 14:05:01 +0100 | [diff] [blame] | 886 | } |
Damjan Marion | c98275f | 2019-03-06 14:05:01 +0100 | [diff] [blame] | 887 | |
Jeff Shaw | 26574dc | 2024-06-17 16:01:20 -0700 | [diff] [blame] | 888 | if (ipsec_sa_is_set_UDP_ENCAP (sa0) && |
| 889 | ipsec_sa_is_set_IS_TUNNEL_V6 (sa0)) |
| 890 | { |
| 891 | i16 l3_off = b[0]->current_data - hdr_len; |
| 892 | i16 l4_off = l3_off + sizeof (ip6_header_t); |
| 893 | |
| 894 | set_ip6_udp_cksum_offload (b[0], l3_off, l4_off); |
| 895 | } |
| 896 | |
Neale Ranns | 72f2a3a | 2019-06-17 15:43:38 +0000 | [diff] [blame] | 897 | dpo = &sa0->dpo; |
Neale Ranns | 25edf14 | 2019-03-22 08:12:48 +0000 | [diff] [blame] | 898 | if (!is_tun) |
| 899 | { |
Neale Ranns | f16e9a5 | 2021-02-25 19:09:24 +0000 | [diff] [blame] | 900 | sync_next[0] = dpo->dpoi_next_node; |
Neale Ranns | 25edf14 | 2019-03-22 08:12:48 +0000 | [diff] [blame] | 901 | vnet_buffer (b[0])->ip.adj_index[VLIB_TX] = dpo->dpoi_index; |
| 902 | } |
Neale Ranns | 4ec36c5 | 2020-03-31 09:21:29 -0400 | [diff] [blame] | 903 | else |
Neale Ranns | f16e9a5 | 2021-02-25 19:09:24 +0000 | [diff] [blame] | 904 | sync_next[0] = ESP_ENCRYPT_NEXT_INTERFACE_OUTPUT; |
Neale Ranns | 9ec846c | 2021-02-09 14:04:02 +0000 | [diff] [blame] | 905 | b[0]->flags |= VNET_BUFFER_F_LOCALLY_ORIGINATED; |
Damjan Marion | c98275f | 2019-03-06 14:05:01 +0100 | [diff] [blame] | 906 | } |
Damjan Marion | c59b9a2 | 2019-03-19 15:38:40 +0100 | [diff] [blame] | 907 | else /* transport mode */ |
Damjan Marion | c98275f | 2019-03-06 14:05:01 +0100 | [diff] [blame] | 908 | { |
Ole Troan | 03092c1 | 2021-11-23 15:55:39 +0100 | [diff] [blame] | 909 | u8 *l2_hdr, l2_len, *ip_hdr; |
| 910 | u16 ip_len; |
Neale Ranns | 0295040 | 2019-12-20 00:54:57 +0000 | [diff] [blame] | 911 | ip6_ext_header_t *ext_hdr; |
Damjan Marion | c59b9a2 | 2019-03-19 15:38:40 +0100 | [diff] [blame] | 912 | udp_header_t *udp = 0; |
Alexander Chernavin | b0d2eda | 2020-03-25 10:56:52 -0400 | [diff] [blame] | 913 | u16 udp_len = 0; |
Damjan Marion | c59b9a2 | 2019-03-19 15:38:40 +0100 | [diff] [blame] | 914 | u8 *old_ip_hdr = vlib_buffer_get_current (b[0]); |
Damjan Marion | c98275f | 2019-03-06 14:05:01 +0100 | [diff] [blame] | 915 | |
Ole Troan | 03092c1 | 2021-11-23 15:55:39 +0100 | [diff] [blame] | 916 | /* |
| 917 | * Get extension header chain length. It might be longer than the |
| 918 | * buffer's pre_data area. |
| 919 | */ |
Neale Ranns | 4a58e49 | 2020-12-21 13:19:10 +0000 | [diff] [blame] | 920 | ip_len = |
| 921 | (VNET_LINK_IP6 == lt ? |
| 922 | esp_get_ip6_hdr_len ((ip6_header_t *) old_ip_hdr, &ext_hdr) : |
| 923 | ip4_header_bytes ((ip4_header_t *) old_ip_hdr)); |
Ole Troan | 03092c1 | 2021-11-23 15:55:39 +0100 | [diff] [blame] | 924 | if ((old_ip_hdr - ip_len) < &b[0]->pre_data[0]) |
| 925 | { |
| 926 | err = ESP_ENCRYPT_ERROR_NO_BUFFERS; |
Arthur de Kerhor | ad95b06 | 2022-11-16 19:12:05 +0100 | [diff] [blame] | 927 | esp_encrypt_set_next_index (b[0], node, thread_index, err, |
| 928 | n_noop, noop_nexts, drop_next, |
| 929 | current_sa_index); |
Ole Troan | 03092c1 | 2021-11-23 15:55:39 +0100 | [diff] [blame] | 930 | goto trace; |
| 931 | } |
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 | vlib_buffer_advance (b[0], ip_len); |
| 934 | payload = vlib_buffer_get_current (b[0]); |
Neale Ranns | f16e9a5 | 2021-02-25 19:09:24 +0000 | [diff] [blame] | 935 | next_hdr_ptr = esp_add_footer_and_icv ( |
Fan Zhang | b8cb223 | 2024-03-12 20:39:12 +0000 | [diff] [blame] | 936 | vm, &lb, esp_align, icv_sz, buffer_data_size, |
Neale Ranns | f16e9a5 | 2021-02-25 19:09:24 +0000 | [diff] [blame] | 937 | vlib_buffer_length_in_chain (vm, b[0])); |
Filip Tehlar | 8cdb1a0 | 2019-11-18 22:21:37 +0000 | [diff] [blame] | 938 | if (!next_hdr_ptr) |
Fan Zhang | 18f0e31 | 2020-10-19 13:08:34 +0100 | [diff] [blame] | 939 | { |
Neale Ranns | f16e9a5 | 2021-02-25 19:09:24 +0000 | [diff] [blame] | 940 | err = ESP_ENCRYPT_ERROR_NO_BUFFERS; |
Arthur de Kerhor | ad95b06 | 2022-11-16 19:12:05 +0100 | [diff] [blame] | 941 | esp_encrypt_set_next_index (b[0], node, thread_index, err, |
| 942 | n_noop, noop_nexts, drop_next, |
| 943 | current_sa_index); |
Fan Zhang | 18f0e31 | 2020-10-19 13:08:34 +0100 | [diff] [blame] | 944 | goto trace; |
| 945 | } |
Filip Tehlar | efcad1a | 2020-02-04 09:36:04 +0000 | [diff] [blame] | 946 | |
| 947 | b[0]->flags &= ~VLIB_BUFFER_TOTAL_LENGTH_VALID; |
Filip Tehlar | 8cdb1a0 | 2019-11-18 22:21:37 +0000 | [diff] [blame] | 948 | payload_len = b[0]->current_length; |
Filip Tehlar | efcad1a | 2020-02-04 09:36:04 +0000 | [diff] [blame] | 949 | payload_len_total = vlib_buffer_length_in_chain (vm, b[0]); |
Damjan Marion | c59b9a2 | 2019-03-19 15:38:40 +0100 | [diff] [blame] | 950 | |
| 951 | /* ESP header */ |
| 952 | hdr_len += sizeof (*esp); |
| 953 | esp = (esp_header_t *) (payload - hdr_len); |
| 954 | |
| 955 | /* optional UDP header */ |
Damjan Marion | d709cbc | 2019-03-26 13:16:42 +0100 | [diff] [blame] | 956 | if (ipsec_sa_is_set_UDP_ENCAP (sa0)) |
Damjan Marion | c98275f | 2019-03-06 14:05:01 +0100 | [diff] [blame] | 957 | { |
Damjan Marion | c59b9a2 | 2019-03-19 15:38:40 +0100 | [diff] [blame] | 958 | hdr_len += sizeof (udp_header_t); |
| 959 | udp = (udp_header_t *) (payload - hdr_len); |
Damjan Marion | c98275f | 2019-03-06 14:05:01 +0100 | [diff] [blame] | 960 | } |
| 961 | |
Damjan Marion | c59b9a2 | 2019-03-19 15:38:40 +0100 | [diff] [blame] | 962 | /* IP header */ |
| 963 | hdr_len += ip_len; |
| 964 | ip_hdr = payload - hdr_len; |
| 965 | |
| 966 | /* L2 header */ |
Neale Ranns | c87b66c | 2019-02-07 07:26:12 -0800 | [diff] [blame] | 967 | if (!is_tun) |
| 968 | { |
| 969 | l2_len = vnet_buffer (b[0])->ip.save_rewrite_length; |
| 970 | hdr_len += l2_len; |
| 971 | l2_hdr = payload - hdr_len; |
Damjan Marion | c59b9a2 | 2019-03-19 15:38:40 +0100 | [diff] [blame] | 972 | |
Neale Ranns | c87b66c | 2019-02-07 07:26:12 -0800 | [diff] [blame] | 973 | /* copy l2 and ip header */ |
| 974 | clib_memcpy_le32 (l2_hdr, old_ip_hdr - l2_len, l2_len); |
| 975 | } |
| 976 | else |
| 977 | l2_len = 0; |
| 978 | |
Matthew Smith | 6f1eb48 | 2022-08-09 22:19:38 +0000 | [diff] [blame] | 979 | u16 len; |
| 980 | len = payload_len_total + hdr_len - l2_len; |
| 981 | |
Neale Ranns | 4a58e49 | 2020-12-21 13:19:10 +0000 | [diff] [blame] | 982 | if (VNET_LINK_IP6 == lt) |
Damjan Marion | c59b9a2 | 2019-03-19 15:38:40 +0100 | [diff] [blame] | 983 | { |
Neale Ranns | 0295040 | 2019-12-20 00:54:57 +0000 | [diff] [blame] | 984 | ip6_header_t *ip6 = (ip6_header_t *) (old_ip_hdr); |
| 985 | if (PREDICT_TRUE (NULL == ext_hdr)) |
| 986 | { |
| 987 | *next_hdr_ptr = ip6->protocol; |
Matthew Smith | 6f1eb48 | 2022-08-09 22:19:38 +0000 | [diff] [blame] | 988 | ip6->protocol = |
| 989 | (udp) ? IP_PROTOCOL_UDP : IP_PROTOCOL_IPSEC_ESP; |
Neale Ranns | 0295040 | 2019-12-20 00:54:57 +0000 | [diff] [blame] | 990 | } |
| 991 | else |
| 992 | { |
| 993 | *next_hdr_ptr = ext_hdr->next_hdr; |
Matthew Smith | 6f1eb48 | 2022-08-09 22:19:38 +0000 | [diff] [blame] | 994 | ext_hdr->next_hdr = |
| 995 | (udp) ? IP_PROTOCOL_UDP : IP_PROTOCOL_IPSEC_ESP; |
Neale Ranns | 0295040 | 2019-12-20 00:54:57 +0000 | [diff] [blame] | 996 | } |
Neale Ranns | d207fd7 | 2019-04-18 17:18:12 -0700 | [diff] [blame] | 997 | ip6->payload_length = |
Matthew Smith | 6f1eb48 | 2022-08-09 22:19:38 +0000 | [diff] [blame] | 998 | clib_host_to_net_u16 (len - sizeof (ip6_header_t)); |
Damjan Marion | c59b9a2 | 2019-03-19 15:38:40 +0100 | [diff] [blame] | 999 | } |
Neale Ranns | 4a58e49 | 2020-12-21 13:19:10 +0000 | [diff] [blame] | 1000 | else if (VNET_LINK_IP4 == lt) |
Damjan Marion | c98275f | 2019-03-06 14:05:01 +0100 | [diff] [blame] | 1001 | { |
Neale Ranns | 0295040 | 2019-12-20 00:54:57 +0000 | [diff] [blame] | 1002 | ip4_header_t *ip4 = (ip4_header_t *) (old_ip_hdr); |
Damjan Marion | c59b9a2 | 2019-03-19 15:38:40 +0100 | [diff] [blame] | 1003 | *next_hdr_ptr = ip4->protocol; |
Matthew Smith | 6f1eb48 | 2022-08-09 22:19:38 +0000 | [diff] [blame] | 1004 | esp_update_ip4_hdr (ip4, len, /* is_transport */ 1, |
| 1005 | (udp != NULL)); |
Damjan Marion | c98275f | 2019-03-06 14:05:01 +0100 | [diff] [blame] | 1006 | } |
Damjan Marion | c98275f | 2019-03-06 14:05:01 +0100 | [diff] [blame] | 1007 | |
Neale Ranns | 0295040 | 2019-12-20 00:54:57 +0000 | [diff] [blame] | 1008 | clib_memcpy_le64 (ip_hdr, old_ip_hdr, ip_len); |
| 1009 | |
Alexander Chernavin | b0d2eda | 2020-03-25 10:56:52 -0400 | [diff] [blame] | 1010 | if (udp) |
| 1011 | { |
Matthew Smith | 6f1eb48 | 2022-08-09 22:19:38 +0000 | [diff] [blame] | 1012 | udp_len = len - ip_len; |
Alexander Chernavin | b0d2eda | 2020-03-25 10:56:52 -0400 | [diff] [blame] | 1013 | esp_fill_udp_hdr (sa0, udp, udp_len); |
| 1014 | } |
| 1015 | |
Jeff Shaw | 26574dc | 2024-06-17 16:01:20 -0700 | [diff] [blame] | 1016 | if (udp && (VNET_LINK_IP6 == lt)) |
| 1017 | { |
| 1018 | i16 l3_off = b[0]->current_data - hdr_len + l2_len; |
| 1019 | i16 l4_off = l3_off + ip_len; |
| 1020 | |
| 1021 | set_ip6_udp_cksum_offload (b[0], l3_off, l4_off); |
| 1022 | } |
| 1023 | |
Neale Ranns | f16e9a5 | 2021-02-25 19:09:24 +0000 | [diff] [blame] | 1024 | sync_next[0] = ESP_ENCRYPT_NEXT_INTERFACE_OUTPUT; |
Damjan Marion | c98275f | 2019-03-06 14:05:01 +0100 | [diff] [blame] | 1025 | } |
| 1026 | |
PiotrX Kleski | fdca4dd | 2020-05-05 14:14:22 +0200 | [diff] [blame] | 1027 | if (lb != b[0]) |
| 1028 | { |
| 1029 | crypto_ops = &ptd->chained_crypto_ops; |
| 1030 | integ_ops = &ptd->chained_integ_ops; |
| 1031 | } |
| 1032 | else |
| 1033 | { |
| 1034 | crypto_ops = &ptd->crypto_ops; |
| 1035 | integ_ops = &ptd->integ_ops; |
| 1036 | } |
| 1037 | |
Damjan Marion | c59b9a2 | 2019-03-19 15:38:40 +0100 | [diff] [blame] | 1038 | esp->spi = spi; |
| 1039 | esp->seq = clib_net_to_host_u32 (sa0->seq); |
Damjan Marion | c98275f | 2019-03-06 14:05:01 +0100 | [diff] [blame] | 1040 | |
Fan Zhang | f539578 | 2020-04-29 14:00:03 +0100 | [diff] [blame] | 1041 | if (is_async) |
Matthew Smith | 51d56ba | 2021-06-04 09:18:37 -0500 | [diff] [blame] | 1042 | { |
| 1043 | async_op = sa0->crypto_async_enc_op_id; |
| 1044 | |
| 1045 | /* get a frame for this op if we don't yet have one or it's full |
| 1046 | */ |
| 1047 | if (NULL == async_frames[async_op] || |
| 1048 | vnet_crypto_async_frame_is_full (async_frames[async_op])) |
| 1049 | { |
| 1050 | async_frames[async_op] = |
| 1051 | vnet_crypto_async_get_frame (vm, async_op); |
gaoginskx | f441b5d | 2021-06-07 12:07:01 +0100 | [diff] [blame] | 1052 | |
| 1053 | if (PREDICT_FALSE (!async_frames[async_op])) |
| 1054 | { |
| 1055 | err = ESP_ENCRYPT_ERROR_NO_AVAIL_FRAME; |
| 1056 | esp_encrypt_set_next_index (b[0], node, thread_index, err, |
| 1057 | n_noop, noop_nexts, drop_next, |
| 1058 | current_sa_index); |
| 1059 | goto trace; |
| 1060 | } |
| 1061 | |
Matthew Smith | 51d56ba | 2021-06-04 09:18:37 -0500 | [diff] [blame] | 1062 | /* Save the frame to the list we'll submit at the end */ |
| 1063 | vec_add1 (ptd->async_frames, async_frames[async_op]); |
| 1064 | } |
| 1065 | |
| 1066 | esp_prepare_async_frame (vm, ptd, async_frames[async_op], sa0, b[0], |
| 1067 | esp, payload, payload_len, iv_sz, icv_sz, |
| 1068 | from[b - bufs], sync_next[0], hdr_len, |
| 1069 | async_next_node, lb); |
| 1070 | } |
Fan Zhang | f539578 | 2020-04-29 14:00:03 +0100 | [diff] [blame] | 1071 | else |
Neale Ranns | 5b89110 | 2021-06-28 13:31:28 +0000 | [diff] [blame] | 1072 | esp_prepare_sync_op (vm, ptd, crypto_ops, integ_ops, sa0, sa0->seq_hi, |
| 1073 | payload, payload_len, iv_sz, icv_sz, n_sync, b, |
| 1074 | lb, hdr_len, esp); |
Damjan Marion | c98275f | 2019-03-06 14:05:01 +0100 | [diff] [blame] | 1075 | |
Damjan Marion | c59b9a2 | 2019-03-19 15:38:40 +0100 | [diff] [blame] | 1076 | vlib_buffer_advance (b[0], 0LL - hdr_len); |
Damjan Marion | c98275f | 2019-03-06 14:05:01 +0100 | [diff] [blame] | 1077 | |
Damjan Marion | c59b9a2 | 2019-03-19 15:38:40 +0100 | [diff] [blame] | 1078 | current_sa_packets += 1; |
Filip Tehlar | efcad1a | 2020-02-04 09:36:04 +0000 | [diff] [blame] | 1079 | current_sa_bytes += payload_len_total; |
Damjan Marion | c59b9a2 | 2019-03-19 15:38:40 +0100 | [diff] [blame] | 1080 | |
| 1081 | trace: |
| 1082 | if (PREDICT_FALSE (b[0]->flags & VLIB_BUFFER_IS_TRACED)) |
Damjan Marion | c98275f | 2019-03-06 14:05:01 +0100 | [diff] [blame] | 1083 | { |
Damjan Marion | c59b9a2 | 2019-03-19 15:38:40 +0100 | [diff] [blame] | 1084 | esp_encrypt_trace_t *tr = vlib_add_trace (vm, node, b[0], |
| 1085 | sizeof (*tr)); |
Neale Ranns | 6fdcc3d | 2021-10-08 07:30:47 +0000 | [diff] [blame] | 1086 | if (INDEX_INVALID == sa_index0) |
| 1087 | clib_memset_u8 (tr, 0xff, sizeof (*tr)); |
| 1088 | else |
| 1089 | { |
| 1090 | tr->sa_index = sa_index0; |
| 1091 | tr->spi = sa0->spi; |
Neale Ranns | 6fdcc3d | 2021-10-08 07:30:47 +0000 | [diff] [blame] | 1092 | tr->seq = sa0->seq; |
| 1093 | tr->sa_seq_hi = sa0->seq_hi; |
| 1094 | tr->udp_encap = ipsec_sa_is_set_UDP_ENCAP (sa0); |
| 1095 | tr->crypto_alg = sa0->crypto_alg; |
| 1096 | tr->integ_alg = sa0->integ_alg; |
| 1097 | } |
Damjan Marion | c98275f | 2019-03-06 14:05:01 +0100 | [diff] [blame] | 1098 | } |
Neale Ranns | f16e9a5 | 2021-02-25 19:09:24 +0000 | [diff] [blame] | 1099 | |
Damjan Marion | c98275f | 2019-03-06 14:05:01 +0100 | [diff] [blame] | 1100 | /* next */ |
Neale Ranns | f16e9a5 | 2021-02-25 19:09:24 +0000 | [diff] [blame] | 1101 | if (ESP_ENCRYPT_ERROR_RX_PKTS != err) |
| 1102 | { |
| 1103 | noop_bi[n_noop] = from[b - bufs]; |
| 1104 | n_noop++; |
Neale Ranns | f16e9a5 | 2021-02-25 19:09:24 +0000 | [diff] [blame] | 1105 | } |
| 1106 | else if (!is_async) |
| 1107 | { |
| 1108 | sync_bi[n_sync] = from[b - bufs]; |
| 1109 | sync_bufs[n_sync] = b[0]; |
| 1110 | n_sync++; |
| 1111 | sync_next++; |
| 1112 | } |
| 1113 | else |
| 1114 | { |
| 1115 | n_async++; |
Neale Ranns | f16e9a5 | 2021-02-25 19:09:24 +0000 | [diff] [blame] | 1116 | } |
Damjan Marion | c59b9a2 | 2019-03-19 15:38:40 +0100 | [diff] [blame] | 1117 | n_left -= 1; |
Damjan Marion | c59b9a2 | 2019-03-19 15:38:40 +0100 | [diff] [blame] | 1118 | b += 1; |
Damjan Marion | c98275f | 2019-03-06 14:05:01 +0100 | [diff] [blame] | 1119 | } |
| 1120 | |
Neale Ranns | 6fdcc3d | 2021-10-08 07:30:47 +0000 | [diff] [blame] | 1121 | if (INDEX_INVALID != current_sa_index) |
| 1122 | vlib_increment_combined_counter (&ipsec_sa_counters, thread_index, |
| 1123 | current_sa_index, current_sa_packets, |
| 1124 | current_sa_bytes); |
Neale Ranns | f16e9a5 | 2021-02-25 19:09:24 +0000 | [diff] [blame] | 1125 | if (n_sync) |
Fan Zhang | f539578 | 2020-04-29 14:00:03 +0100 | [diff] [blame] | 1126 | { |
Neale Ranns | f16e9a5 | 2021-02-25 19:09:24 +0000 | [diff] [blame] | 1127 | esp_process_ops (vm, node, ptd->crypto_ops, sync_bufs, sync_nexts, |
| 1128 | drop_next); |
| 1129 | esp_process_chained_ops (vm, node, ptd->chained_crypto_ops, sync_bufs, |
| 1130 | sync_nexts, ptd->chunks, drop_next); |
Filip Tehlar | efcad1a | 2020-02-04 09:36:04 +0000 | [diff] [blame] | 1131 | |
Neale Ranns | f16e9a5 | 2021-02-25 19:09:24 +0000 | [diff] [blame] | 1132 | esp_process_ops (vm, node, ptd->integ_ops, sync_bufs, sync_nexts, |
| 1133 | drop_next); |
| 1134 | esp_process_chained_ops (vm, node, ptd->chained_integ_ops, sync_bufs, |
| 1135 | sync_nexts, ptd->chunks, drop_next); |
Neale Ranns | fc81134 | 2021-02-26 10:35:33 +0000 | [diff] [blame] | 1136 | |
Neale Ranns | f16e9a5 | 2021-02-25 19:09:24 +0000 | [diff] [blame] | 1137 | 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] | 1138 | } |
Neale Ranns | f16e9a5 | 2021-02-25 19:09:24 +0000 | [diff] [blame] | 1139 | if (n_async) |
Fan Zhang | f539578 | 2020-04-29 14:00:03 +0100 | [diff] [blame] | 1140 | { |
Neale Ranns | fc81134 | 2021-02-26 10:35:33 +0000 | [diff] [blame] | 1141 | /* submit all of the open frames */ |
| 1142 | vnet_crypto_async_frame_t **async_frame; |
| 1143 | |
| 1144 | vec_foreach (async_frame, ptd->async_frames) |
Fan Zhang | 18f0e31 | 2020-10-19 13:08:34 +0100 | [diff] [blame] | 1145 | { |
Neale Ranns | fc81134 | 2021-02-26 10:35:33 +0000 | [diff] [blame] | 1146 | if (vnet_crypto_async_submit_open_frame (vm, *async_frame) < 0) |
| 1147 | { |
Neale Ranns | f16e9a5 | 2021-02-25 19:09:24 +0000 | [diff] [blame] | 1148 | n_noop += esp_async_recycle_failed_submit ( |
| 1149 | vm, *async_frame, node, ESP_ENCRYPT_ERROR_CRYPTO_ENGINE_ERROR, |
Arthur de Kerhor | ad95b06 | 2022-11-16 19:12:05 +0100 | [diff] [blame] | 1150 | IPSEC_SA_ERROR_CRYPTO_ENGINE_ERROR, n_noop, noop_bi, |
Xiaoming Jiang | 0c1454c | 2023-05-05 02:28:20 +0000 | [diff] [blame] | 1151 | noop_nexts, drop_next, true); |
Neale Ranns | fc81134 | 2021-02-26 10:35:33 +0000 | [diff] [blame] | 1152 | vnet_crypto_async_reset_frame (*async_frame); |
| 1153 | vnet_crypto_async_free_frame (vm, *async_frame); |
| 1154 | } |
Fan Zhang | 18f0e31 | 2020-10-19 13:08:34 +0100 | [diff] [blame] | 1155 | } |
Fan Zhang | f539578 | 2020-04-29 14:00:03 +0100 | [diff] [blame] | 1156 | } |
Neale Ranns | f16e9a5 | 2021-02-25 19:09:24 +0000 | [diff] [blame] | 1157 | if (n_noop) |
| 1158 | vlib_buffer_enqueue_to_next (vm, node, noop_bi, noop_nexts, n_noop); |
| 1159 | |
| 1160 | vlib_node_increment_counter (vm, node->node_index, ESP_ENCRYPT_ERROR_RX_PKTS, |
| 1161 | frame->n_vectors); |
Damjan Marion | c59b9a2 | 2019-03-19 15:38:40 +0100 | [diff] [blame] | 1162 | |
Damjan Marion | c59b9a2 | 2019-03-19 15:38:40 +0100 | [diff] [blame] | 1163 | return frame->n_vectors; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1164 | } |
| 1165 | |
Fan Zhang | f539578 | 2020-04-29 14:00:03 +0100 | [diff] [blame] | 1166 | always_inline uword |
| 1167 | esp_encrypt_post_inline (vlib_main_t * vm, vlib_node_runtime_t * node, |
| 1168 | vlib_frame_t * frame) |
| 1169 | { |
| 1170 | vlib_buffer_t *bufs[VLIB_FRAME_SIZE], **b = bufs; |
| 1171 | u16 nexts[VLIB_FRAME_SIZE], *next = nexts; |
| 1172 | u32 *from = vlib_frame_vector_args (frame); |
| 1173 | u32 n_left = frame->n_vectors; |
| 1174 | |
| 1175 | vlib_get_buffers (vm, from, b, n_left); |
| 1176 | |
| 1177 | if (n_left >= 4) |
| 1178 | { |
| 1179 | vlib_prefetch_buffer_header (b[0], LOAD); |
| 1180 | vlib_prefetch_buffer_header (b[1], LOAD); |
| 1181 | vlib_prefetch_buffer_header (b[2], LOAD); |
| 1182 | vlib_prefetch_buffer_header (b[3], LOAD); |
| 1183 | } |
| 1184 | |
| 1185 | while (n_left > 8) |
| 1186 | { |
| 1187 | vlib_prefetch_buffer_header (b[4], LOAD); |
| 1188 | vlib_prefetch_buffer_header (b[5], LOAD); |
| 1189 | vlib_prefetch_buffer_header (b[6], LOAD); |
| 1190 | vlib_prefetch_buffer_header (b[7], LOAD); |
| 1191 | |
| 1192 | next[0] = (esp_post_data (b[0]))->next_index; |
| 1193 | next[1] = (esp_post_data (b[1]))->next_index; |
| 1194 | next[2] = (esp_post_data (b[2]))->next_index; |
| 1195 | next[3] = (esp_post_data (b[3]))->next_index; |
| 1196 | |
| 1197 | if (PREDICT_FALSE (node->flags & VLIB_NODE_FLAG_TRACE)) |
| 1198 | { |
| 1199 | if (b[0]->flags & VLIB_BUFFER_IS_TRACED) |
| 1200 | { |
| 1201 | esp_encrypt_post_trace_t *tr = vlib_add_trace (vm, node, b[0], |
| 1202 | sizeof (*tr)); |
| 1203 | tr->next_index = next[0]; |
| 1204 | } |
| 1205 | if (b[1]->flags & VLIB_BUFFER_IS_TRACED) |
| 1206 | { |
| 1207 | esp_encrypt_post_trace_t *tr = vlib_add_trace (vm, node, b[1], |
| 1208 | sizeof (*tr)); |
| 1209 | tr->next_index = next[1]; |
| 1210 | } |
| 1211 | if (b[2]->flags & VLIB_BUFFER_IS_TRACED) |
| 1212 | { |
| 1213 | esp_encrypt_post_trace_t *tr = vlib_add_trace (vm, node, b[2], |
| 1214 | sizeof (*tr)); |
| 1215 | tr->next_index = next[2]; |
| 1216 | } |
| 1217 | if (b[3]->flags & VLIB_BUFFER_IS_TRACED) |
| 1218 | { |
| 1219 | esp_encrypt_post_trace_t *tr = vlib_add_trace (vm, node, b[3], |
| 1220 | sizeof (*tr)); |
| 1221 | tr->next_index = next[3]; |
| 1222 | } |
| 1223 | } |
| 1224 | |
| 1225 | b += 4; |
| 1226 | next += 4; |
| 1227 | n_left -= 4; |
| 1228 | } |
| 1229 | |
| 1230 | while (n_left > 0) |
| 1231 | { |
| 1232 | next[0] = (esp_post_data (b[0]))->next_index; |
| 1233 | if (PREDICT_FALSE (b[0]->flags & VLIB_BUFFER_IS_TRACED)) |
| 1234 | { |
| 1235 | esp_encrypt_post_trace_t *tr = vlib_add_trace (vm, node, b[0], |
| 1236 | sizeof (*tr)); |
| 1237 | tr->next_index = next[0]; |
| 1238 | } |
| 1239 | |
| 1240 | b += 1; |
| 1241 | next += 1; |
| 1242 | n_left -= 1; |
| 1243 | } |
| 1244 | |
| 1245 | vlib_node_increment_counter (vm, node->node_index, |
| 1246 | ESP_ENCRYPT_ERROR_POST_RX_PKTS, |
| 1247 | frame->n_vectors); |
| 1248 | vlib_buffer_enqueue_to_next (vm, node, from, nexts, frame->n_vectors); |
| 1249 | return frame->n_vectors; |
| 1250 | } |
| 1251 | |
Klement Sekera | b8f3544 | 2018-10-29 13:38:19 +0100 | [diff] [blame] | 1252 | VLIB_NODE_FN (esp4_encrypt_node) (vlib_main_t * vm, |
| 1253 | vlib_node_runtime_t * node, |
| 1254 | vlib_frame_t * from_frame) |
Klement Sekera | be5a5dd | 2018-10-09 16:05:48 +0200 | [diff] [blame] | 1255 | { |
Neale Ranns | 4a58e49 | 2020-12-21 13:19:10 +0000 | [diff] [blame] | 1256 | return esp_encrypt_inline (vm, node, from_frame, VNET_LINK_IP4, 0, |
Fan Zhang | f539578 | 2020-04-29 14:00:03 +0100 | [diff] [blame] | 1257 | esp_encrypt_async_next.esp4_post_next); |
Klement Sekera | be5a5dd | 2018-10-09 16:05:48 +0200 | [diff] [blame] | 1258 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1259 | |
Klement Sekera | be5a5dd | 2018-10-09 16:05:48 +0200 | [diff] [blame] | 1260 | VLIB_REGISTER_NODE (esp4_encrypt_node) = { |
Klement Sekera | be5a5dd | 2018-10-09 16:05:48 +0200 | [diff] [blame] | 1261 | .name = "esp4-encrypt", |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1262 | .vector_size = sizeof (u32), |
| 1263 | .format_trace = format_esp_encrypt_trace, |
| 1264 | .type = VLIB_NODE_TYPE_INTERNAL, |
| 1265 | |
Neale Ranns | 93688d7 | 2022-08-09 03:34:51 +0000 | [diff] [blame] | 1266 | .n_errors = ESP_ENCRYPT_N_ERROR, |
| 1267 | .error_counters = esp_encrypt_error_counters, |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1268 | |
| 1269 | .n_next_nodes = ESP_ENCRYPT_N_NEXT, |
Neale Ranns | 4a58e49 | 2020-12-21 13:19:10 +0000 | [diff] [blame] | 1270 | .next_nodes = { [ESP_ENCRYPT_NEXT_DROP4] = "ip4-drop", |
| 1271 | [ESP_ENCRYPT_NEXT_DROP6] = "ip6-drop", |
| 1272 | [ESP_ENCRYPT_NEXT_DROP_MPLS] = "mpls-drop", |
| 1273 | [ESP_ENCRYPT_NEXT_HANDOFF4] = "esp4-encrypt-handoff", |
| 1274 | [ESP_ENCRYPT_NEXT_HANDOFF6] = "esp6-encrypt-handoff", |
| 1275 | [ESP_ENCRYPT_NEXT_HANDOFF_MPLS] = "error-drop", |
| 1276 | [ESP_ENCRYPT_NEXT_INTERFACE_OUTPUT] = "interface-output" }, |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1277 | }; |
| 1278 | |
Fan Zhang | f539578 | 2020-04-29 14:00:03 +0100 | [diff] [blame] | 1279 | VLIB_NODE_FN (esp4_encrypt_post_node) (vlib_main_t * vm, |
| 1280 | vlib_node_runtime_t * node, |
| 1281 | vlib_frame_t * from_frame) |
| 1282 | { |
| 1283 | return esp_encrypt_post_inline (vm, node, from_frame); |
| 1284 | } |
| 1285 | |
Fan Zhang | f539578 | 2020-04-29 14:00:03 +0100 | [diff] [blame] | 1286 | VLIB_REGISTER_NODE (esp4_encrypt_post_node) = { |
| 1287 | .name = "esp4-encrypt-post", |
| 1288 | .vector_size = sizeof (u32), |
| 1289 | .format_trace = format_esp_post_encrypt_trace, |
| 1290 | .type = VLIB_NODE_TYPE_INTERNAL, |
| 1291 | .sibling_of = "esp4-encrypt", |
| 1292 | |
Neale Ranns | 93688d7 | 2022-08-09 03:34:51 +0000 | [diff] [blame] | 1293 | .n_errors = ESP_ENCRYPT_N_ERROR, |
| 1294 | .error_counters = esp_encrypt_error_counters, |
Fan Zhang | f539578 | 2020-04-29 14:00:03 +0100 | [diff] [blame] | 1295 | }; |
Fan Zhang | f539578 | 2020-04-29 14:00:03 +0100 | [diff] [blame] | 1296 | |
Klement Sekera | b8f3544 | 2018-10-29 13:38:19 +0100 | [diff] [blame] | 1297 | VLIB_NODE_FN (esp6_encrypt_node) (vlib_main_t * vm, |
| 1298 | vlib_node_runtime_t * node, |
| 1299 | vlib_frame_t * from_frame) |
Klement Sekera | be5a5dd | 2018-10-09 16:05:48 +0200 | [diff] [blame] | 1300 | { |
Neale Ranns | 4a58e49 | 2020-12-21 13:19:10 +0000 | [diff] [blame] | 1301 | return esp_encrypt_inline (vm, node, from_frame, VNET_LINK_IP6, 0, |
Fan Zhang | f539578 | 2020-04-29 14:00:03 +0100 | [diff] [blame] | 1302 | esp_encrypt_async_next.esp6_post_next); |
Klement Sekera | be5a5dd | 2018-10-09 16:05:48 +0200 | [diff] [blame] | 1303 | } |
| 1304 | |
Klement Sekera | be5a5dd | 2018-10-09 16:05:48 +0200 | [diff] [blame] | 1305 | VLIB_REGISTER_NODE (esp6_encrypt_node) = { |
Klement Sekera | be5a5dd | 2018-10-09 16:05:48 +0200 | [diff] [blame] | 1306 | .name = "esp6-encrypt", |
| 1307 | .vector_size = sizeof (u32), |
| 1308 | .format_trace = format_esp_encrypt_trace, |
| 1309 | .type = VLIB_NODE_TYPE_INTERNAL, |
Neale Ranns | b1fd80f | 2020-05-12 13:33:56 +0000 | [diff] [blame] | 1310 | .sibling_of = "esp4-encrypt", |
Klement Sekera | be5a5dd | 2018-10-09 16:05:48 +0200 | [diff] [blame] | 1311 | |
Neale Ranns | 93688d7 | 2022-08-09 03:34:51 +0000 | [diff] [blame] | 1312 | .n_errors = ESP_ENCRYPT_N_ERROR, |
| 1313 | .error_counters = esp_encrypt_error_counters, |
Klement Sekera | be5a5dd | 2018-10-09 16:05:48 +0200 | [diff] [blame] | 1314 | }; |
Klement Sekera | be5a5dd | 2018-10-09 16:05:48 +0200 | [diff] [blame] | 1315 | |
Fan Zhang | f539578 | 2020-04-29 14:00:03 +0100 | [diff] [blame] | 1316 | VLIB_NODE_FN (esp6_encrypt_post_node) (vlib_main_t * vm, |
| 1317 | vlib_node_runtime_t * node, |
| 1318 | vlib_frame_t * from_frame) |
| 1319 | { |
| 1320 | return esp_encrypt_post_inline (vm, node, from_frame); |
| 1321 | } |
| 1322 | |
Fan Zhang | f539578 | 2020-04-29 14:00:03 +0100 | [diff] [blame] | 1323 | VLIB_REGISTER_NODE (esp6_encrypt_post_node) = { |
| 1324 | .name = "esp6-encrypt-post", |
| 1325 | .vector_size = sizeof (u32), |
| 1326 | .format_trace = format_esp_post_encrypt_trace, |
| 1327 | .type = VLIB_NODE_TYPE_INTERNAL, |
Neale Ranns | b1fd80f | 2020-05-12 13:33:56 +0000 | [diff] [blame] | 1328 | .sibling_of = "esp4-encrypt", |
Fan Zhang | f539578 | 2020-04-29 14:00:03 +0100 | [diff] [blame] | 1329 | |
Neale Ranns | 93688d7 | 2022-08-09 03:34:51 +0000 | [diff] [blame] | 1330 | .n_errors = ESP_ENCRYPT_N_ERROR, |
| 1331 | .error_counters = esp_encrypt_error_counters, |
Fan Zhang | f539578 | 2020-04-29 14:00:03 +0100 | [diff] [blame] | 1332 | }; |
Fan Zhang | f539578 | 2020-04-29 14:00:03 +0100 | [diff] [blame] | 1333 | |
Neale Ranns | 25edf14 | 2019-03-22 08:12:48 +0000 | [diff] [blame] | 1334 | VLIB_NODE_FN (esp4_encrypt_tun_node) (vlib_main_t * vm, |
| 1335 | vlib_node_runtime_t * node, |
| 1336 | vlib_frame_t * from_frame) |
| 1337 | { |
Neale Ranns | 4a58e49 | 2020-12-21 13:19:10 +0000 | [diff] [blame] | 1338 | return esp_encrypt_inline (vm, node, from_frame, VNET_LINK_IP4, 1, |
Fan Zhang | f539578 | 2020-04-29 14:00:03 +0100 | [diff] [blame] | 1339 | esp_encrypt_async_next.esp4_tun_post_next); |
Neale Ranns | 25edf14 | 2019-03-22 08:12:48 +0000 | [diff] [blame] | 1340 | } |
| 1341 | |
Neale Ranns | 25edf14 | 2019-03-22 08:12:48 +0000 | [diff] [blame] | 1342 | VLIB_REGISTER_NODE (esp4_encrypt_tun_node) = { |
| 1343 | .name = "esp4-encrypt-tun", |
| 1344 | .vector_size = sizeof (u32), |
| 1345 | .format_trace = format_esp_encrypt_trace, |
| 1346 | .type = VLIB_NODE_TYPE_INTERNAL, |
| 1347 | |
Neale Ranns | 93688d7 | 2022-08-09 03:34:51 +0000 | [diff] [blame] | 1348 | .n_errors = ESP_ENCRYPT_N_ERROR, |
| 1349 | .error_counters = esp_encrypt_error_counters, |
Neale Ranns | d7603d9 | 2019-03-28 08:56:10 +0000 | [diff] [blame] | 1350 | |
Neale Ranns | f62a8c0 | 2019-04-02 08:13:33 +0000 | [diff] [blame] | 1351 | .n_next_nodes = ESP_ENCRYPT_N_NEXT, |
Neale Ranns | d7603d9 | 2019-03-28 08:56:10 +0000 | [diff] [blame] | 1352 | .next_nodes = { |
Neale Ranns | b1fd80f | 2020-05-12 13:33:56 +0000 | [diff] [blame] | 1353 | [ESP_ENCRYPT_NEXT_DROP4] = "ip4-drop", |
| 1354 | [ESP_ENCRYPT_NEXT_DROP6] = "ip6-drop", |
Neale Ranns | 4a58e49 | 2020-12-21 13:19:10 +0000 | [diff] [blame] | 1355 | [ESP_ENCRYPT_NEXT_DROP_MPLS] = "mpls-drop", |
Neale Ranns | b1fd80f | 2020-05-12 13:33:56 +0000 | [diff] [blame] | 1356 | [ESP_ENCRYPT_NEXT_HANDOFF4] = "esp4-encrypt-tun-handoff", |
Neale Ranns | 4a58e49 | 2020-12-21 13:19:10 +0000 | [diff] [blame] | 1357 | [ESP_ENCRYPT_NEXT_HANDOFF6] = "esp6-encrypt-tun-handoff", |
| 1358 | [ESP_ENCRYPT_NEXT_HANDOFF_MPLS] = "esp-mpls-encrypt-tun-handoff", |
Neale Ranns | 4ec36c5 | 2020-03-31 09:21:29 -0400 | [diff] [blame] | 1359 | [ESP_ENCRYPT_NEXT_INTERFACE_OUTPUT] = "adj-midchain-tx", |
Neale Ranns | d7603d9 | 2019-03-28 08:56:10 +0000 | [diff] [blame] | 1360 | }, |
Neale Ranns | 25edf14 | 2019-03-22 08:12:48 +0000 | [diff] [blame] | 1361 | }; |
| 1362 | |
Fan Zhang | f539578 | 2020-04-29 14:00:03 +0100 | [diff] [blame] | 1363 | VLIB_NODE_FN (esp4_encrypt_tun_post_node) (vlib_main_t * vm, |
| 1364 | vlib_node_runtime_t * node, |
| 1365 | vlib_frame_t * from_frame) |
| 1366 | { |
| 1367 | return esp_encrypt_post_inline (vm, node, from_frame); |
| 1368 | } |
| 1369 | |
Fan Zhang | f539578 | 2020-04-29 14:00:03 +0100 | [diff] [blame] | 1370 | VLIB_REGISTER_NODE (esp4_encrypt_tun_post_node) = { |
| 1371 | .name = "esp4-encrypt-tun-post", |
| 1372 | .vector_size = sizeof (u32), |
| 1373 | .format_trace = format_esp_post_encrypt_trace, |
| 1374 | .type = VLIB_NODE_TYPE_INTERNAL, |
| 1375 | .sibling_of = "esp4-encrypt-tun", |
| 1376 | |
Neale Ranns | 93688d7 | 2022-08-09 03:34:51 +0000 | [diff] [blame] | 1377 | .n_errors = ESP_ENCRYPT_N_ERROR, |
| 1378 | .error_counters = esp_encrypt_error_counters, |
Fan Zhang | f539578 | 2020-04-29 14:00:03 +0100 | [diff] [blame] | 1379 | }; |
Neale Ranns | 25edf14 | 2019-03-22 08:12:48 +0000 | [diff] [blame] | 1380 | |
| 1381 | VLIB_NODE_FN (esp6_encrypt_tun_node) (vlib_main_t * vm, |
| 1382 | vlib_node_runtime_t * node, |
| 1383 | vlib_frame_t * from_frame) |
| 1384 | { |
Neale Ranns | 4a58e49 | 2020-12-21 13:19:10 +0000 | [diff] [blame] | 1385 | return esp_encrypt_inline (vm, node, from_frame, VNET_LINK_IP6, 1, |
Fan Zhang | f539578 | 2020-04-29 14:00:03 +0100 | [diff] [blame] | 1386 | esp_encrypt_async_next.esp6_tun_post_next); |
Neale Ranns | 25edf14 | 2019-03-22 08:12:48 +0000 | [diff] [blame] | 1387 | } |
| 1388 | |
Neale Ranns | 25edf14 | 2019-03-22 08:12:48 +0000 | [diff] [blame] | 1389 | VLIB_REGISTER_NODE (esp6_encrypt_tun_node) = { |
| 1390 | .name = "esp6-encrypt-tun", |
| 1391 | .vector_size = sizeof (u32), |
| 1392 | .format_trace = format_esp_encrypt_trace, |
| 1393 | .type = VLIB_NODE_TYPE_INTERNAL, |
| 1394 | |
Neale Ranns | 93688d7 | 2022-08-09 03:34:51 +0000 | [diff] [blame] | 1395 | .n_errors = ESP_ENCRYPT_N_ERROR, |
| 1396 | .error_counters = esp_encrypt_error_counters, |
Neale Ranns | d7603d9 | 2019-03-28 08:56:10 +0000 | [diff] [blame] | 1397 | |
Neale Ranns | f62a8c0 | 2019-04-02 08:13:33 +0000 | [diff] [blame] | 1398 | .n_next_nodes = ESP_ENCRYPT_N_NEXT, |
Neale Ranns | d7603d9 | 2019-03-28 08:56:10 +0000 | [diff] [blame] | 1399 | .next_nodes = { |
Neale Ranns | b1fd80f | 2020-05-12 13:33:56 +0000 | [diff] [blame] | 1400 | [ESP_ENCRYPT_NEXT_DROP4] = "ip4-drop", |
| 1401 | [ESP_ENCRYPT_NEXT_DROP6] = "ip6-drop", |
Neale Ranns | 4a58e49 | 2020-12-21 13:19:10 +0000 | [diff] [blame] | 1402 | [ESP_ENCRYPT_NEXT_DROP_MPLS] = "mpls-drop", |
| 1403 | [ESP_ENCRYPT_NEXT_HANDOFF4] = "esp4-encrypt-tun-handoff", |
Neale Ranns | b1fd80f | 2020-05-12 13:33:56 +0000 | [diff] [blame] | 1404 | [ESP_ENCRYPT_NEXT_HANDOFF6] = "esp6-encrypt-tun-handoff", |
Neale Ranns | 4a58e49 | 2020-12-21 13:19:10 +0000 | [diff] [blame] | 1405 | [ESP_ENCRYPT_NEXT_HANDOFF_MPLS] = "esp-mpls-encrypt-tun-handoff", |
Neale Ranns | 4ec36c5 | 2020-03-31 09:21:29 -0400 | [diff] [blame] | 1406 | [ESP_ENCRYPT_NEXT_INTERFACE_OUTPUT] = "adj-midchain-tx", |
Neale Ranns | d7603d9 | 2019-03-28 08:56:10 +0000 | [diff] [blame] | 1407 | }, |
Neale Ranns | 25edf14 | 2019-03-22 08:12:48 +0000 | [diff] [blame] | 1408 | }; |
| 1409 | |
Neale Ranns | 25edf14 | 2019-03-22 08:12:48 +0000 | [diff] [blame] | 1410 | |
Fan Zhang | f539578 | 2020-04-29 14:00:03 +0100 | [diff] [blame] | 1411 | VLIB_NODE_FN (esp6_encrypt_tun_post_node) (vlib_main_t * vm, |
| 1412 | vlib_node_runtime_t * node, |
| 1413 | vlib_frame_t * from_frame) |
| 1414 | { |
| 1415 | return esp_encrypt_post_inline (vm, node, from_frame); |
| 1416 | } |
| 1417 | |
Fan Zhang | f539578 | 2020-04-29 14:00:03 +0100 | [diff] [blame] | 1418 | VLIB_REGISTER_NODE (esp6_encrypt_tun_post_node) = { |
| 1419 | .name = "esp6-encrypt-tun-post", |
| 1420 | .vector_size = sizeof (u32), |
| 1421 | .format_trace = format_esp_post_encrypt_trace, |
| 1422 | .type = VLIB_NODE_TYPE_INTERNAL, |
Neale Ranns | 4a58e49 | 2020-12-21 13:19:10 +0000 | [diff] [blame] | 1423 | .sibling_of = "esp-mpls-encrypt-tun", |
Fan Zhang | f539578 | 2020-04-29 14:00:03 +0100 | [diff] [blame] | 1424 | |
Neale Ranns | 93688d7 | 2022-08-09 03:34:51 +0000 | [diff] [blame] | 1425 | .n_errors = ESP_ENCRYPT_N_ERROR, |
| 1426 | .error_counters = esp_encrypt_error_counters, |
Fan Zhang | f539578 | 2020-04-29 14:00:03 +0100 | [diff] [blame] | 1427 | }; |
Fan Zhang | f539578 | 2020-04-29 14:00:03 +0100 | [diff] [blame] | 1428 | |
Neale Ranns | 4a58e49 | 2020-12-21 13:19:10 +0000 | [diff] [blame] | 1429 | VLIB_NODE_FN (esp_mpls_encrypt_tun_node) |
| 1430 | (vlib_main_t *vm, vlib_node_runtime_t *node, vlib_frame_t *from_frame) |
| 1431 | { |
| 1432 | return esp_encrypt_inline (vm, node, from_frame, VNET_LINK_MPLS, 1, |
| 1433 | esp_encrypt_async_next.esp_mpls_tun_post_next); |
| 1434 | } |
| 1435 | |
| 1436 | VLIB_REGISTER_NODE (esp_mpls_encrypt_tun_node) = { |
| 1437 | .name = "esp-mpls-encrypt-tun", |
| 1438 | .vector_size = sizeof (u32), |
| 1439 | .format_trace = format_esp_encrypt_trace, |
| 1440 | .type = VLIB_NODE_TYPE_INTERNAL, |
| 1441 | |
Neale Ranns | 93688d7 | 2022-08-09 03:34:51 +0000 | [diff] [blame] | 1442 | .n_errors = ESP_ENCRYPT_N_ERROR, |
| 1443 | .error_counters = esp_encrypt_error_counters, |
Neale Ranns | 4a58e49 | 2020-12-21 13:19:10 +0000 | [diff] [blame] | 1444 | |
| 1445 | .n_next_nodes = ESP_ENCRYPT_N_NEXT, |
| 1446 | .next_nodes = { |
| 1447 | [ESP_ENCRYPT_NEXT_DROP4] = "ip4-drop", |
| 1448 | [ESP_ENCRYPT_NEXT_DROP6] = "ip6-drop", |
| 1449 | [ESP_ENCRYPT_NEXT_DROP_MPLS] = "mpls-drop", |
| 1450 | [ESP_ENCRYPT_NEXT_HANDOFF4] = "esp4-encrypt-tun-handoff", |
| 1451 | [ESP_ENCRYPT_NEXT_HANDOFF6] = "esp6-encrypt-tun-handoff", |
| 1452 | [ESP_ENCRYPT_NEXT_HANDOFF_MPLS] = "esp-mpls-encrypt-tun-handoff", |
| 1453 | [ESP_ENCRYPT_NEXT_INTERFACE_OUTPUT] = "adj-midchain-tx", |
| 1454 | }, |
| 1455 | }; |
| 1456 | |
| 1457 | VLIB_NODE_FN (esp_mpls_encrypt_tun_post_node) |
| 1458 | (vlib_main_t *vm, vlib_node_runtime_t *node, vlib_frame_t *from_frame) |
| 1459 | { |
| 1460 | return esp_encrypt_post_inline (vm, node, from_frame); |
| 1461 | } |
| 1462 | |
| 1463 | VLIB_REGISTER_NODE (esp_mpls_encrypt_tun_post_node) = { |
| 1464 | .name = "esp-mpls-encrypt-tun-post", |
| 1465 | .vector_size = sizeof (u32), |
| 1466 | .format_trace = format_esp_post_encrypt_trace, |
| 1467 | .type = VLIB_NODE_TYPE_INTERNAL, |
| 1468 | .sibling_of = "esp-mpls-encrypt-tun", |
| 1469 | |
Neale Ranns | 93688d7 | 2022-08-09 03:34:51 +0000 | [diff] [blame] | 1470 | .n_errors = ESP_ENCRYPT_N_ERROR, |
| 1471 | .error_counters = esp_encrypt_error_counters, |
Neale Ranns | 4a58e49 | 2020-12-21 13:19:10 +0000 | [diff] [blame] | 1472 | }; |
| 1473 | |
Neale Ranns | 2d49830 | 2021-02-25 08:38:58 +0000 | [diff] [blame] | 1474 | #ifndef CLIB_MARCH_VARIANT |
| 1475 | |
| 1476 | static clib_error_t * |
| 1477 | esp_encrypt_init (vlib_main_t *vm) |
| 1478 | { |
| 1479 | ipsec_main_t *im = &ipsec_main; |
| 1480 | |
Dau Do | ea92116 | 2024-04-29 03:00:55 +0000 | [diff] [blame] | 1481 | im->esp4_enc_fq_index = vlib_frame_queue_main_init (esp4_encrypt_node.index, |
| 1482 | im->handoff_queue_size); |
| 1483 | im->esp6_enc_fq_index = vlib_frame_queue_main_init (esp6_encrypt_node.index, |
| 1484 | im->handoff_queue_size); |
| 1485 | im->esp4_enc_tun_fq_index = vlib_frame_queue_main_init ( |
| 1486 | esp4_encrypt_tun_node.index, im->handoff_queue_size); |
| 1487 | im->esp6_enc_tun_fq_index = vlib_frame_queue_main_init ( |
| 1488 | esp6_encrypt_tun_node.index, im->handoff_queue_size); |
| 1489 | im->esp_mpls_enc_tun_fq_index = vlib_frame_queue_main_init ( |
| 1490 | esp_mpls_encrypt_tun_node.index, im->handoff_queue_size); |
Neale Ranns | 2d49830 | 2021-02-25 08:38:58 +0000 | [diff] [blame] | 1491 | |
| 1492 | return 0; |
| 1493 | } |
| 1494 | |
| 1495 | VLIB_INIT_FUNCTION (esp_encrypt_init); |
| 1496 | |
| 1497 | #endif |
| 1498 | |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 1499 | /* |
| 1500 | * fd.io coding-style-patch-verification: ON |
| 1501 | * |
| 1502 | * Local Variables: |
| 1503 | * eval: (c-set-style "gnu") |
| 1504 | * End: |
| 1505 | */ |