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 \ |
| 30 | _(DROP, "error-drop") \ |
Neale Ranns | f62a8c0 | 2019-04-02 08:13:33 +0000 | [diff] [blame] | 31 | _(HANDOFF, "handoff") \ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 32 | _(INTERFACE_OUTPUT, "interface-output") |
| 33 | |
| 34 | #define _(v, s) ESP_ENCRYPT_NEXT_##v, |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 35 | typedef enum |
| 36 | { |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 37 | foreach_esp_encrypt_next |
| 38 | #undef _ |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 39 | ESP_ENCRYPT_N_NEXT, |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 40 | } esp_encrypt_next_t; |
| 41 | |
Damjan Marion | c59b9a2 | 2019-03-19 15:38:40 +0100 | [diff] [blame] | 42 | #define foreach_esp_encrypt_error \ |
| 43 | _(RX_PKTS, "ESP pkts received") \ |
| 44 | _(SEQ_CYCLED, "sequence number cycled (packet dropped)") \ |
| 45 | _(CRYPTO_ENGINE_ERROR, "crypto engine error (packet dropped)") \ |
Filip Tehlar | efcad1a | 2020-02-04 09:36:04 +0000 | [diff] [blame] | 46 | _(NO_BUFFERS, "no buffers (packet dropped)") \ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 47 | |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 48 | typedef enum |
| 49 | { |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 50 | #define _(sym,str) ESP_ENCRYPT_ERROR_##sym, |
| 51 | foreach_esp_encrypt_error |
| 52 | #undef _ |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 53 | ESP_ENCRYPT_N_ERROR, |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 54 | } esp_encrypt_error_t; |
| 55 | |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 56 | static char *esp_encrypt_error_strings[] = { |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 57 | #define _(sym,string) string, |
| 58 | foreach_esp_encrypt_error |
| 59 | #undef _ |
| 60 | }; |
| 61 | |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 62 | typedef struct |
| 63 | { |
Neale Ranns | 8d7c502 | 2019-02-06 01:41:05 -0800 | [diff] [blame] | 64 | u32 sa_index; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 65 | u32 spi; |
| 66 | u32 seq; |
Neale Ranns | 6afaae1 | 2019-07-17 15:07:14 +0000 | [diff] [blame] | 67 | u32 sa_seq_hi; |
Klement Sekera | 4b089f2 | 2018-04-17 18:04:57 +0200 | [diff] [blame] | 68 | u8 udp_encap; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 69 | ipsec_crypto_alg_t crypto_alg; |
| 70 | ipsec_integ_alg_t integ_alg; |
| 71 | } esp_encrypt_trace_t; |
| 72 | |
| 73 | /* packet trace format function */ |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 74 | static u8 * |
| 75 | format_esp_encrypt_trace (u8 * s, va_list * args) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 76 | { |
| 77 | CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *); |
| 78 | 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] | 79 | esp_encrypt_trace_t *t = va_arg (*args, esp_encrypt_trace_t *); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 80 | |
Guillaume Solignac | 8f818cc | 2019-05-15 12:02:33 +0200 | [diff] [blame] | 81 | s = |
| 82 | format (s, |
Neale Ranns | 6afaae1 | 2019-07-17 15:07:14 +0000 | [diff] [blame] | 83 | "esp: sa-index %d spi %u (0x%08x) seq %u sa-seq-hi %u crypto %U integrity %U%s", |
| 84 | t->sa_index, t->spi, t->spi, t->seq, t->sa_seq_hi, |
| 85 | format_ipsec_crypto_alg, |
Guillaume Solignac | 8f818cc | 2019-05-15 12:02:33 +0200 | [diff] [blame] | 86 | t->crypto_alg, format_ipsec_integ_alg, t->integ_alg, |
| 87 | t->udp_encap ? " udp-encap-enabled" : ""); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 88 | return s; |
| 89 | } |
| 90 | |
Damjan Marion | c59b9a2 | 2019-03-19 15:38:40 +0100 | [diff] [blame] | 91 | /* pad packet in input buffer */ |
| 92 | static_always_inline u8 * |
Filip Tehlar | e4e8c6b | 2020-02-13 07:49:30 +0000 | [diff] [blame] | 93 | esp_add_footer_and_icv (vlib_main_t * vm, vlib_buffer_t ** last, |
| 94 | u8 block_size, u8 icv_sz, |
Filip Tehlar | 8cdb1a0 | 2019-11-18 22:21:37 +0000 | [diff] [blame] | 95 | u16 * next, vlib_node_runtime_t * node, |
Filip Tehlar | efcad1a | 2020-02-04 09:36:04 +0000 | [diff] [blame] | 96 | u16 buffer_data_size, uword total_len) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 97 | { |
Damjan Marion | c59b9a2 | 2019-03-19 15:38:40 +0100 | [diff] [blame] | 98 | static const u8 pad_data[ESP_MAX_BLOCK_SIZE] = { |
| 99 | 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, |
| 100 | 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x00, 0x00, |
| 101 | }; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 102 | |
Filip Tehlar | efcad1a | 2020-02-04 09:36:04 +0000 | [diff] [blame] | 103 | u16 min_length = total_len + sizeof (esp_footer_t); |
Damjan Marion | c59b9a2 | 2019-03-19 15:38:40 +0100 | [diff] [blame] | 104 | u16 new_length = round_pow2 (min_length, block_size); |
| 105 | u8 pad_bytes = new_length - min_length; |
Filip Tehlar | e4e8c6b | 2020-02-13 07:49:30 +0000 | [diff] [blame] | 106 | esp_footer_t *f = (esp_footer_t *) (vlib_buffer_get_current (last[0]) + |
| 107 | last[0]->current_length + pad_bytes); |
Filip Tehlar | efcad1a | 2020-02-04 09:36:04 +0000 | [diff] [blame] | 108 | u16 tail_sz = sizeof (esp_footer_t) + pad_bytes + icv_sz; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 109 | |
Filip Tehlar | e4e8c6b | 2020-02-13 07:49:30 +0000 | [diff] [blame] | 110 | if (last[0]->current_length + tail_sz > buffer_data_size) |
Filip Tehlar | 8cdb1a0 | 2019-11-18 22:21:37 +0000 | [diff] [blame] | 111 | { |
Filip Tehlar | e4e8c6b | 2020-02-13 07:49:30 +0000 | [diff] [blame] | 112 | u32 tmp_bi = 0; |
| 113 | if (vlib_buffer_alloc (vm, &tmp_bi, 1) != 1) |
| 114 | return 0; |
Filip Tehlar | c2c1bfd | 2020-02-13 07:49:30 +0000 | [diff] [blame] | 115 | |
Filip Tehlar | e4e8c6b | 2020-02-13 07:49:30 +0000 | [diff] [blame] | 116 | vlib_buffer_t *tmp = vlib_get_buffer (vm, tmp_bi); |
| 117 | last[0]->next_buffer = tmp_bi; |
| 118 | last[0]->flags |= VLIB_BUFFER_NEXT_PRESENT; |
| 119 | f = (esp_footer_t *) (vlib_buffer_get_current (tmp) + pad_bytes); |
| 120 | tmp->current_length += tail_sz; |
| 121 | last[0] = tmp; |
| 122 | } |
| 123 | else |
| 124 | last[0]->current_length += tail_sz; |
| 125 | |
| 126 | f->pad_length = pad_bytes; |
Damjan Marion | c59b9a2 | 2019-03-19 15:38:40 +0100 | [diff] [blame] | 127 | if (pad_bytes) |
Benoît Ganne | 4505f01 | 2019-12-07 09:14:27 -0700 | [diff] [blame] | 128 | { |
| 129 | ASSERT (pad_bytes <= ESP_MAX_BLOCK_SIZE); |
| 130 | pad_bytes = clib_min (ESP_MAX_BLOCK_SIZE, pad_bytes); |
| 131 | clib_memcpy_fast ((u8 *) f - pad_bytes, pad_data, pad_bytes); |
| 132 | } |
Damjan Marion | c59b9a2 | 2019-03-19 15:38:40 +0100 | [diff] [blame] | 133 | |
Damjan Marion | c59b9a2 | 2019-03-19 15:38:40 +0100 | [diff] [blame] | 134 | return &f->next_header; |
| 135 | } |
| 136 | |
| 137 | static_always_inline void |
| 138 | esp_update_ip4_hdr (ip4_header_t * ip4, u16 len, int is_transport, int is_udp) |
| 139 | { |
Neale Ranns | 1b582b8 | 2019-04-18 19:49:13 -0700 | [diff] [blame] | 140 | ip_csum_t sum; |
| 141 | u16 old_len; |
| 142 | |
| 143 | len = clib_net_to_host_u16 (len); |
| 144 | old_len = ip4->length; |
Damjan Marion | c59b9a2 | 2019-03-19 15:38:40 +0100 | [diff] [blame] | 145 | |
| 146 | if (is_transport) |
| 147 | { |
| 148 | u8 prot = is_udp ? IP_PROTOCOL_UDP : IP_PROTOCOL_IPSEC_ESP; |
Damjan Marion | c59b9a2 | 2019-03-19 15:38:40 +0100 | [diff] [blame] | 149 | |
Neale Ranns | 1b582b8 | 2019-04-18 19:49:13 -0700 | [diff] [blame] | 150 | sum = ip_csum_update (ip4->checksum, ip4->protocol, |
| 151 | prot, ip4_header_t, protocol); |
| 152 | ip4->protocol = prot; |
| 153 | |
| 154 | sum = ip_csum_update (sum, old_len, len, ip4_header_t, length); |
| 155 | } |
| 156 | else |
| 157 | sum = ip_csum_update (ip4->checksum, old_len, len, ip4_header_t, length); |
| 158 | |
| 159 | ip4->length = len; |
Damjan Marion | c59b9a2 | 2019-03-19 15:38:40 +0100 | [diff] [blame] | 160 | ip4->checksum = ip_csum_fold (sum); |
| 161 | } |
| 162 | |
| 163 | static_always_inline void |
| 164 | esp_fill_udp_hdr (ipsec_sa_t * sa, udp_header_t * udp, u16 len) |
| 165 | { |
| 166 | clib_memcpy_fast (udp, &sa->udp_hdr, sizeof (udp_header_t)); |
| 167 | udp->length = clib_net_to_host_u16 (len); |
| 168 | } |
| 169 | |
| 170 | static_always_inline u8 |
| 171 | ext_hdr_is_pre_esp (u8 nexthdr) |
| 172 | { |
| 173 | #ifdef CLIB_HAVE_VEC128 |
| 174 | static const u8x16 ext_hdr_types = { |
| 175 | IP_PROTOCOL_IP6_HOP_BY_HOP_OPTIONS, |
| 176 | IP_PROTOCOL_IPV6_ROUTE, |
| 177 | IP_PROTOCOL_IPV6_FRAGMENTATION, |
| 178 | }; |
| 179 | |
| 180 | return !u8x16_is_all_zero (ext_hdr_types == u8x16_splat (nexthdr)); |
| 181 | #else |
| 182 | return ((nexthdr ^ IP_PROTOCOL_IP6_HOP_BY_HOP_OPTIONS) | |
| 183 | (nexthdr ^ IP_PROTOCOL_IPV6_ROUTE) | |
| 184 | (nexthdr ^ IP_PROTOCOL_IPV6_FRAGMENTATION) != 0); |
| 185 | #endif |
| 186 | } |
| 187 | |
| 188 | static_always_inline u8 |
Neale Ranns | 0295040 | 2019-12-20 00:54:57 +0000 | [diff] [blame] | 189 | 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] | 190 | { |
| 191 | /* this code assumes that HbH, route and frag headers will be before |
| 192 | others, if that is not the case, they will end up encrypted */ |
Damjan Marion | c59b9a2 | 2019-03-19 15:38:40 +0100 | [diff] [blame] | 193 | u8 len = sizeof (ip6_header_t); |
| 194 | ip6_ext_header_t *p; |
| 195 | |
| 196 | /* if next packet doesn't have ext header */ |
| 197 | if (ext_hdr_is_pre_esp (ip6->protocol) == 0) |
Neale Ranns | 0295040 | 2019-12-20 00:54:57 +0000 | [diff] [blame] | 198 | { |
| 199 | *ext_hdr = NULL; |
| 200 | return len; |
| 201 | } |
Damjan Marion | c59b9a2 | 2019-03-19 15:38:40 +0100 | [diff] [blame] | 202 | |
| 203 | p = (void *) (ip6 + 1); |
| 204 | len += ip6_ext_header_len (p); |
| 205 | |
| 206 | while (ext_hdr_is_pre_esp (p->next_hdr)) |
| 207 | { |
| 208 | len += ip6_ext_header_len (p); |
| 209 | p = ip6_ext_next_header (p); |
| 210 | } |
| 211 | |
Neale Ranns | 0295040 | 2019-12-20 00:54:57 +0000 | [diff] [blame] | 212 | *ext_hdr = p; |
Damjan Marion | c59b9a2 | 2019-03-19 15:38:40 +0100 | [diff] [blame] | 213 | return len; |
| 214 | } |
| 215 | |
Damjan Marion | c59b9a2 | 2019-03-19 15:38:40 +0100 | [diff] [blame] | 216 | static_always_inline void |
Filip Tehlar | efcad1a | 2020-02-04 09:36:04 +0000 | [diff] [blame] | 217 | esp_process_chained_ops (vlib_main_t * vm, vlib_node_runtime_t * node, |
| 218 | vnet_crypto_op_t * ops, vlib_buffer_t * b[], |
| 219 | u16 * nexts, vnet_crypto_op_chunk_t * chunks) |
| 220 | { |
| 221 | u32 n_fail, n_ops = vec_len (ops); |
| 222 | vnet_crypto_op_t *op = ops; |
| 223 | |
| 224 | if (n_ops == 0) |
| 225 | return; |
| 226 | |
| 227 | n_fail = n_ops - vnet_crypto_process_chained_ops (vm, op, chunks, n_ops); |
| 228 | |
| 229 | while (n_fail) |
| 230 | { |
| 231 | ASSERT (op - ops < n_ops); |
| 232 | |
| 233 | if (op->status != VNET_CRYPTO_OP_STATUS_COMPLETED) |
| 234 | { |
| 235 | u32 bi = op->user_data; |
| 236 | b[bi]->error = node->errors[ESP_ENCRYPT_ERROR_CRYPTO_ENGINE_ERROR]; |
| 237 | nexts[bi] = ESP_ENCRYPT_NEXT_DROP; |
| 238 | n_fail--; |
| 239 | } |
| 240 | op++; |
| 241 | } |
| 242 | } |
| 243 | |
| 244 | static_always_inline void |
Damjan Marion | c59b9a2 | 2019-03-19 15:38:40 +0100 | [diff] [blame] | 245 | esp_process_ops (vlib_main_t * vm, vlib_node_runtime_t * node, |
| 246 | vnet_crypto_op_t * ops, vlib_buffer_t * b[], u16 * nexts) |
| 247 | { |
| 248 | u32 n_fail, n_ops = vec_len (ops); |
| 249 | vnet_crypto_op_t *op = ops; |
| 250 | |
| 251 | if (n_ops == 0) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 252 | return; |
| 253 | |
Damjan Marion | c59b9a2 | 2019-03-19 15:38:40 +0100 | [diff] [blame] | 254 | n_fail = n_ops - vnet_crypto_process_ops (vm, op, n_ops); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 255 | |
Damjan Marion | c59b9a2 | 2019-03-19 15:38:40 +0100 | [diff] [blame] | 256 | while (n_fail) |
| 257 | { |
| 258 | ASSERT (op - ops < n_ops); |
| 259 | |
| 260 | if (op->status != VNET_CRYPTO_OP_STATUS_COMPLETED) |
| 261 | { |
| 262 | u32 bi = op->user_data; |
| 263 | b[bi]->error = node->errors[ESP_ENCRYPT_ERROR_CRYPTO_ENGINE_ERROR]; |
| 264 | nexts[bi] = ESP_ENCRYPT_NEXT_DROP; |
| 265 | n_fail--; |
| 266 | } |
| 267 | op++; |
| 268 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 269 | } |
| 270 | |
Damjan Marion | d97918e | 2019-04-25 18:28:31 +0200 | [diff] [blame] | 271 | typedef struct |
| 272 | { |
| 273 | u32 salt; |
| 274 | u64 iv; |
| 275 | } __clib_packed esp_gcm_nonce_t; |
| 276 | |
| 277 | STATIC_ASSERT_SIZEOF (esp_gcm_nonce_t, 12); |
| 278 | |
Klement Sekera | be5a5dd | 2018-10-09 16:05:48 +0200 | [diff] [blame] | 279 | always_inline uword |
Damjan Marion | c59b9a2 | 2019-03-19 15:38:40 +0100 | [diff] [blame] | 280 | esp_encrypt_inline (vlib_main_t * vm, vlib_node_runtime_t * node, |
Neale Ranns | 25edf14 | 2019-03-22 08:12:48 +0000 | [diff] [blame] | 281 | vlib_frame_t * frame, int is_ip6, int is_tun) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 282 | { |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 283 | ipsec_main_t *im = &ipsec_main; |
Damjan Marion | c59b9a2 | 2019-03-19 15:38:40 +0100 | [diff] [blame] | 284 | ipsec_per_thread_data_t *ptd = vec_elt_at_index (im->ptd, vm->thread_index); |
| 285 | u32 *from = vlib_frame_vector_args (frame); |
| 286 | u32 n_left = frame->n_vectors; |
| 287 | vlib_buffer_t *bufs[VLIB_FRAME_SIZE], **b = bufs; |
Damjan Marion | c98275f | 2019-03-06 14:05:01 +0100 | [diff] [blame] | 288 | u16 nexts[VLIB_FRAME_SIZE], *next = nexts; |
Damjan Marion | d97918e | 2019-04-25 18:28:31 +0200 | [diff] [blame] | 289 | esp_gcm_nonce_t nonces[VLIB_FRAME_SIZE], *nonce = nonces; |
Damjan Marion | c59b9a2 | 2019-03-19 15:38:40 +0100 | [diff] [blame] | 290 | u32 thread_index = vm->thread_index; |
| 291 | u16 buffer_data_size = vlib_buffer_get_default_data_size (vm); |
| 292 | u32 current_sa_index = ~0, current_sa_packets = 0; |
| 293 | u32 current_sa_bytes = 0, spi = 0; |
| 294 | u8 block_sz = 0, iv_sz = 0, icv_sz = 0; |
| 295 | ipsec_sa_t *sa0 = 0; |
Filip Tehlar | efcad1a | 2020-02-04 09:36:04 +0000 | [diff] [blame] | 296 | vnet_crypto_op_chunk_t *ch; |
| 297 | vlib_buffer_t *lb; |
| 298 | vnet_crypto_op_t **crypto_ops = &ptd->crypto_ops; |
| 299 | vnet_crypto_op_t **integ_ops = &ptd->integ_ops; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 300 | |
Damjan Marion | c59b9a2 | 2019-03-19 15:38:40 +0100 | [diff] [blame] | 301 | vlib_get_buffers (vm, from, b, n_left); |
| 302 | vec_reset_length (ptd->crypto_ops); |
| 303 | vec_reset_length (ptd->integ_ops); |
Filip Tehlar | efcad1a | 2020-02-04 09:36:04 +0000 | [diff] [blame] | 304 | vec_reset_length (ptd->chained_crypto_ops); |
| 305 | vec_reset_length (ptd->chained_integ_ops); |
| 306 | vec_reset_length (ptd->chunks); |
Damjan Marion | c59b9a2 | 2019-03-19 15:38:40 +0100 | [diff] [blame] | 307 | |
| 308 | while (n_left > 0) |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 309 | { |
Zhiyong Yang | 1cff643 | 2019-04-30 05:33:53 -0400 | [diff] [blame] | 310 | u32 sa_index0; |
Damjan Marion | c59b9a2 | 2019-03-19 15:38:40 +0100 | [diff] [blame] | 311 | dpo_id_t *dpo; |
| 312 | esp_header_t *esp; |
| 313 | u8 *payload, *next_hdr_ptr; |
Filip Tehlar | efcad1a | 2020-02-04 09:36:04 +0000 | [diff] [blame] | 314 | u16 payload_len, payload_len_total, n_bufs; |
Neale Ranns | f62a8c0 | 2019-04-02 08:13:33 +0000 | [diff] [blame] | 315 | u32 hdr_len, config_index; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 316 | |
Damjan Marion | c59b9a2 | 2019-03-19 15:38:40 +0100 | [diff] [blame] | 317 | if (n_left > 2) |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 318 | { |
Damjan Marion | c59b9a2 | 2019-03-19 15:38:40 +0100 | [diff] [blame] | 319 | u8 *p; |
| 320 | vlib_prefetch_buffer_header (b[2], LOAD); |
| 321 | p = vlib_buffer_get_current (b[1]); |
| 322 | CLIB_PREFETCH (p, CLIB_CACHE_LINE_BYTES, LOAD); |
| 323 | p -= CLIB_CACHE_LINE_BYTES; |
| 324 | CLIB_PREFETCH (p, CLIB_CACHE_LINE_BYTES, LOAD); |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 325 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 326 | |
Neale Ranns | 25edf14 | 2019-03-22 08:12:48 +0000 | [diff] [blame] | 327 | if (is_tun) |
| 328 | { |
| 329 | /* we are on a ipsec tunnel's feature arc */ |
Neale Ranns | f62a8c0 | 2019-04-02 08:13:33 +0000 | [diff] [blame] | 330 | config_index = b[0]->current_config_index; |
Neale Ranns | 5cd31ec | 2020-02-24 12:54:31 +0000 | [diff] [blame] | 331 | vnet_feature_next_u16 (&next[0], b[0]); |
Neale Ranns | 2828721 | 2019-12-16 00:53:11 +0000 | [diff] [blame] | 332 | vnet_buffer (b[0])->ipsec.sad_index = |
| 333 | sa_index0 = ipsec_tun_protect_get_sa_out |
| 334 | (vnet_buffer (b[0])->ip.adj_index[VLIB_TX]); |
Neale Ranns | 25edf14 | 2019-03-22 08:12:48 +0000 | [diff] [blame] | 335 | } |
| 336 | else |
| 337 | sa_index0 = vnet_buffer (b[0])->ipsec.sad_index; |
| 338 | |
| 339 | if (sa_index0 != current_sa_index) |
Damjan Marion | c59b9a2 | 2019-03-19 15:38:40 +0100 | [diff] [blame] | 340 | { |
Damjan Marion | 21f265f | 2019-06-05 15:45:50 +0200 | [diff] [blame] | 341 | if (current_sa_packets) |
| 342 | vlib_increment_combined_counter (&ipsec_sa_counters, thread_index, |
| 343 | current_sa_index, |
| 344 | current_sa_packets, |
| 345 | current_sa_bytes); |
| 346 | current_sa_packets = current_sa_bytes = 0; |
| 347 | |
Damjan Marion | c59b9a2 | 2019-03-19 15:38:40 +0100 | [diff] [blame] | 348 | sa0 = pool_elt_at_index (im->sad, sa_index0); |
| 349 | current_sa_index = sa_index0; |
Damjan Marion | c59b9a2 | 2019-03-19 15:38:40 +0100 | [diff] [blame] | 350 | spi = clib_net_to_host_u32 (sa0->spi); |
| 351 | block_sz = sa0->crypto_block_size; |
Damjan Marion | 7c22ff7 | 2019-04-04 12:25:44 +0200 | [diff] [blame] | 352 | icv_sz = sa0->integ_icv_size; |
Damjan Marion | c59b9a2 | 2019-03-19 15:38:40 +0100 | [diff] [blame] | 353 | iv_sz = sa0->crypto_iv_size; |
| 354 | } |
| 355 | |
Neale Ranns | f62a8c0 | 2019-04-02 08:13:33 +0000 | [diff] [blame] | 356 | if (PREDICT_FALSE (~0 == sa0->encrypt_thread_index)) |
| 357 | { |
| 358 | /* this is the first packet to use this SA, claim the SA |
| 359 | * for this thread. this could happen simultaneously on |
| 360 | * another thread */ |
| 361 | clib_atomic_cmp_and_swap (&sa0->encrypt_thread_index, ~0, |
| 362 | ipsec_sa_assign_thread (thread_index)); |
| 363 | } |
| 364 | |
| 365 | if (PREDICT_TRUE (thread_index != sa0->encrypt_thread_index)) |
| 366 | { |
| 367 | next[0] = ESP_ENCRYPT_NEXT_HANDOFF; |
| 368 | if (is_tun) |
| 369 | { |
| 370 | b[0]->current_config_index = config_index; |
| 371 | } |
| 372 | goto trace; |
| 373 | } |
| 374 | |
Filip Tehlar | efcad1a | 2020-02-04 09:36:04 +0000 | [diff] [blame] | 375 | lb = b[0]; |
| 376 | n_bufs = vlib_buffer_chain_linearize (vm, b[0]); |
| 377 | if (n_bufs == 0) |
Damjan Marion | c59b9a2 | 2019-03-19 15:38:40 +0100 | [diff] [blame] | 378 | { |
Filip Tehlar | efcad1a | 2020-02-04 09:36:04 +0000 | [diff] [blame] | 379 | b[0]->error = node->errors[ESP_ENCRYPT_ERROR_NO_BUFFERS]; |
Damjan Marion | c59b9a2 | 2019-03-19 15:38:40 +0100 | [diff] [blame] | 380 | next[0] = ESP_ENCRYPT_NEXT_DROP; |
| 381 | goto trace; |
| 382 | } |
| 383 | |
Filip Tehlar | efcad1a | 2020-02-04 09:36:04 +0000 | [diff] [blame] | 384 | if (n_bufs > 1) |
| 385 | { |
| 386 | crypto_ops = &ptd->chained_crypto_ops; |
| 387 | integ_ops = &ptd->chained_integ_ops; |
| 388 | |
| 389 | /* find last buffer in the chain */ |
| 390 | while (lb->flags & VLIB_BUFFER_NEXT_PRESENT) |
| 391 | lb = vlib_get_buffer (vm, lb->next_buffer); |
| 392 | } |
| 393 | else |
| 394 | { |
| 395 | crypto_ops = &ptd->crypto_ops; |
| 396 | integ_ops = &ptd->integ_ops; |
| 397 | } |
| 398 | |
Damjan Marion | c59b9a2 | 2019-03-19 15:38:40 +0100 | [diff] [blame] | 399 | if (PREDICT_FALSE (esp_seq_advance (sa0))) |
| 400 | { |
| 401 | b[0]->error = node->errors[ESP_ENCRYPT_ERROR_SEQ_CYCLED]; |
| 402 | next[0] = ESP_ENCRYPT_NEXT_DROP; |
| 403 | goto trace; |
| 404 | } |
| 405 | |
| 406 | /* space for IV */ |
| 407 | hdr_len = iv_sz; |
| 408 | |
Damjan Marion | d709cbc | 2019-03-26 13:16:42 +0100 | [diff] [blame] | 409 | if (ipsec_sa_is_set_IS_TUNNEL (sa0)) |
Damjan Marion | c59b9a2 | 2019-03-19 15:38:40 +0100 | [diff] [blame] | 410 | { |
| 411 | payload = vlib_buffer_get_current (b[0]); |
Filip Tehlar | e4e8c6b | 2020-02-13 07:49:30 +0000 | [diff] [blame] | 412 | next_hdr_ptr = esp_add_footer_and_icv (vm, &lb, block_sz, icv_sz, |
Filip Tehlar | 8cdb1a0 | 2019-11-18 22:21:37 +0000 | [diff] [blame] | 413 | next, node, |
Filip Tehlar | efcad1a | 2020-02-04 09:36:04 +0000 | [diff] [blame] | 414 | buffer_data_size, |
| 415 | vlib_buffer_length_in_chain |
| 416 | (vm, b[0])); |
Filip Tehlar | 8cdb1a0 | 2019-11-18 22:21:37 +0000 | [diff] [blame] | 417 | if (!next_hdr_ptr) |
Filip Tehlar | e4e8c6b | 2020-02-13 07:49:30 +0000 | [diff] [blame] | 418 | { |
| 419 | b[0]->error = node->errors[ESP_ENCRYPT_ERROR_NO_BUFFERS]; |
| 420 | next[0] = ESP_ENCRYPT_NEXT_DROP; |
| 421 | goto trace; |
| 422 | } |
Filip Tehlar | efcad1a | 2020-02-04 09:36:04 +0000 | [diff] [blame] | 423 | b[0]->flags &= ~VLIB_BUFFER_TOTAL_LENGTH_VALID; |
Filip Tehlar | 8cdb1a0 | 2019-11-18 22:21:37 +0000 | [diff] [blame] | 424 | payload_len = b[0]->current_length; |
Filip Tehlar | efcad1a | 2020-02-04 09:36:04 +0000 | [diff] [blame] | 425 | payload_len_total = vlib_buffer_length_in_chain (vm, b[0]); |
Damjan Marion | c59b9a2 | 2019-03-19 15:38:40 +0100 | [diff] [blame] | 426 | |
| 427 | /* ESP header */ |
| 428 | hdr_len += sizeof (*esp); |
| 429 | esp = (esp_header_t *) (payload - hdr_len); |
| 430 | |
| 431 | /* optional UDP header */ |
Damjan Marion | d709cbc | 2019-03-26 13:16:42 +0100 | [diff] [blame] | 432 | if (ipsec_sa_is_set_UDP_ENCAP (sa0)) |
Damjan Marion | c98275f | 2019-03-06 14:05:01 +0100 | [diff] [blame] | 433 | { |
Damjan Marion | c59b9a2 | 2019-03-19 15:38:40 +0100 | [diff] [blame] | 434 | hdr_len += sizeof (udp_header_t); |
| 435 | esp_fill_udp_hdr (sa0, (udp_header_t *) (payload - hdr_len), |
Filip Tehlar | efcad1a | 2020-02-04 09:36:04 +0000 | [diff] [blame] | 436 | payload_len_total + hdr_len); |
Damjan Marion | c59b9a2 | 2019-03-19 15:38:40 +0100 | [diff] [blame] | 437 | } |
| 438 | |
| 439 | /* IP header */ |
Damjan Marion | d709cbc | 2019-03-26 13:16:42 +0100 | [diff] [blame] | 440 | if (ipsec_sa_is_set_IS_TUNNEL_V6 (sa0)) |
Damjan Marion | c59b9a2 | 2019-03-19 15:38:40 +0100 | [diff] [blame] | 441 | { |
| 442 | ip6_header_t *ip6; |
| 443 | u16 len = sizeof (ip6_header_t); |
| 444 | hdr_len += len; |
| 445 | ip6 = (ip6_header_t *) (payload - hdr_len); |
| 446 | clib_memcpy_fast (ip6, &sa0->ip6_hdr, len); |
Neale Ranns | 987aea8 | 2019-03-27 13:40:35 +0000 | [diff] [blame] | 447 | *next_hdr_ptr = (is_ip6 ? |
| 448 | IP_PROTOCOL_IPV6 : IP_PROTOCOL_IP_IN_IP); |
Filip Tehlar | efcad1a | 2020-02-04 09:36:04 +0000 | [diff] [blame] | 449 | len = payload_len_total + hdr_len - len; |
Damjan Marion | c59b9a2 | 2019-03-19 15:38:40 +0100 | [diff] [blame] | 450 | ip6->payload_length = clib_net_to_host_u16 (len); |
Damjan Marion | c98275f | 2019-03-06 14:05:01 +0100 | [diff] [blame] | 451 | } |
| 452 | else |
| 453 | { |
Damjan Marion | c59b9a2 | 2019-03-19 15:38:40 +0100 | [diff] [blame] | 454 | ip4_header_t *ip4; |
| 455 | u16 len = sizeof (ip4_header_t); |
| 456 | hdr_len += len; |
| 457 | ip4 = (ip4_header_t *) (payload - hdr_len); |
| 458 | clib_memcpy_fast (ip4, &sa0->ip4_hdr, len); |
Neale Ranns | 987aea8 | 2019-03-27 13:40:35 +0000 | [diff] [blame] | 459 | *next_hdr_ptr = (is_ip6 ? |
| 460 | IP_PROTOCOL_IPV6 : IP_PROTOCOL_IP_IN_IP); |
Filip Tehlar | efcad1a | 2020-02-04 09:36:04 +0000 | [diff] [blame] | 461 | len = payload_len_total + hdr_len; |
Damjan Marion | c59b9a2 | 2019-03-19 15:38:40 +0100 | [diff] [blame] | 462 | esp_update_ip4_hdr (ip4, len, /* is_transport */ 0, 0); |
Damjan Marion | c98275f | 2019-03-06 14:05:01 +0100 | [diff] [blame] | 463 | } |
Damjan Marion | c98275f | 2019-03-06 14:05:01 +0100 | [diff] [blame] | 464 | |
Neale Ranns | 72f2a3a | 2019-06-17 15:43:38 +0000 | [diff] [blame] | 465 | dpo = &sa0->dpo; |
Neale Ranns | 25edf14 | 2019-03-22 08:12:48 +0000 | [diff] [blame] | 466 | if (!is_tun) |
| 467 | { |
| 468 | next[0] = dpo->dpoi_next_node; |
| 469 | vnet_buffer (b[0])->ip.adj_index[VLIB_TX] = dpo->dpoi_index; |
| 470 | } |
Damjan Marion | c98275f | 2019-03-06 14:05:01 +0100 | [diff] [blame] | 471 | } |
Damjan Marion | c59b9a2 | 2019-03-19 15:38:40 +0100 | [diff] [blame] | 472 | else /* transport mode */ |
Damjan Marion | c98275f | 2019-03-06 14:05:01 +0100 | [diff] [blame] | 473 | { |
Damjan Marion | c59b9a2 | 2019-03-19 15:38:40 +0100 | [diff] [blame] | 474 | u8 *l2_hdr, l2_len, *ip_hdr, ip_len; |
Neale Ranns | 0295040 | 2019-12-20 00:54:57 +0000 | [diff] [blame] | 475 | ip6_ext_header_t *ext_hdr; |
Damjan Marion | c59b9a2 | 2019-03-19 15:38:40 +0100 | [diff] [blame] | 476 | udp_header_t *udp = 0; |
Alexander Chernavin | b0d2eda | 2020-03-25 10:56:52 -0400 | [diff] [blame] | 477 | u16 udp_len = 0; |
Damjan Marion | c59b9a2 | 2019-03-19 15:38:40 +0100 | [diff] [blame] | 478 | u8 *old_ip_hdr = vlib_buffer_get_current (b[0]); |
Damjan Marion | c98275f | 2019-03-06 14:05:01 +0100 | [diff] [blame] | 479 | |
Damjan Marion | c59b9a2 | 2019-03-19 15:38:40 +0100 | [diff] [blame] | 480 | ip_len = is_ip6 ? |
Neale Ranns | 0295040 | 2019-12-20 00:54:57 +0000 | [diff] [blame] | 481 | 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] | 482 | ip4_header_bytes ((ip4_header_t *) old_ip_hdr); |
Damjan Marion | c98275f | 2019-03-06 14:05:01 +0100 | [diff] [blame] | 483 | |
Damjan Marion | c59b9a2 | 2019-03-19 15:38:40 +0100 | [diff] [blame] | 484 | vlib_buffer_advance (b[0], ip_len); |
| 485 | payload = vlib_buffer_get_current (b[0]); |
Filip Tehlar | e4e8c6b | 2020-02-13 07:49:30 +0000 | [diff] [blame] | 486 | next_hdr_ptr = esp_add_footer_and_icv (vm, &lb, block_sz, icv_sz, |
Filip Tehlar | 8cdb1a0 | 2019-11-18 22:21:37 +0000 | [diff] [blame] | 487 | next, node, |
Filip Tehlar | efcad1a | 2020-02-04 09:36:04 +0000 | [diff] [blame] | 488 | buffer_data_size, |
| 489 | vlib_buffer_length_in_chain |
| 490 | (vm, b[0])); |
Filip Tehlar | 8cdb1a0 | 2019-11-18 22:21:37 +0000 | [diff] [blame] | 491 | if (!next_hdr_ptr) |
Damjan Marion | c59b9a2 | 2019-03-19 15:38:40 +0100 | [diff] [blame] | 492 | goto trace; |
Filip Tehlar | efcad1a | 2020-02-04 09:36:04 +0000 | [diff] [blame] | 493 | |
| 494 | b[0]->flags &= ~VLIB_BUFFER_TOTAL_LENGTH_VALID; |
Filip Tehlar | 8cdb1a0 | 2019-11-18 22:21:37 +0000 | [diff] [blame] | 495 | payload_len = b[0]->current_length; |
Filip Tehlar | efcad1a | 2020-02-04 09:36:04 +0000 | [diff] [blame] | 496 | payload_len_total = vlib_buffer_length_in_chain (vm, b[0]); |
Damjan Marion | c59b9a2 | 2019-03-19 15:38:40 +0100 | [diff] [blame] | 497 | |
| 498 | /* ESP header */ |
| 499 | hdr_len += sizeof (*esp); |
| 500 | esp = (esp_header_t *) (payload - hdr_len); |
| 501 | |
| 502 | /* optional UDP header */ |
Damjan Marion | d709cbc | 2019-03-26 13:16:42 +0100 | [diff] [blame] | 503 | if (ipsec_sa_is_set_UDP_ENCAP (sa0)) |
Damjan Marion | c98275f | 2019-03-06 14:05:01 +0100 | [diff] [blame] | 504 | { |
Damjan Marion | c59b9a2 | 2019-03-19 15:38:40 +0100 | [diff] [blame] | 505 | hdr_len += sizeof (udp_header_t); |
| 506 | udp = (udp_header_t *) (payload - hdr_len); |
Damjan Marion | c98275f | 2019-03-06 14:05:01 +0100 | [diff] [blame] | 507 | } |
| 508 | |
Damjan Marion | c59b9a2 | 2019-03-19 15:38:40 +0100 | [diff] [blame] | 509 | /* IP header */ |
| 510 | hdr_len += ip_len; |
| 511 | ip_hdr = payload - hdr_len; |
| 512 | |
| 513 | /* L2 header */ |
Neale Ranns | c87b66c | 2019-02-07 07:26:12 -0800 | [diff] [blame] | 514 | if (!is_tun) |
| 515 | { |
| 516 | l2_len = vnet_buffer (b[0])->ip.save_rewrite_length; |
| 517 | hdr_len += l2_len; |
| 518 | l2_hdr = payload - hdr_len; |
Damjan Marion | c59b9a2 | 2019-03-19 15:38:40 +0100 | [diff] [blame] | 519 | |
Neale Ranns | c87b66c | 2019-02-07 07:26:12 -0800 | [diff] [blame] | 520 | /* copy l2 and ip header */ |
| 521 | clib_memcpy_le32 (l2_hdr, old_ip_hdr - l2_len, l2_len); |
| 522 | } |
| 523 | else |
| 524 | l2_len = 0; |
| 525 | |
Damjan Marion | c98275f | 2019-03-06 14:05:01 +0100 | [diff] [blame] | 526 | if (is_ip6) |
Damjan Marion | c59b9a2 | 2019-03-19 15:38:40 +0100 | [diff] [blame] | 527 | { |
Neale Ranns | 0295040 | 2019-12-20 00:54:57 +0000 | [diff] [blame] | 528 | ip6_header_t *ip6 = (ip6_header_t *) (old_ip_hdr); |
| 529 | if (PREDICT_TRUE (NULL == ext_hdr)) |
| 530 | { |
| 531 | *next_hdr_ptr = ip6->protocol; |
| 532 | ip6->protocol = IP_PROTOCOL_IPSEC_ESP; |
| 533 | } |
| 534 | else |
| 535 | { |
| 536 | *next_hdr_ptr = ext_hdr->next_hdr; |
| 537 | ext_hdr->next_hdr = IP_PROTOCOL_IPSEC_ESP; |
| 538 | } |
Neale Ranns | d207fd7 | 2019-04-18 17:18:12 -0700 | [diff] [blame] | 539 | ip6->payload_length = |
Filip Tehlar | efcad1a | 2020-02-04 09:36:04 +0000 | [diff] [blame] | 540 | clib_host_to_net_u16 (payload_len_total + hdr_len - l2_len - |
Neale Ranns | 0295040 | 2019-12-20 00:54:57 +0000 | [diff] [blame] | 541 | sizeof (ip6_header_t)); |
Damjan Marion | c59b9a2 | 2019-03-19 15:38:40 +0100 | [diff] [blame] | 542 | } |
Damjan Marion | c98275f | 2019-03-06 14:05:01 +0100 | [diff] [blame] | 543 | else |
Damjan Marion | c98275f | 2019-03-06 14:05:01 +0100 | [diff] [blame] | 544 | { |
Damjan Marion | c59b9a2 | 2019-03-19 15:38:40 +0100 | [diff] [blame] | 545 | u16 len; |
Neale Ranns | 0295040 | 2019-12-20 00:54:57 +0000 | [diff] [blame] | 546 | ip4_header_t *ip4 = (ip4_header_t *) (old_ip_hdr); |
Damjan Marion | c59b9a2 | 2019-03-19 15:38:40 +0100 | [diff] [blame] | 547 | *next_hdr_ptr = ip4->protocol; |
Filip Tehlar | efcad1a | 2020-02-04 09:36:04 +0000 | [diff] [blame] | 548 | len = payload_len_total + hdr_len - l2_len; |
Damjan Marion | c59b9a2 | 2019-03-19 15:38:40 +0100 | [diff] [blame] | 549 | if (udp) |
| 550 | { |
| 551 | esp_update_ip4_hdr (ip4, len, /* is_transport */ 1, 1); |
Alexander Chernavin | b0d2eda | 2020-03-25 10:56:52 -0400 | [diff] [blame] | 552 | udp_len = len - ip_len; |
Damjan Marion | c59b9a2 | 2019-03-19 15:38:40 +0100 | [diff] [blame] | 553 | } |
| 554 | else |
| 555 | esp_update_ip4_hdr (ip4, len, /* is_transport */ 1, 0); |
Damjan Marion | c98275f | 2019-03-06 14:05:01 +0100 | [diff] [blame] | 556 | } |
Damjan Marion | c98275f | 2019-03-06 14:05:01 +0100 | [diff] [blame] | 557 | |
Neale Ranns | 0295040 | 2019-12-20 00:54:57 +0000 | [diff] [blame] | 558 | clib_memcpy_le64 (ip_hdr, old_ip_hdr, ip_len); |
| 559 | |
Alexander Chernavin | b0d2eda | 2020-03-25 10:56:52 -0400 | [diff] [blame] | 560 | if (udp) |
| 561 | { |
| 562 | esp_fill_udp_hdr (sa0, udp, udp_len); |
| 563 | } |
| 564 | |
Neale Ranns | c87b66c | 2019-02-07 07:26:12 -0800 | [diff] [blame] | 565 | if (!is_tun) |
| 566 | next[0] = ESP_ENCRYPT_NEXT_INTERFACE_OUTPUT; |
Damjan Marion | c98275f | 2019-03-06 14:05:01 +0100 | [diff] [blame] | 567 | } |
| 568 | |
Damjan Marion | c59b9a2 | 2019-03-19 15:38:40 +0100 | [diff] [blame] | 569 | esp->spi = spi; |
| 570 | esp->seq = clib_net_to_host_u32 (sa0->seq); |
Damjan Marion | c98275f | 2019-03-06 14:05:01 +0100 | [diff] [blame] | 571 | |
Damjan Marion | 060bfb9 | 2019-03-29 13:47:54 +0100 | [diff] [blame] | 572 | if (sa0->crypto_enc_op_id) |
Damjan Marion | c98275f | 2019-03-06 14:05:01 +0100 | [diff] [blame] | 573 | { |
Damjan Marion | c59b9a2 | 2019-03-19 15:38:40 +0100 | [diff] [blame] | 574 | vnet_crypto_op_t *op; |
Filip Tehlar | efcad1a | 2020-02-04 09:36:04 +0000 | [diff] [blame] | 575 | vec_add2_aligned (crypto_ops[0], op, 1, CLIB_CACHE_LINE_BYTES); |
Damjan Marion | 060bfb9 | 2019-03-29 13:47:54 +0100 | [diff] [blame] | 576 | vnet_crypto_op_init (op, sa0->crypto_enc_op_id); |
Filip Tehlar | efcad1a | 2020-02-04 09:36:04 +0000 | [diff] [blame] | 577 | |
Damjan Marion | c59b9a2 | 2019-03-19 15:38:40 +0100 | [diff] [blame] | 578 | op->src = op->dst = payload; |
Damjan Marion | d1bed68 | 2019-04-24 15:20:35 +0200 | [diff] [blame] | 579 | op->key_index = sa0->crypto_key_index; |
Damjan Marion | c59b9a2 | 2019-03-19 15:38:40 +0100 | [diff] [blame] | 580 | op->len = payload_len - icv_sz; |
Damjan Marion | c59b9a2 | 2019-03-19 15:38:40 +0100 | [diff] [blame] | 581 | op->user_data = b - bufs; |
Neale Ranns | 47feb11 | 2019-04-11 15:14:07 +0000 | [diff] [blame] | 582 | |
| 583 | if (ipsec_sa_is_set_IS_AEAD (sa0)) |
| 584 | { |
| 585 | /* |
| 586 | * construct the AAD in a scratch space in front |
| 587 | * of the IP header. |
| 588 | */ |
| 589 | op->aad = payload - hdr_len - sizeof (esp_aead_t); |
| 590 | |
| 591 | esp_aad_fill (op, esp, sa0); |
| 592 | |
| 593 | op->tag = payload + op->len; |
| 594 | op->tag_len = 16; |
Damjan Marion | d97918e | 2019-04-25 18:28:31 +0200 | [diff] [blame] | 595 | |
| 596 | u64 *iv = (u64 *) (payload - iv_sz); |
| 597 | nonce->salt = sa0->salt; |
| 598 | nonce->iv = *iv = clib_host_to_net_u64 (sa0->gcm_iv_counter++); |
| 599 | op->iv = (u8 *) nonce; |
| 600 | nonce++; |
| 601 | } |
| 602 | else |
| 603 | { |
| 604 | op->iv = payload - iv_sz; |
| 605 | op->flags = VNET_CRYPTO_OP_FLAG_INIT_IV; |
Neale Ranns | 47feb11 | 2019-04-11 15:14:07 +0000 | [diff] [blame] | 606 | } |
Filip Tehlar | efcad1a | 2020-02-04 09:36:04 +0000 | [diff] [blame] | 607 | |
| 608 | if (lb != b[0]) |
| 609 | { |
| 610 | /* is chained */ |
| 611 | vlib_buffer_t *cb = b[0]; |
| 612 | op->flags |= VNET_CRYPTO_OP_FLAG_CHAINED_BUFFERS; |
| 613 | op->chunk_index = vec_len (ptd->chunks); |
| 614 | op->tag = vlib_buffer_get_tail (lb) - icv_sz; |
| 615 | vec_add2 (ptd->chunks, ch, 1); |
| 616 | ch->len = payload_len; |
| 617 | ch->src = ch->dst = payload; |
| 618 | cb = vlib_get_buffer (vm, cb->next_buffer); |
| 619 | op->n_chunks = 1; |
| 620 | |
| 621 | while (1) |
| 622 | { |
| 623 | vec_add2 (ptd->chunks, ch, 1); |
| 624 | op->n_chunks += 1; |
| 625 | if (lb == cb) |
| 626 | ch->len = cb->current_length - icv_sz; |
| 627 | else |
| 628 | ch->len = cb->current_length; |
| 629 | ch->src = ch->dst = vlib_buffer_get_current (cb); |
| 630 | |
| 631 | if (!(cb->flags & VLIB_BUFFER_NEXT_PRESENT)) |
| 632 | break; |
| 633 | |
| 634 | cb = vlib_get_buffer (vm, cb->next_buffer); |
| 635 | } |
| 636 | } |
Damjan Marion | c98275f | 2019-03-06 14:05:01 +0100 | [diff] [blame] | 637 | } |
Damjan Marion | c59b9a2 | 2019-03-19 15:38:40 +0100 | [diff] [blame] | 638 | |
Damjan Marion | 060bfb9 | 2019-03-29 13:47:54 +0100 | [diff] [blame] | 639 | if (sa0->integ_op_id) |
Damjan Marion | c98275f | 2019-03-06 14:05:01 +0100 | [diff] [blame] | 640 | { |
Damjan Marion | c59b9a2 | 2019-03-19 15:38:40 +0100 | [diff] [blame] | 641 | vnet_crypto_op_t *op; |
Filip Tehlar | efcad1a | 2020-02-04 09:36:04 +0000 | [diff] [blame] | 642 | vec_add2_aligned (integ_ops[0], op, 1, CLIB_CACHE_LINE_BYTES); |
Damjan Marion | 060bfb9 | 2019-03-29 13:47:54 +0100 | [diff] [blame] | 643 | vnet_crypto_op_init (op, sa0->integ_op_id); |
Damjan Marion | c59b9a2 | 2019-03-19 15:38:40 +0100 | [diff] [blame] | 644 | op->src = payload - iv_sz - sizeof (esp_header_t); |
Damjan Marion | 060bfb9 | 2019-03-29 13:47:54 +0100 | [diff] [blame] | 645 | op->digest = payload + payload_len - icv_sz; |
Damjan Marion | d1bed68 | 2019-04-24 15:20:35 +0200 | [diff] [blame] | 646 | op->key_index = sa0->integ_key_index; |
Damjan Marion | 060bfb9 | 2019-03-29 13:47:54 +0100 | [diff] [blame] | 647 | op->digest_len = icv_sz; |
Damjan Marion | c59b9a2 | 2019-03-19 15:38:40 +0100 | [diff] [blame] | 648 | op->len = payload_len - icv_sz + iv_sz + sizeof (esp_header_t); |
Damjan Marion | c59b9a2 | 2019-03-19 15:38:40 +0100 | [diff] [blame] | 649 | op->user_data = b - bufs; |
Filip Tehlar | efcad1a | 2020-02-04 09:36:04 +0000 | [diff] [blame] | 650 | |
| 651 | if (lb != b[0]) |
| 652 | { |
| 653 | /* is chained */ |
| 654 | op->flags |= VNET_CRYPTO_OP_FLAG_CHAINED_BUFFERS; |
| 655 | vlib_buffer_t *cb = b[0]; |
| 656 | op->chunk_index = vec_len (ptd->chunks); |
| 657 | op->digest = vlib_buffer_get_tail (lb) - icv_sz; |
| 658 | vec_add2 (ptd->chunks, ch, 1); |
| 659 | ch->len = payload_len + iv_sz + sizeof (esp_header_t); |
| 660 | ch->src = payload - iv_sz - sizeof (esp_header_t); |
| 661 | cb = vlib_get_buffer (vm, cb->next_buffer); |
| 662 | op->n_chunks = 1; |
| 663 | |
| 664 | while (1) |
| 665 | { |
| 666 | vec_add2 (ptd->chunks, ch, 1); |
| 667 | op->n_chunks += 1; |
| 668 | if (lb == cb) |
| 669 | { |
| 670 | ch->len = cb->current_length - icv_sz; |
| 671 | if (ipsec_sa_is_set_USE_ESN (sa0)) |
| 672 | { |
| 673 | u32 seq_hi = clib_net_to_host_u32 (sa0->seq_hi); |
| 674 | clib_memcpy_fast (op->digest, &seq_hi, |
| 675 | sizeof (seq_hi)); |
| 676 | ch->len += sizeof (seq_hi); |
| 677 | } |
| 678 | } |
| 679 | else |
| 680 | ch->len = cb->current_length; |
| 681 | ch->src = vlib_buffer_get_current (cb); |
| 682 | |
| 683 | if (!(cb->flags & VLIB_BUFFER_NEXT_PRESENT)) |
| 684 | break; |
| 685 | |
| 686 | cb = vlib_get_buffer (vm, cb->next_buffer); |
| 687 | } |
| 688 | } |
| 689 | else if (ipsec_sa_is_set_USE_ESN (sa0)) |
Damjan Marion | c98275f | 2019-03-06 14:05:01 +0100 | [diff] [blame] | 690 | { |
Damjan Marion | c59b9a2 | 2019-03-19 15:38:40 +0100 | [diff] [blame] | 691 | u32 seq_hi = clib_net_to_host_u32 (sa0->seq_hi); |
Neale Ranns | 49e7ef6 | 2019-04-10 17:24:29 +0000 | [diff] [blame] | 692 | clib_memcpy_fast (op->digest, &seq_hi, sizeof (seq_hi)); |
Damjan Marion | c59b9a2 | 2019-03-19 15:38:40 +0100 | [diff] [blame] | 693 | op->len += sizeof (seq_hi); |
Damjan Marion | c98275f | 2019-03-06 14:05:01 +0100 | [diff] [blame] | 694 | } |
| 695 | } |
| 696 | |
Damjan Marion | c59b9a2 | 2019-03-19 15:38:40 +0100 | [diff] [blame] | 697 | vlib_buffer_advance (b[0], 0LL - hdr_len); |
Damjan Marion | c98275f | 2019-03-06 14:05:01 +0100 | [diff] [blame] | 698 | |
Damjan Marion | c59b9a2 | 2019-03-19 15:38:40 +0100 | [diff] [blame] | 699 | current_sa_packets += 1; |
Filip Tehlar | efcad1a | 2020-02-04 09:36:04 +0000 | [diff] [blame] | 700 | current_sa_bytes += payload_len_total; |
Damjan Marion | c59b9a2 | 2019-03-19 15:38:40 +0100 | [diff] [blame] | 701 | |
| 702 | trace: |
| 703 | if (PREDICT_FALSE (b[0]->flags & VLIB_BUFFER_IS_TRACED)) |
Damjan Marion | c98275f | 2019-03-06 14:05:01 +0100 | [diff] [blame] | 704 | { |
Damjan Marion | c59b9a2 | 2019-03-19 15:38:40 +0100 | [diff] [blame] | 705 | esp_encrypt_trace_t *tr = vlib_add_trace (vm, node, b[0], |
| 706 | sizeof (*tr)); |
| 707 | tr->sa_index = sa_index0; |
| 708 | tr->spi = sa0->spi; |
Neale Ranns | 6afaae1 | 2019-07-17 15:07:14 +0000 | [diff] [blame] | 709 | tr->seq = sa0->seq; |
| 710 | tr->sa_seq_hi = sa0->seq_hi; |
Damjan Marion | d709cbc | 2019-03-26 13:16:42 +0100 | [diff] [blame] | 711 | tr->udp_encap = ipsec_sa_is_set_UDP_ENCAP (sa0); |
Damjan Marion | c59b9a2 | 2019-03-19 15:38:40 +0100 | [diff] [blame] | 712 | tr->crypto_alg = sa0->crypto_alg; |
| 713 | tr->integ_alg = sa0->integ_alg; |
Damjan Marion | c98275f | 2019-03-06 14:05:01 +0100 | [diff] [blame] | 714 | } |
Damjan Marion | c98275f | 2019-03-06 14:05:01 +0100 | [diff] [blame] | 715 | /* next */ |
Damjan Marion | c59b9a2 | 2019-03-19 15:38:40 +0100 | [diff] [blame] | 716 | n_left -= 1; |
Damjan Marion | c98275f | 2019-03-06 14:05:01 +0100 | [diff] [blame] | 717 | next += 1; |
Damjan Marion | c59b9a2 | 2019-03-19 15:38:40 +0100 | [diff] [blame] | 718 | b += 1; |
Damjan Marion | c98275f | 2019-03-06 14:05:01 +0100 | [diff] [blame] | 719 | } |
| 720 | |
Damjan Marion | c59b9a2 | 2019-03-19 15:38:40 +0100 | [diff] [blame] | 721 | vlib_increment_combined_counter (&ipsec_sa_counters, thread_index, |
| 722 | current_sa_index, current_sa_packets, |
| 723 | current_sa_bytes); |
Filip Tehlar | efcad1a | 2020-02-04 09:36:04 +0000 | [diff] [blame] | 724 | |
Damjan Marion | c59b9a2 | 2019-03-19 15:38:40 +0100 | [diff] [blame] | 725 | esp_process_ops (vm, node, ptd->crypto_ops, bufs, nexts); |
Filip Tehlar | efcad1a | 2020-02-04 09:36:04 +0000 | [diff] [blame] | 726 | esp_process_chained_ops (vm, node, ptd->chained_crypto_ops, bufs, nexts, |
| 727 | ptd->chunks); |
| 728 | |
Damjan Marion | c59b9a2 | 2019-03-19 15:38:40 +0100 | [diff] [blame] | 729 | esp_process_ops (vm, node, ptd->integ_ops, bufs, nexts); |
Filip Tehlar | efcad1a | 2020-02-04 09:36:04 +0000 | [diff] [blame] | 730 | esp_process_chained_ops (vm, node, ptd->chained_integ_ops, bufs, nexts, |
| 731 | ptd->chunks); |
Damjan Marion | c59b9a2 | 2019-03-19 15:38:40 +0100 | [diff] [blame] | 732 | |
| 733 | vlib_node_increment_counter (vm, node->node_index, |
| 734 | ESP_ENCRYPT_ERROR_RX_PKTS, frame->n_vectors); |
| 735 | |
| 736 | vlib_buffer_enqueue_to_next (vm, node, from, nexts, frame->n_vectors); |
| 737 | return frame->n_vectors; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 738 | } |
| 739 | |
Klement Sekera | b8f3544 | 2018-10-29 13:38:19 +0100 | [diff] [blame] | 740 | VLIB_NODE_FN (esp4_encrypt_node) (vlib_main_t * vm, |
| 741 | vlib_node_runtime_t * node, |
| 742 | vlib_frame_t * from_frame) |
Klement Sekera | be5a5dd | 2018-10-09 16:05:48 +0200 | [diff] [blame] | 743 | { |
Neale Ranns | 25edf14 | 2019-03-22 08:12:48 +0000 | [diff] [blame] | 744 | return esp_encrypt_inline (vm, node, from_frame, 0 /* is_ip6 */ , 0); |
Klement Sekera | be5a5dd | 2018-10-09 16:05:48 +0200 | [diff] [blame] | 745 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 746 | |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 747 | /* *INDENT-OFF* */ |
Klement Sekera | be5a5dd | 2018-10-09 16:05:48 +0200 | [diff] [blame] | 748 | VLIB_REGISTER_NODE (esp4_encrypt_node) = { |
Klement Sekera | be5a5dd | 2018-10-09 16:05:48 +0200 | [diff] [blame] | 749 | .name = "esp4-encrypt", |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 750 | .vector_size = sizeof (u32), |
| 751 | .format_trace = format_esp_encrypt_trace, |
| 752 | .type = VLIB_NODE_TYPE_INTERNAL, |
| 753 | |
| 754 | .n_errors = ARRAY_LEN(esp_encrypt_error_strings), |
| 755 | .error_strings = esp_encrypt_error_strings, |
| 756 | |
| 757 | .n_next_nodes = ESP_ENCRYPT_N_NEXT, |
| 758 | .next_nodes = { |
Neale Ranns | f62a8c0 | 2019-04-02 08:13:33 +0000 | [diff] [blame] | 759 | [ESP_ENCRYPT_NEXT_DROP] = "ip4-drop", |
| 760 | [ESP_ENCRYPT_NEXT_HANDOFF] = "esp4-encrypt-handoff", |
| 761 | [ESP_ENCRYPT_NEXT_INTERFACE_OUTPUT] = "interface-output", |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 762 | }, |
| 763 | }; |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 764 | /* *INDENT-ON* */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 765 | |
Klement Sekera | b8f3544 | 2018-10-29 13:38:19 +0100 | [diff] [blame] | 766 | VLIB_NODE_FN (esp6_encrypt_node) (vlib_main_t * vm, |
| 767 | vlib_node_runtime_t * node, |
| 768 | vlib_frame_t * from_frame) |
Klement Sekera | be5a5dd | 2018-10-09 16:05:48 +0200 | [diff] [blame] | 769 | { |
Neale Ranns | 25edf14 | 2019-03-22 08:12:48 +0000 | [diff] [blame] | 770 | return esp_encrypt_inline (vm, node, from_frame, 1 /* is_ip6 */ , 0); |
Klement Sekera | be5a5dd | 2018-10-09 16:05:48 +0200 | [diff] [blame] | 771 | } |
| 772 | |
| 773 | /* *INDENT-OFF* */ |
| 774 | VLIB_REGISTER_NODE (esp6_encrypt_node) = { |
Klement Sekera | be5a5dd | 2018-10-09 16:05:48 +0200 | [diff] [blame] | 775 | .name = "esp6-encrypt", |
| 776 | .vector_size = sizeof (u32), |
| 777 | .format_trace = format_esp_encrypt_trace, |
| 778 | .type = VLIB_NODE_TYPE_INTERNAL, |
| 779 | |
| 780 | .n_errors = ARRAY_LEN(esp_encrypt_error_strings), |
| 781 | .error_strings = esp_encrypt_error_strings, |
| 782 | |
| 783 | .n_next_nodes = ESP_ENCRYPT_N_NEXT, |
| 784 | .next_nodes = { |
Neale Ranns | f62a8c0 | 2019-04-02 08:13:33 +0000 | [diff] [blame] | 785 | [ESP_ENCRYPT_NEXT_DROP] = "ip6-drop", |
| 786 | [ESP_ENCRYPT_NEXT_HANDOFF] = "esp6-encrypt-handoff", |
| 787 | [ESP_ENCRYPT_NEXT_INTERFACE_OUTPUT] = "interface-output", |
Klement Sekera | be5a5dd | 2018-10-09 16:05:48 +0200 | [diff] [blame] | 788 | }, |
| 789 | }; |
| 790 | /* *INDENT-ON* */ |
| 791 | |
Neale Ranns | 25edf14 | 2019-03-22 08:12:48 +0000 | [diff] [blame] | 792 | VLIB_NODE_FN (esp4_encrypt_tun_node) (vlib_main_t * vm, |
| 793 | vlib_node_runtime_t * node, |
| 794 | vlib_frame_t * from_frame) |
| 795 | { |
| 796 | return esp_encrypt_inline (vm, node, from_frame, 0 /* is_ip6 */ , 1); |
| 797 | } |
| 798 | |
| 799 | /* *INDENT-OFF* */ |
| 800 | VLIB_REGISTER_NODE (esp4_encrypt_tun_node) = { |
| 801 | .name = "esp4-encrypt-tun", |
| 802 | .vector_size = sizeof (u32), |
| 803 | .format_trace = format_esp_encrypt_trace, |
| 804 | .type = VLIB_NODE_TYPE_INTERNAL, |
| 805 | |
| 806 | .n_errors = ARRAY_LEN(esp_encrypt_error_strings), |
| 807 | .error_strings = esp_encrypt_error_strings, |
Neale Ranns | d7603d9 | 2019-03-28 08:56:10 +0000 | [diff] [blame] | 808 | |
Neale Ranns | f62a8c0 | 2019-04-02 08:13:33 +0000 | [diff] [blame] | 809 | .n_next_nodes = ESP_ENCRYPT_N_NEXT, |
Neale Ranns | d7603d9 | 2019-03-28 08:56:10 +0000 | [diff] [blame] | 810 | .next_nodes = { |
| 811 | [ESP_ENCRYPT_NEXT_DROP] = "ip4-drop", |
Neale Ranns | 4a56f4e | 2019-12-23 04:10:25 +0000 | [diff] [blame] | 812 | [ESP_ENCRYPT_NEXT_HANDOFF] = "esp4-encrypt-tun-handoff", |
Neale Ranns | f62a8c0 | 2019-04-02 08:13:33 +0000 | [diff] [blame] | 813 | [ESP_ENCRYPT_NEXT_INTERFACE_OUTPUT] = "error-drop", |
Neale Ranns | d7603d9 | 2019-03-28 08:56:10 +0000 | [diff] [blame] | 814 | }, |
Neale Ranns | 25edf14 | 2019-03-22 08:12:48 +0000 | [diff] [blame] | 815 | }; |
| 816 | |
| 817 | VNET_FEATURE_INIT (esp4_encrypt_tun_feat_node, static) = |
| 818 | { |
| 819 | .arc_name = "ip4-output", |
| 820 | .node_name = "esp4-encrypt-tun", |
Neale Ranns | ea5bb77 | 2019-04-02 05:43:34 -0700 | [diff] [blame] | 821 | .runs_before = VNET_FEATURES ("adj-midchain-tx"), |
Neale Ranns | 25edf14 | 2019-03-22 08:12:48 +0000 | [diff] [blame] | 822 | }; |
Neale Ranns | c87b66c | 2019-02-07 07:26:12 -0800 | [diff] [blame] | 823 | |
Neale Ranns | b325983 | 2019-09-27 13:32:02 +0000 | [diff] [blame] | 824 | VNET_FEATURE_INIT (esp6o4_encrypt_tun_feat_node, static) = |
| 825 | { |
| 826 | .arc_name = "ip6-output", |
| 827 | .node_name = "esp4-encrypt-tun", |
| 828 | .runs_before = VNET_FEATURES ("adj-midchain-tx"), |
| 829 | }; |
| 830 | |
Neale Ranns | c87b66c | 2019-02-07 07:26:12 -0800 | [diff] [blame] | 831 | VNET_FEATURE_INIT (esp4_ethernet_encrypt_tun_feat_node, static) = |
| 832 | { |
| 833 | .arc_name = "ethernet-output", |
| 834 | .node_name = "esp4-encrypt-tun", |
| 835 | .runs_before = VNET_FEATURES ("adj-midchain-tx", "adj-midchain-tx-no-count"), |
| 836 | }; |
Neale Ranns | 25edf14 | 2019-03-22 08:12:48 +0000 | [diff] [blame] | 837 | /* *INDENT-ON* */ |
| 838 | |
| 839 | VLIB_NODE_FN (esp6_encrypt_tun_node) (vlib_main_t * vm, |
| 840 | vlib_node_runtime_t * node, |
| 841 | vlib_frame_t * from_frame) |
| 842 | { |
| 843 | return esp_encrypt_inline (vm, node, from_frame, 1 /* is_ip6 */ , 1); |
| 844 | } |
| 845 | |
| 846 | /* *INDENT-OFF* */ |
| 847 | VLIB_REGISTER_NODE (esp6_encrypt_tun_node) = { |
| 848 | .name = "esp6-encrypt-tun", |
| 849 | .vector_size = sizeof (u32), |
| 850 | .format_trace = format_esp_encrypt_trace, |
| 851 | .type = VLIB_NODE_TYPE_INTERNAL, |
| 852 | |
| 853 | .n_errors = ARRAY_LEN(esp_encrypt_error_strings), |
| 854 | .error_strings = esp_encrypt_error_strings, |
Neale Ranns | d7603d9 | 2019-03-28 08:56:10 +0000 | [diff] [blame] | 855 | |
Neale Ranns | f62a8c0 | 2019-04-02 08:13:33 +0000 | [diff] [blame] | 856 | .n_next_nodes = ESP_ENCRYPT_N_NEXT, |
Neale Ranns | d7603d9 | 2019-03-28 08:56:10 +0000 | [diff] [blame] | 857 | .next_nodes = { |
| 858 | [ESP_ENCRYPT_NEXT_DROP] = "ip6-drop", |
Neale Ranns | 4a56f4e | 2019-12-23 04:10:25 +0000 | [diff] [blame] | 859 | [ESP_ENCRYPT_NEXT_HANDOFF] = "esp6-encrypt-tun-handoff", |
Neale Ranns | f62a8c0 | 2019-04-02 08:13:33 +0000 | [diff] [blame] | 860 | [ESP_ENCRYPT_NEXT_INTERFACE_OUTPUT] = "error-drop", |
Neale Ranns | d7603d9 | 2019-03-28 08:56:10 +0000 | [diff] [blame] | 861 | }, |
Neale Ranns | 25edf14 | 2019-03-22 08:12:48 +0000 | [diff] [blame] | 862 | }; |
| 863 | |
| 864 | VNET_FEATURE_INIT (esp6_encrypt_tun_feat_node, static) = |
| 865 | { |
| 866 | .arc_name = "ip6-output", |
| 867 | .node_name = "esp6-encrypt-tun", |
Neale Ranns | ea5bb77 | 2019-04-02 05:43:34 -0700 | [diff] [blame] | 868 | .runs_before = VNET_FEATURES ("adj-midchain-tx"), |
Neale Ranns | 25edf14 | 2019-03-22 08:12:48 +0000 | [diff] [blame] | 869 | }; |
Neale Ranns | b325983 | 2019-09-27 13:32:02 +0000 | [diff] [blame] | 870 | |
| 871 | VNET_FEATURE_INIT (esp4o6_encrypt_tun_feat_node, static) = |
| 872 | { |
| 873 | .arc_name = "ip4-output", |
| 874 | .node_name = "esp6-encrypt-tun", |
| 875 | .runs_before = VNET_FEATURES ("adj-midchain-tx"), |
| 876 | }; |
| 877 | |
Neale Ranns | 25edf14 | 2019-03-22 08:12:48 +0000 | [diff] [blame] | 878 | /* *INDENT-ON* */ |
| 879 | |
Matthew Smith | 401aedf | 2019-07-08 14:45:04 -0500 | [diff] [blame] | 880 | typedef struct |
| 881 | { |
| 882 | u32 sa_index; |
| 883 | } esp_no_crypto_trace_t; |
| 884 | |
| 885 | static u8 * |
| 886 | format_esp_no_crypto_trace (u8 * s, va_list * args) |
| 887 | { |
| 888 | CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *); |
| 889 | CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *); |
| 890 | esp_no_crypto_trace_t *t = va_arg (*args, esp_no_crypto_trace_t *); |
| 891 | |
| 892 | s = format (s, "esp-no-crypto: sa-index %u", t->sa_index); |
| 893 | |
| 894 | return s; |
| 895 | } |
| 896 | |
| 897 | enum |
| 898 | { |
| 899 | ESP_NO_CRYPTO_NEXT_DROP, |
| 900 | ESP_NO_CRYPTO_N_NEXT, |
| 901 | }; |
| 902 | |
| 903 | enum |
| 904 | { |
| 905 | ESP_NO_CRYPTO_ERROR_RX_PKTS, |
| 906 | }; |
| 907 | |
| 908 | static char *esp_no_crypto_error_strings[] = { |
| 909 | "Outbound ESP packets received", |
| 910 | }; |
| 911 | |
| 912 | always_inline uword |
| 913 | esp_no_crypto_inline (vlib_main_t * vm, vlib_node_runtime_t * node, |
| 914 | vlib_frame_t * frame) |
| 915 | { |
| 916 | vlib_buffer_t *bufs[VLIB_FRAME_SIZE], **b = bufs; |
| 917 | u16 nexts[VLIB_FRAME_SIZE], *next = nexts; |
| 918 | u32 *from = vlib_frame_vector_args (frame); |
| 919 | u32 n_left = frame->n_vectors; |
| 920 | |
| 921 | vlib_get_buffers (vm, from, b, n_left); |
| 922 | |
| 923 | while (n_left > 0) |
| 924 | { |
| 925 | u32 next0; |
| 926 | u32 sa_index0; |
| 927 | |
| 928 | /* packets are always going to be dropped, but get the sa_index */ |
| 929 | sa_index0 = *(u32 *) vnet_feature_next_with_data (&next0, b[0], |
| 930 | sizeof (sa_index0)); |
| 931 | |
| 932 | next[0] = ESP_NO_CRYPTO_NEXT_DROP; |
| 933 | |
| 934 | if (PREDICT_FALSE (b[0]->flags & VLIB_BUFFER_IS_TRACED)) |
| 935 | { |
| 936 | esp_no_crypto_trace_t *tr = vlib_add_trace (vm, node, b[0], |
| 937 | sizeof (*tr)); |
| 938 | tr->sa_index = sa_index0; |
| 939 | } |
| 940 | |
| 941 | n_left -= 1; |
| 942 | next += 1; |
| 943 | b += 1; |
| 944 | } |
| 945 | |
| 946 | vlib_node_increment_counter (vm, node->node_index, |
| 947 | ESP_NO_CRYPTO_ERROR_RX_PKTS, frame->n_vectors); |
| 948 | |
| 949 | vlib_buffer_enqueue_to_next (vm, node, from, nexts, frame->n_vectors); |
| 950 | |
| 951 | return frame->n_vectors; |
| 952 | } |
| 953 | |
| 954 | VLIB_NODE_FN (esp4_no_crypto_tun_node) (vlib_main_t * vm, |
| 955 | vlib_node_runtime_t * node, |
| 956 | vlib_frame_t * from_frame) |
| 957 | { |
| 958 | return esp_no_crypto_inline (vm, node, from_frame); |
| 959 | } |
| 960 | |
| 961 | /* *INDENT-OFF* */ |
| 962 | VLIB_REGISTER_NODE (esp4_no_crypto_tun_node) = |
| 963 | { |
| 964 | .name = "esp4-no-crypto", |
| 965 | .vector_size = sizeof (u32), |
| 966 | .format_trace = format_esp_no_crypto_trace, |
| 967 | .n_errors = ARRAY_LEN(esp_no_crypto_error_strings), |
| 968 | .error_strings = esp_no_crypto_error_strings, |
| 969 | .n_next_nodes = ESP_NO_CRYPTO_N_NEXT, |
| 970 | .next_nodes = { |
| 971 | [ESP_NO_CRYPTO_NEXT_DROP] = "ip4-drop", |
| 972 | }, |
| 973 | }; |
| 974 | |
| 975 | VNET_FEATURE_INIT (esp4_no_crypto_tun_feat_node, static) = |
| 976 | { |
| 977 | .arc_name = "ip4-output", |
| 978 | .node_name = "esp4-no-crypto", |
| 979 | .runs_before = VNET_FEATURES ("adj-midchain-tx"), |
| 980 | }; |
| 981 | |
| 982 | VLIB_NODE_FN (esp6_no_crypto_tun_node) (vlib_main_t * vm, |
| 983 | vlib_node_runtime_t * node, |
| 984 | vlib_frame_t * from_frame) |
| 985 | { |
| 986 | return esp_no_crypto_inline (vm, node, from_frame); |
| 987 | } |
| 988 | |
| 989 | /* *INDENT-OFF* */ |
| 990 | VLIB_REGISTER_NODE (esp6_no_crypto_tun_node) = |
| 991 | { |
| 992 | .name = "esp6-no-crypto", |
| 993 | .vector_size = sizeof (u32), |
| 994 | .format_trace = format_esp_no_crypto_trace, |
| 995 | .n_errors = ARRAY_LEN(esp_no_crypto_error_strings), |
| 996 | .error_strings = esp_no_crypto_error_strings, |
| 997 | .n_next_nodes = ESP_NO_CRYPTO_N_NEXT, |
| 998 | .next_nodes = { |
| 999 | [ESP_NO_CRYPTO_NEXT_DROP] = "ip6-drop", |
| 1000 | }, |
| 1001 | }; |
| 1002 | |
| 1003 | VNET_FEATURE_INIT (esp6_no_crypto_tun_feat_node, static) = |
| 1004 | { |
| 1005 | .arc_name = "ip6-output", |
| 1006 | .node_name = "esp6-no-crypto", |
| 1007 | .runs_before = VNET_FEATURES ("adj-midchain-tx"), |
| 1008 | }; |
| 1009 | /* *INDENT-ON* */ |
| 1010 | |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 1011 | /* |
| 1012 | * fd.io coding-style-patch-verification: ON |
| 1013 | * |
| 1014 | * Local Variables: |
| 1015 | * eval: (c-set-style "gnu") |
| 1016 | * End: |
| 1017 | */ |