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