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