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