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