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