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> |
| 26 | #include <vnet/ipsec/esp.h> |
| 27 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 28 | #define foreach_esp_encrypt_next \ |
| 29 | _(DROP, "error-drop") \ |
Sergio Gonzalez Monroy | 1b6f902 | 2016-12-12 10:37:49 +0000 | [diff] [blame] | 30 | _(IP4_LOOKUP, "ip4-lookup") \ |
| 31 | _(IP6_LOOKUP, "ip6-lookup") \ |
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)") \ |
| 46 | _(CHAINED_BUFFER, "chained buffers (packet dropped)") \ |
| 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, |
| 96 | u16 buffer_data_size) |
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 | |
Damjan Marion | c59b9a2 | 2019-03-19 15:38:40 +0100 | [diff] [blame] | 103 | u16 min_length = b->current_length + sizeof (esp_footer_t); |
| 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) + |
| 107 | new_length - sizeof (esp_footer_t)); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 108 | |
Filip Tehlar | 8cdb1a0 | 2019-11-18 22:21:37 +0000 | [diff] [blame] | 109 | if (b->current_data + new_length + icv_sz > buffer_data_size) |
| 110 | { |
| 111 | b->error = node->errors[ESP_ENCRYPT_ERROR_NO_TRAILER_SPACE]; |
| 112 | next[0] = ESP_ENCRYPT_NEXT_DROP; |
| 113 | return 0; |
| 114 | } |
| 115 | |
Damjan Marion | c59b9a2 | 2019-03-19 15:38:40 +0100 | [diff] [blame] | 116 | if (pad_bytes) |
Benoît Ganne | 4505f01 | 2019-12-07 09:14:27 -0700 | [diff] [blame] | 117 | { |
| 118 | ASSERT (pad_bytes <= ESP_MAX_BLOCK_SIZE); |
| 119 | pad_bytes = clib_min (ESP_MAX_BLOCK_SIZE, pad_bytes); |
| 120 | clib_memcpy_fast ((u8 *) f - pad_bytes, pad_data, pad_bytes); |
| 121 | } |
Damjan Marion | c59b9a2 | 2019-03-19 15:38:40 +0100 | [diff] [blame] | 122 | |
| 123 | f->pad_length = pad_bytes; |
| 124 | b->current_length = new_length + icv_sz; |
| 125 | return &f->next_header; |
| 126 | } |
| 127 | |
| 128 | static_always_inline void |
| 129 | esp_update_ip4_hdr (ip4_header_t * ip4, u16 len, int is_transport, int is_udp) |
| 130 | { |
Neale Ranns | 1b582b8 | 2019-04-18 19:49:13 -0700 | [diff] [blame] | 131 | ip_csum_t sum; |
| 132 | u16 old_len; |
| 133 | |
| 134 | len = clib_net_to_host_u16 (len); |
| 135 | old_len = ip4->length; |
Damjan Marion | c59b9a2 | 2019-03-19 15:38:40 +0100 | [diff] [blame] | 136 | |
| 137 | if (is_transport) |
| 138 | { |
| 139 | u8 prot = is_udp ? IP_PROTOCOL_UDP : IP_PROTOCOL_IPSEC_ESP; |
Damjan Marion | c59b9a2 | 2019-03-19 15:38:40 +0100 | [diff] [blame] | 140 | |
Neale Ranns | 1b582b8 | 2019-04-18 19:49:13 -0700 | [diff] [blame] | 141 | sum = ip_csum_update (ip4->checksum, ip4->protocol, |
| 142 | prot, ip4_header_t, protocol); |
| 143 | ip4->protocol = prot; |
| 144 | |
| 145 | sum = ip_csum_update (sum, old_len, len, ip4_header_t, length); |
| 146 | } |
| 147 | else |
| 148 | sum = ip_csum_update (ip4->checksum, old_len, len, ip4_header_t, length); |
| 149 | |
| 150 | ip4->length = len; |
Damjan Marion | c59b9a2 | 2019-03-19 15:38:40 +0100 | [diff] [blame] | 151 | ip4->checksum = ip_csum_fold (sum); |
| 152 | } |
| 153 | |
| 154 | static_always_inline void |
| 155 | esp_fill_udp_hdr (ipsec_sa_t * sa, udp_header_t * udp, u16 len) |
| 156 | { |
| 157 | clib_memcpy_fast (udp, &sa->udp_hdr, sizeof (udp_header_t)); |
| 158 | udp->length = clib_net_to_host_u16 (len); |
| 159 | } |
| 160 | |
| 161 | static_always_inline u8 |
| 162 | ext_hdr_is_pre_esp (u8 nexthdr) |
| 163 | { |
| 164 | #ifdef CLIB_HAVE_VEC128 |
| 165 | static const u8x16 ext_hdr_types = { |
| 166 | IP_PROTOCOL_IP6_HOP_BY_HOP_OPTIONS, |
| 167 | IP_PROTOCOL_IPV6_ROUTE, |
| 168 | IP_PROTOCOL_IPV6_FRAGMENTATION, |
| 169 | }; |
| 170 | |
| 171 | return !u8x16_is_all_zero (ext_hdr_types == u8x16_splat (nexthdr)); |
| 172 | #else |
| 173 | return ((nexthdr ^ IP_PROTOCOL_IP6_HOP_BY_HOP_OPTIONS) | |
| 174 | (nexthdr ^ IP_PROTOCOL_IPV6_ROUTE) | |
| 175 | (nexthdr ^ IP_PROTOCOL_IPV6_FRAGMENTATION) != 0); |
| 176 | #endif |
| 177 | } |
| 178 | |
| 179 | static_always_inline u8 |
| 180 | esp_get_ip6_hdr_len (ip6_header_t * ip6) |
| 181 | { |
| 182 | /* this code assumes that HbH, route and frag headers will be before |
| 183 | others, if that is not the case, they will end up encrypted */ |
| 184 | |
| 185 | u8 len = sizeof (ip6_header_t); |
| 186 | ip6_ext_header_t *p; |
| 187 | |
| 188 | /* if next packet doesn't have ext header */ |
| 189 | if (ext_hdr_is_pre_esp (ip6->protocol) == 0) |
| 190 | return len; |
| 191 | |
| 192 | p = (void *) (ip6 + 1); |
| 193 | len += ip6_ext_header_len (p); |
| 194 | |
| 195 | while (ext_hdr_is_pre_esp (p->next_hdr)) |
| 196 | { |
| 197 | len += ip6_ext_header_len (p); |
| 198 | p = ip6_ext_next_header (p); |
| 199 | } |
| 200 | |
| 201 | return len; |
| 202 | } |
| 203 | |
Damjan Marion | c59b9a2 | 2019-03-19 15:38:40 +0100 | [diff] [blame] | 204 | static_always_inline void |
| 205 | esp_process_ops (vlib_main_t * vm, vlib_node_runtime_t * node, |
| 206 | vnet_crypto_op_t * ops, vlib_buffer_t * b[], u16 * nexts) |
| 207 | { |
| 208 | u32 n_fail, n_ops = vec_len (ops); |
| 209 | vnet_crypto_op_t *op = ops; |
| 210 | |
| 211 | if (n_ops == 0) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 212 | return; |
| 213 | |
Damjan Marion | c59b9a2 | 2019-03-19 15:38:40 +0100 | [diff] [blame] | 214 | n_fail = n_ops - vnet_crypto_process_ops (vm, op, n_ops); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 215 | |
Damjan Marion | c59b9a2 | 2019-03-19 15:38:40 +0100 | [diff] [blame] | 216 | while (n_fail) |
| 217 | { |
| 218 | ASSERT (op - ops < n_ops); |
| 219 | |
| 220 | if (op->status != VNET_CRYPTO_OP_STATUS_COMPLETED) |
| 221 | { |
| 222 | u32 bi = op->user_data; |
| 223 | b[bi]->error = node->errors[ESP_ENCRYPT_ERROR_CRYPTO_ENGINE_ERROR]; |
| 224 | nexts[bi] = ESP_ENCRYPT_NEXT_DROP; |
| 225 | n_fail--; |
| 226 | } |
| 227 | op++; |
| 228 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 229 | } |
| 230 | |
Damjan Marion | d97918e | 2019-04-25 18:28:31 +0200 | [diff] [blame] | 231 | typedef struct |
| 232 | { |
| 233 | u32 salt; |
| 234 | u64 iv; |
| 235 | } __clib_packed esp_gcm_nonce_t; |
| 236 | |
| 237 | STATIC_ASSERT_SIZEOF (esp_gcm_nonce_t, 12); |
| 238 | |
Klement Sekera | be5a5dd | 2018-10-09 16:05:48 +0200 | [diff] [blame] | 239 | always_inline uword |
Damjan Marion | c59b9a2 | 2019-03-19 15:38:40 +0100 | [diff] [blame] | 240 | esp_encrypt_inline (vlib_main_t * vm, vlib_node_runtime_t * node, |
Neale Ranns | 25edf14 | 2019-03-22 08:12:48 +0000 | [diff] [blame] | 241 | vlib_frame_t * frame, int is_ip6, int is_tun) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 242 | { |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 243 | ipsec_main_t *im = &ipsec_main; |
Damjan Marion | c59b9a2 | 2019-03-19 15:38:40 +0100 | [diff] [blame] | 244 | ipsec_per_thread_data_t *ptd = vec_elt_at_index (im->ptd, vm->thread_index); |
| 245 | u32 *from = vlib_frame_vector_args (frame); |
| 246 | u32 n_left = frame->n_vectors; |
| 247 | vlib_buffer_t *bufs[VLIB_FRAME_SIZE], **b = bufs; |
Damjan Marion | c98275f | 2019-03-06 14:05:01 +0100 | [diff] [blame] | 248 | u16 nexts[VLIB_FRAME_SIZE], *next = nexts; |
Damjan Marion | d97918e | 2019-04-25 18:28:31 +0200 | [diff] [blame] | 249 | esp_gcm_nonce_t nonces[VLIB_FRAME_SIZE], *nonce = nonces; |
Damjan Marion | c59b9a2 | 2019-03-19 15:38:40 +0100 | [diff] [blame] | 250 | u32 thread_index = vm->thread_index; |
| 251 | u16 buffer_data_size = vlib_buffer_get_default_data_size (vm); |
| 252 | u32 current_sa_index = ~0, current_sa_packets = 0; |
| 253 | u32 current_sa_bytes = 0, spi = 0; |
| 254 | u8 block_sz = 0, iv_sz = 0, icv_sz = 0; |
| 255 | ipsec_sa_t *sa0 = 0; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 256 | |
Damjan Marion | c59b9a2 | 2019-03-19 15:38:40 +0100 | [diff] [blame] | 257 | vlib_get_buffers (vm, from, b, n_left); |
| 258 | vec_reset_length (ptd->crypto_ops); |
| 259 | vec_reset_length (ptd->integ_ops); |
| 260 | |
| 261 | while (n_left > 0) |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 262 | { |
Zhiyong Yang | 1cff643 | 2019-04-30 05:33:53 -0400 | [diff] [blame] | 263 | u32 sa_index0; |
Damjan Marion | c59b9a2 | 2019-03-19 15:38:40 +0100 | [diff] [blame] | 264 | dpo_id_t *dpo; |
| 265 | esp_header_t *esp; |
| 266 | u8 *payload, *next_hdr_ptr; |
| 267 | u16 payload_len; |
| 268 | u32 hdr_len; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 269 | |
Damjan Marion | c59b9a2 | 2019-03-19 15:38:40 +0100 | [diff] [blame] | 270 | if (n_left > 2) |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 271 | { |
Damjan Marion | c59b9a2 | 2019-03-19 15:38:40 +0100 | [diff] [blame] | 272 | u8 *p; |
| 273 | vlib_prefetch_buffer_header (b[2], LOAD); |
| 274 | p = vlib_buffer_get_current (b[1]); |
| 275 | CLIB_PREFETCH (p, CLIB_CACHE_LINE_BYTES, LOAD); |
| 276 | p -= CLIB_CACHE_LINE_BYTES; |
| 277 | CLIB_PREFETCH (p, CLIB_CACHE_LINE_BYTES, LOAD); |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 278 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 279 | |
Neale Ranns | 25edf14 | 2019-03-22 08:12:48 +0000 | [diff] [blame] | 280 | if (is_tun) |
| 281 | { |
| 282 | /* we are on a ipsec tunnel's feature arc */ |
| 283 | u32 next0; |
| 284 | sa_index0 = *(u32 *) vnet_feature_next_with_data (&next0, b[0], |
| 285 | sizeof |
| 286 | (sa_index0)); |
| 287 | next[0] = next0; |
| 288 | } |
| 289 | else |
| 290 | sa_index0 = vnet_buffer (b[0])->ipsec.sad_index; |
| 291 | |
| 292 | if (sa_index0 != current_sa_index) |
Damjan Marion | c59b9a2 | 2019-03-19 15:38:40 +0100 | [diff] [blame] | 293 | { |
Damjan Marion | 21f265f | 2019-06-05 15:45:50 +0200 | [diff] [blame] | 294 | if (current_sa_packets) |
| 295 | vlib_increment_combined_counter (&ipsec_sa_counters, thread_index, |
| 296 | current_sa_index, |
| 297 | current_sa_packets, |
| 298 | current_sa_bytes); |
| 299 | current_sa_packets = current_sa_bytes = 0; |
| 300 | |
Damjan Marion | c59b9a2 | 2019-03-19 15:38:40 +0100 | [diff] [blame] | 301 | sa0 = pool_elt_at_index (im->sad, sa_index0); |
| 302 | current_sa_index = sa_index0; |
Damjan Marion | c59b9a2 | 2019-03-19 15:38:40 +0100 | [diff] [blame] | 303 | spi = clib_net_to_host_u32 (sa0->spi); |
| 304 | block_sz = sa0->crypto_block_size; |
Damjan Marion | 7c22ff7 | 2019-04-04 12:25:44 +0200 | [diff] [blame] | 305 | icv_sz = sa0->integ_icv_size; |
Damjan Marion | c59b9a2 | 2019-03-19 15:38:40 +0100 | [diff] [blame] | 306 | iv_sz = sa0->crypto_iv_size; |
| 307 | } |
| 308 | |
| 309 | if (vlib_buffer_chain_linearize (vm, b[0]) != 1) |
| 310 | { |
| 311 | b[0]->error = node->errors[ESP_ENCRYPT_ERROR_CHAINED_BUFFER]; |
| 312 | next[0] = ESP_ENCRYPT_NEXT_DROP; |
| 313 | goto trace; |
| 314 | } |
| 315 | |
| 316 | if (PREDICT_FALSE (esp_seq_advance (sa0))) |
| 317 | { |
| 318 | b[0]->error = node->errors[ESP_ENCRYPT_ERROR_SEQ_CYCLED]; |
| 319 | next[0] = ESP_ENCRYPT_NEXT_DROP; |
| 320 | goto trace; |
| 321 | } |
| 322 | |
| 323 | /* space for IV */ |
| 324 | hdr_len = iv_sz; |
| 325 | |
Damjan Marion | d709cbc | 2019-03-26 13:16:42 +0100 | [diff] [blame] | 326 | if (ipsec_sa_is_set_IS_TUNNEL (sa0)) |
Damjan Marion | c59b9a2 | 2019-03-19 15:38:40 +0100 | [diff] [blame] | 327 | { |
| 328 | payload = vlib_buffer_get_current (b[0]); |
Filip Tehlar | 8cdb1a0 | 2019-11-18 22:21:37 +0000 | [diff] [blame] | 329 | next_hdr_ptr = esp_add_footer_and_icv (b[0], block_sz, icv_sz, |
| 330 | next, node, |
| 331 | buffer_data_size); |
| 332 | if (!next_hdr_ptr) |
Damjan Marion | c59b9a2 | 2019-03-19 15:38:40 +0100 | [diff] [blame] | 333 | goto trace; |
Filip Tehlar | 8cdb1a0 | 2019-11-18 22:21:37 +0000 | [diff] [blame] | 334 | payload_len = b[0]->current_length; |
Damjan Marion | c59b9a2 | 2019-03-19 15:38:40 +0100 | [diff] [blame] | 335 | |
| 336 | /* ESP header */ |
| 337 | hdr_len += sizeof (*esp); |
| 338 | esp = (esp_header_t *) (payload - hdr_len); |
| 339 | |
| 340 | /* optional UDP header */ |
Damjan Marion | d709cbc | 2019-03-26 13:16:42 +0100 | [diff] [blame] | 341 | if (ipsec_sa_is_set_UDP_ENCAP (sa0)) |
Damjan Marion | c98275f | 2019-03-06 14:05:01 +0100 | [diff] [blame] | 342 | { |
Damjan Marion | c59b9a2 | 2019-03-19 15:38:40 +0100 | [diff] [blame] | 343 | hdr_len += sizeof (udp_header_t); |
| 344 | esp_fill_udp_hdr (sa0, (udp_header_t *) (payload - hdr_len), |
| 345 | payload_len + hdr_len); |
| 346 | } |
| 347 | |
| 348 | /* IP header */ |
Damjan Marion | d709cbc | 2019-03-26 13:16:42 +0100 | [diff] [blame] | 349 | if (ipsec_sa_is_set_IS_TUNNEL_V6 (sa0)) |
Damjan Marion | c59b9a2 | 2019-03-19 15:38:40 +0100 | [diff] [blame] | 350 | { |
| 351 | ip6_header_t *ip6; |
| 352 | u16 len = sizeof (ip6_header_t); |
| 353 | hdr_len += len; |
| 354 | ip6 = (ip6_header_t *) (payload - hdr_len); |
| 355 | clib_memcpy_fast (ip6, &sa0->ip6_hdr, len); |
Neale Ranns | 987aea8 | 2019-03-27 13:40:35 +0000 | [diff] [blame] | 356 | *next_hdr_ptr = (is_ip6 ? |
| 357 | IP_PROTOCOL_IPV6 : IP_PROTOCOL_IP_IN_IP); |
Damjan Marion | c59b9a2 | 2019-03-19 15:38:40 +0100 | [diff] [blame] | 358 | len = payload_len + hdr_len - len; |
| 359 | ip6->payload_length = clib_net_to_host_u16 (len); |
Damjan Marion | c98275f | 2019-03-06 14:05:01 +0100 | [diff] [blame] | 360 | } |
| 361 | else |
| 362 | { |
Damjan Marion | c59b9a2 | 2019-03-19 15:38:40 +0100 | [diff] [blame] | 363 | ip4_header_t *ip4; |
| 364 | u16 len = sizeof (ip4_header_t); |
| 365 | hdr_len += len; |
| 366 | ip4 = (ip4_header_t *) (payload - hdr_len); |
| 367 | clib_memcpy_fast (ip4, &sa0->ip4_hdr, len); |
Neale Ranns | 987aea8 | 2019-03-27 13:40:35 +0000 | [diff] [blame] | 368 | *next_hdr_ptr = (is_ip6 ? |
| 369 | IP_PROTOCOL_IPV6 : IP_PROTOCOL_IP_IN_IP); |
Damjan Marion | c59b9a2 | 2019-03-19 15:38:40 +0100 | [diff] [blame] | 370 | len = payload_len + hdr_len; |
| 371 | esp_update_ip4_hdr (ip4, len, /* is_transport */ 0, 0); |
Damjan Marion | c98275f | 2019-03-06 14:05:01 +0100 | [diff] [blame] | 372 | } |
Damjan Marion | c98275f | 2019-03-06 14:05:01 +0100 | [diff] [blame] | 373 | |
Neale Ranns | 72f2a3a | 2019-06-17 15:43:38 +0000 | [diff] [blame] | 374 | dpo = &sa0->dpo; |
Neale Ranns | 25edf14 | 2019-03-22 08:12:48 +0000 | [diff] [blame] | 375 | if (!is_tun) |
| 376 | { |
| 377 | next[0] = dpo->dpoi_next_node; |
| 378 | vnet_buffer (b[0])->ip.adj_index[VLIB_TX] = dpo->dpoi_index; |
| 379 | } |
Damjan Marion | c98275f | 2019-03-06 14:05:01 +0100 | [diff] [blame] | 380 | } |
Damjan Marion | c59b9a2 | 2019-03-19 15:38:40 +0100 | [diff] [blame] | 381 | else /* transport mode */ |
Damjan Marion | c98275f | 2019-03-06 14:05:01 +0100 | [diff] [blame] | 382 | { |
Damjan Marion | c59b9a2 | 2019-03-19 15:38:40 +0100 | [diff] [blame] | 383 | u8 *l2_hdr, l2_len, *ip_hdr, ip_len; |
| 384 | udp_header_t *udp = 0; |
| 385 | u8 *old_ip_hdr = vlib_buffer_get_current (b[0]); |
Damjan Marion | c98275f | 2019-03-06 14:05:01 +0100 | [diff] [blame] | 386 | |
Damjan Marion | c59b9a2 | 2019-03-19 15:38:40 +0100 | [diff] [blame] | 387 | ip_len = is_ip6 ? |
| 388 | esp_get_ip6_hdr_len ((ip6_header_t *) old_ip_hdr) : |
| 389 | ip4_header_bytes ((ip4_header_t *) old_ip_hdr); |
Damjan Marion | c98275f | 2019-03-06 14:05:01 +0100 | [diff] [blame] | 390 | |
Damjan Marion | c59b9a2 | 2019-03-19 15:38:40 +0100 | [diff] [blame] | 391 | vlib_buffer_advance (b[0], ip_len); |
| 392 | payload = vlib_buffer_get_current (b[0]); |
Filip Tehlar | 8cdb1a0 | 2019-11-18 22:21:37 +0000 | [diff] [blame] | 393 | next_hdr_ptr = esp_add_footer_and_icv (b[0], block_sz, icv_sz, |
| 394 | next, node, |
| 395 | buffer_data_size); |
| 396 | if (!next_hdr_ptr) |
Damjan Marion | c59b9a2 | 2019-03-19 15:38:40 +0100 | [diff] [blame] | 397 | goto trace; |
Filip Tehlar | 8cdb1a0 | 2019-11-18 22:21:37 +0000 | [diff] [blame] | 398 | payload_len = b[0]->current_length; |
Damjan Marion | c59b9a2 | 2019-03-19 15:38:40 +0100 | [diff] [blame] | 399 | |
| 400 | /* ESP header */ |
| 401 | hdr_len += sizeof (*esp); |
| 402 | esp = (esp_header_t *) (payload - hdr_len); |
| 403 | |
| 404 | /* optional UDP header */ |
Damjan Marion | d709cbc | 2019-03-26 13:16:42 +0100 | [diff] [blame] | 405 | if (ipsec_sa_is_set_UDP_ENCAP (sa0)) |
Damjan Marion | c98275f | 2019-03-06 14:05:01 +0100 | [diff] [blame] | 406 | { |
Damjan Marion | c59b9a2 | 2019-03-19 15:38:40 +0100 | [diff] [blame] | 407 | hdr_len += sizeof (udp_header_t); |
| 408 | udp = (udp_header_t *) (payload - hdr_len); |
Damjan Marion | c98275f | 2019-03-06 14:05:01 +0100 | [diff] [blame] | 409 | } |
| 410 | |
Damjan Marion | c59b9a2 | 2019-03-19 15:38:40 +0100 | [diff] [blame] | 411 | /* IP header */ |
| 412 | hdr_len += ip_len; |
| 413 | ip_hdr = payload - hdr_len; |
| 414 | |
| 415 | /* L2 header */ |
Neale Ranns | c87b66c | 2019-02-07 07:26:12 -0800 | [diff] [blame] | 416 | if (!is_tun) |
| 417 | { |
| 418 | l2_len = vnet_buffer (b[0])->ip.save_rewrite_length; |
| 419 | hdr_len += l2_len; |
| 420 | l2_hdr = payload - hdr_len; |
Damjan Marion | c59b9a2 | 2019-03-19 15:38:40 +0100 | [diff] [blame] | 421 | |
Neale Ranns | c87b66c | 2019-02-07 07:26:12 -0800 | [diff] [blame] | 422 | /* copy l2 and ip header */ |
| 423 | clib_memcpy_le32 (l2_hdr, old_ip_hdr - l2_len, l2_len); |
| 424 | } |
| 425 | else |
| 426 | l2_len = 0; |
| 427 | |
Damjan Marion | c59b9a2 | 2019-03-19 15:38:40 +0100 | [diff] [blame] | 428 | clib_memcpy_le64 (ip_hdr, old_ip_hdr, ip_len); |
| 429 | |
Damjan Marion | c98275f | 2019-03-06 14:05:01 +0100 | [diff] [blame] | 430 | if (is_ip6) |
Damjan Marion | c59b9a2 | 2019-03-19 15:38:40 +0100 | [diff] [blame] | 431 | { |
| 432 | ip6_header_t *ip6 = (ip6_header_t *) (ip_hdr); |
| 433 | *next_hdr_ptr = ip6->protocol; |
| 434 | ip6->protocol = IP_PROTOCOL_IPSEC_ESP; |
Neale Ranns | d207fd7 | 2019-04-18 17:18:12 -0700 | [diff] [blame] | 435 | ip6->payload_length = |
| 436 | clib_host_to_net_u16 (payload_len + hdr_len - l2_len - |
| 437 | ip_len); |
Damjan Marion | c59b9a2 | 2019-03-19 15:38:40 +0100 | [diff] [blame] | 438 | } |
Damjan Marion | c98275f | 2019-03-06 14:05:01 +0100 | [diff] [blame] | 439 | else |
Damjan Marion | c98275f | 2019-03-06 14:05:01 +0100 | [diff] [blame] | 440 | { |
Damjan Marion | c59b9a2 | 2019-03-19 15:38:40 +0100 | [diff] [blame] | 441 | u16 len; |
| 442 | ip4_header_t *ip4 = (ip4_header_t *) (ip_hdr); |
| 443 | *next_hdr_ptr = ip4->protocol; |
Neale Ranns | 1b582b8 | 2019-04-18 19:49:13 -0700 | [diff] [blame] | 444 | len = payload_len + hdr_len - l2_len; |
Damjan Marion | c59b9a2 | 2019-03-19 15:38:40 +0100 | [diff] [blame] | 445 | if (udp) |
| 446 | { |
| 447 | esp_update_ip4_hdr (ip4, len, /* is_transport */ 1, 1); |
| 448 | esp_fill_udp_hdr (sa0, udp, len - ip_len); |
| 449 | } |
| 450 | else |
| 451 | esp_update_ip4_hdr (ip4, len, /* is_transport */ 1, 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 | c87b66c | 2019-02-07 07:26:12 -0800 | [diff] [blame] | 454 | if (!is_tun) |
| 455 | next[0] = ESP_ENCRYPT_NEXT_INTERFACE_OUTPUT; |
Damjan Marion | c98275f | 2019-03-06 14:05:01 +0100 | [diff] [blame] | 456 | } |
| 457 | |
Damjan Marion | c59b9a2 | 2019-03-19 15:38:40 +0100 | [diff] [blame] | 458 | esp->spi = spi; |
| 459 | esp->seq = clib_net_to_host_u32 (sa0->seq); |
Damjan Marion | c98275f | 2019-03-06 14:05:01 +0100 | [diff] [blame] | 460 | |
Damjan Marion | 060bfb9 | 2019-03-29 13:47:54 +0100 | [diff] [blame] | 461 | if (sa0->crypto_enc_op_id) |
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 | vnet_crypto_op_t *op; |
| 464 | vec_add2_aligned (ptd->crypto_ops, op, 1, CLIB_CACHE_LINE_BYTES); |
Damjan Marion | 060bfb9 | 2019-03-29 13:47:54 +0100 | [diff] [blame] | 465 | vnet_crypto_op_init (op, sa0->crypto_enc_op_id); |
Damjan Marion | c59b9a2 | 2019-03-19 15:38:40 +0100 | [diff] [blame] | 466 | op->src = op->dst = payload; |
Damjan Marion | d1bed68 | 2019-04-24 15:20:35 +0200 | [diff] [blame] | 467 | op->key_index = sa0->crypto_key_index; |
Damjan Marion | c59b9a2 | 2019-03-19 15:38:40 +0100 | [diff] [blame] | 468 | op->len = payload_len - icv_sz; |
Damjan Marion | c59b9a2 | 2019-03-19 15:38:40 +0100 | [diff] [blame] | 469 | op->user_data = b - bufs; |
Neale Ranns | 47feb11 | 2019-04-11 15:14:07 +0000 | [diff] [blame] | 470 | |
| 471 | if (ipsec_sa_is_set_IS_AEAD (sa0)) |
| 472 | { |
| 473 | /* |
| 474 | * construct the AAD in a scratch space in front |
| 475 | * of the IP header. |
| 476 | */ |
| 477 | op->aad = payload - hdr_len - sizeof (esp_aead_t); |
| 478 | |
| 479 | esp_aad_fill (op, esp, sa0); |
| 480 | |
| 481 | op->tag = payload + op->len; |
| 482 | op->tag_len = 16; |
Damjan Marion | d97918e | 2019-04-25 18:28:31 +0200 | [diff] [blame] | 483 | |
| 484 | u64 *iv = (u64 *) (payload - iv_sz); |
| 485 | nonce->salt = sa0->salt; |
| 486 | nonce->iv = *iv = clib_host_to_net_u64 (sa0->gcm_iv_counter++); |
| 487 | op->iv = (u8 *) nonce; |
| 488 | nonce++; |
| 489 | } |
| 490 | else |
| 491 | { |
| 492 | op->iv = payload - iv_sz; |
| 493 | op->flags = VNET_CRYPTO_OP_FLAG_INIT_IV; |
Neale Ranns | 47feb11 | 2019-04-11 15:14:07 +0000 | [diff] [blame] | 494 | } |
Damjan Marion | c98275f | 2019-03-06 14:05:01 +0100 | [diff] [blame] | 495 | } |
Damjan Marion | c59b9a2 | 2019-03-19 15:38:40 +0100 | [diff] [blame] | 496 | |
Damjan Marion | 060bfb9 | 2019-03-29 13:47:54 +0100 | [diff] [blame] | 497 | if (sa0->integ_op_id) |
Damjan Marion | c98275f | 2019-03-06 14:05:01 +0100 | [diff] [blame] | 498 | { |
Damjan Marion | c59b9a2 | 2019-03-19 15:38:40 +0100 | [diff] [blame] | 499 | vnet_crypto_op_t *op; |
| 500 | vec_add2_aligned (ptd->integ_ops, op, 1, CLIB_CACHE_LINE_BYTES); |
Damjan Marion | 060bfb9 | 2019-03-29 13:47:54 +0100 | [diff] [blame] | 501 | vnet_crypto_op_init (op, sa0->integ_op_id); |
Damjan Marion | c59b9a2 | 2019-03-19 15:38:40 +0100 | [diff] [blame] | 502 | op->src = payload - iv_sz - sizeof (esp_header_t); |
Damjan Marion | 060bfb9 | 2019-03-29 13:47:54 +0100 | [diff] [blame] | 503 | op->digest = payload + payload_len - icv_sz; |
Damjan Marion | d1bed68 | 2019-04-24 15:20:35 +0200 | [diff] [blame] | 504 | op->key_index = sa0->integ_key_index; |
Damjan Marion | 060bfb9 | 2019-03-29 13:47:54 +0100 | [diff] [blame] | 505 | op->digest_len = icv_sz; |
Damjan Marion | c59b9a2 | 2019-03-19 15:38:40 +0100 | [diff] [blame] | 506 | op->len = payload_len - icv_sz + iv_sz + sizeof (esp_header_t); |
Damjan Marion | c59b9a2 | 2019-03-19 15:38:40 +0100 | [diff] [blame] | 507 | op->user_data = b - bufs; |
Damjan Marion | 1e3aa5e | 2019-03-28 10:58:59 +0100 | [diff] [blame] | 508 | if (ipsec_sa_is_set_USE_ESN (sa0)) |
Damjan Marion | c98275f | 2019-03-06 14:05:01 +0100 | [diff] [blame] | 509 | { |
Damjan Marion | c59b9a2 | 2019-03-19 15:38:40 +0100 | [diff] [blame] | 510 | u32 seq_hi = clib_net_to_host_u32 (sa0->seq_hi); |
Neale Ranns | 49e7ef6 | 2019-04-10 17:24:29 +0000 | [diff] [blame] | 511 | clib_memcpy_fast (op->digest, &seq_hi, sizeof (seq_hi)); |
Damjan Marion | c59b9a2 | 2019-03-19 15:38:40 +0100 | [diff] [blame] | 512 | op->len += sizeof (seq_hi); |
Damjan Marion | c98275f | 2019-03-06 14:05:01 +0100 | [diff] [blame] | 513 | } |
| 514 | } |
| 515 | |
Damjan Marion | c59b9a2 | 2019-03-19 15:38:40 +0100 | [diff] [blame] | 516 | vlib_buffer_advance (b[0], 0LL - hdr_len); |
Damjan Marion | c98275f | 2019-03-06 14:05:01 +0100 | [diff] [blame] | 517 | |
Damjan Marion | c59b9a2 | 2019-03-19 15:38:40 +0100 | [diff] [blame] | 518 | current_sa_packets += 1; |
| 519 | current_sa_bytes += payload_len; |
| 520 | |
| 521 | trace: |
| 522 | if (PREDICT_FALSE (b[0]->flags & VLIB_BUFFER_IS_TRACED)) |
Damjan Marion | c98275f | 2019-03-06 14:05:01 +0100 | [diff] [blame] | 523 | { |
Damjan Marion | c59b9a2 | 2019-03-19 15:38:40 +0100 | [diff] [blame] | 524 | esp_encrypt_trace_t *tr = vlib_add_trace (vm, node, b[0], |
| 525 | sizeof (*tr)); |
| 526 | tr->sa_index = sa_index0; |
| 527 | tr->spi = sa0->spi; |
Neale Ranns | 6afaae1 | 2019-07-17 15:07:14 +0000 | [diff] [blame] | 528 | tr->seq = sa0->seq; |
| 529 | tr->sa_seq_hi = sa0->seq_hi; |
Damjan Marion | d709cbc | 2019-03-26 13:16:42 +0100 | [diff] [blame] | 530 | tr->udp_encap = ipsec_sa_is_set_UDP_ENCAP (sa0); |
Damjan Marion | c59b9a2 | 2019-03-19 15:38:40 +0100 | [diff] [blame] | 531 | tr->crypto_alg = sa0->crypto_alg; |
| 532 | tr->integ_alg = sa0->integ_alg; |
Damjan Marion | c98275f | 2019-03-06 14:05:01 +0100 | [diff] [blame] | 533 | } |
Damjan Marion | c98275f | 2019-03-06 14:05:01 +0100 | [diff] [blame] | 534 | /* next */ |
Damjan Marion | c59b9a2 | 2019-03-19 15:38:40 +0100 | [diff] [blame] | 535 | n_left -= 1; |
Damjan Marion | c98275f | 2019-03-06 14:05:01 +0100 | [diff] [blame] | 536 | next += 1; |
Damjan Marion | c59b9a2 | 2019-03-19 15:38:40 +0100 | [diff] [blame] | 537 | b += 1; |
Damjan Marion | c98275f | 2019-03-06 14:05:01 +0100 | [diff] [blame] | 538 | } |
| 539 | |
Damjan Marion | c59b9a2 | 2019-03-19 15:38:40 +0100 | [diff] [blame] | 540 | vlib_increment_combined_counter (&ipsec_sa_counters, thread_index, |
| 541 | current_sa_index, current_sa_packets, |
| 542 | current_sa_bytes); |
Damjan Marion | c59b9a2 | 2019-03-19 15:38:40 +0100 | [diff] [blame] | 543 | esp_process_ops (vm, node, ptd->crypto_ops, bufs, nexts); |
| 544 | esp_process_ops (vm, node, ptd->integ_ops, bufs, nexts); |
| 545 | |
| 546 | vlib_node_increment_counter (vm, node->node_index, |
| 547 | ESP_ENCRYPT_ERROR_RX_PKTS, frame->n_vectors); |
| 548 | |
| 549 | vlib_buffer_enqueue_to_next (vm, node, from, nexts, frame->n_vectors); |
| 550 | return frame->n_vectors; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 551 | } |
| 552 | |
Klement Sekera | b8f3544 | 2018-10-29 13:38:19 +0100 | [diff] [blame] | 553 | VLIB_NODE_FN (esp4_encrypt_node) (vlib_main_t * vm, |
| 554 | vlib_node_runtime_t * node, |
| 555 | vlib_frame_t * from_frame) |
Klement Sekera | be5a5dd | 2018-10-09 16:05:48 +0200 | [diff] [blame] | 556 | { |
Neale Ranns | 25edf14 | 2019-03-22 08:12:48 +0000 | [diff] [blame] | 557 | return esp_encrypt_inline (vm, node, from_frame, 0 /* is_ip6 */ , 0); |
Klement Sekera | be5a5dd | 2018-10-09 16:05:48 +0200 | [diff] [blame] | 558 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 559 | |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 560 | /* *INDENT-OFF* */ |
Klement Sekera | be5a5dd | 2018-10-09 16:05:48 +0200 | [diff] [blame] | 561 | VLIB_REGISTER_NODE (esp4_encrypt_node) = { |
Klement Sekera | be5a5dd | 2018-10-09 16:05:48 +0200 | [diff] [blame] | 562 | .name = "esp4-encrypt", |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 563 | .vector_size = sizeof (u32), |
| 564 | .format_trace = format_esp_encrypt_trace, |
| 565 | .type = VLIB_NODE_TYPE_INTERNAL, |
| 566 | |
| 567 | .n_errors = ARRAY_LEN(esp_encrypt_error_strings), |
| 568 | .error_strings = esp_encrypt_error_strings, |
| 569 | |
| 570 | .n_next_nodes = ESP_ENCRYPT_N_NEXT, |
| 571 | .next_nodes = { |
| 572 | #define _(s,n) [ESP_ENCRYPT_NEXT_##s] = n, |
| 573 | foreach_esp_encrypt_next |
| 574 | #undef _ |
| 575 | }, |
| 576 | }; |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 577 | /* *INDENT-ON* */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 578 | |
Klement Sekera | b8f3544 | 2018-10-29 13:38:19 +0100 | [diff] [blame] | 579 | VLIB_NODE_FN (esp6_encrypt_node) (vlib_main_t * vm, |
| 580 | vlib_node_runtime_t * node, |
| 581 | vlib_frame_t * from_frame) |
Klement Sekera | be5a5dd | 2018-10-09 16:05:48 +0200 | [diff] [blame] | 582 | { |
Neale Ranns | 25edf14 | 2019-03-22 08:12:48 +0000 | [diff] [blame] | 583 | return esp_encrypt_inline (vm, node, from_frame, 1 /* is_ip6 */ , 0); |
Klement Sekera | be5a5dd | 2018-10-09 16:05:48 +0200 | [diff] [blame] | 584 | } |
| 585 | |
| 586 | /* *INDENT-OFF* */ |
| 587 | VLIB_REGISTER_NODE (esp6_encrypt_node) = { |
Klement Sekera | be5a5dd | 2018-10-09 16:05:48 +0200 | [diff] [blame] | 588 | .name = "esp6-encrypt", |
| 589 | .vector_size = sizeof (u32), |
| 590 | .format_trace = format_esp_encrypt_trace, |
| 591 | .type = VLIB_NODE_TYPE_INTERNAL, |
| 592 | |
| 593 | .n_errors = ARRAY_LEN(esp_encrypt_error_strings), |
| 594 | .error_strings = esp_encrypt_error_strings, |
| 595 | |
| 596 | .n_next_nodes = ESP_ENCRYPT_N_NEXT, |
| 597 | .next_nodes = { |
| 598 | #define _(s,n) [ESP_ENCRYPT_NEXT_##s] = n, |
| 599 | foreach_esp_encrypt_next |
| 600 | #undef _ |
| 601 | }, |
| 602 | }; |
| 603 | /* *INDENT-ON* */ |
| 604 | |
Neale Ranns | 25edf14 | 2019-03-22 08:12:48 +0000 | [diff] [blame] | 605 | VLIB_NODE_FN (esp4_encrypt_tun_node) (vlib_main_t * vm, |
| 606 | vlib_node_runtime_t * node, |
| 607 | vlib_frame_t * from_frame) |
| 608 | { |
| 609 | return esp_encrypt_inline (vm, node, from_frame, 0 /* is_ip6 */ , 1); |
| 610 | } |
| 611 | |
| 612 | /* *INDENT-OFF* */ |
| 613 | VLIB_REGISTER_NODE (esp4_encrypt_tun_node) = { |
| 614 | .name = "esp4-encrypt-tun", |
| 615 | .vector_size = sizeof (u32), |
| 616 | .format_trace = format_esp_encrypt_trace, |
| 617 | .type = VLIB_NODE_TYPE_INTERNAL, |
| 618 | |
| 619 | .n_errors = ARRAY_LEN(esp_encrypt_error_strings), |
| 620 | .error_strings = esp_encrypt_error_strings, |
Neale Ranns | d7603d9 | 2019-03-28 08:56:10 +0000 | [diff] [blame] | 621 | |
| 622 | .n_next_nodes = 1, |
| 623 | .next_nodes = { |
| 624 | [ESP_ENCRYPT_NEXT_DROP] = "ip4-drop", |
| 625 | }, |
Neale Ranns | 25edf14 | 2019-03-22 08:12:48 +0000 | [diff] [blame] | 626 | }; |
| 627 | |
| 628 | VNET_FEATURE_INIT (esp4_encrypt_tun_feat_node, static) = |
| 629 | { |
| 630 | .arc_name = "ip4-output", |
| 631 | .node_name = "esp4-encrypt-tun", |
Neale Ranns | ea5bb77 | 2019-04-02 05:43:34 -0700 | [diff] [blame] | 632 | .runs_before = VNET_FEATURES ("adj-midchain-tx"), |
Neale Ranns | 25edf14 | 2019-03-22 08:12:48 +0000 | [diff] [blame] | 633 | }; |
Neale Ranns | c87b66c | 2019-02-07 07:26:12 -0800 | [diff] [blame] | 634 | |
Neale Ranns | b325983 | 2019-09-27 13:32:02 +0000 | [diff] [blame] | 635 | VNET_FEATURE_INIT (esp6o4_encrypt_tun_feat_node, static) = |
| 636 | { |
| 637 | .arc_name = "ip6-output", |
| 638 | .node_name = "esp4-encrypt-tun", |
| 639 | .runs_before = VNET_FEATURES ("adj-midchain-tx"), |
| 640 | }; |
| 641 | |
Neale Ranns | c87b66c | 2019-02-07 07:26:12 -0800 | [diff] [blame] | 642 | VNET_FEATURE_INIT (esp4_ethernet_encrypt_tun_feat_node, static) = |
| 643 | { |
| 644 | .arc_name = "ethernet-output", |
| 645 | .node_name = "esp4-encrypt-tun", |
| 646 | .runs_before = VNET_FEATURES ("adj-midchain-tx", "adj-midchain-tx-no-count"), |
| 647 | }; |
Neale Ranns | 25edf14 | 2019-03-22 08:12:48 +0000 | [diff] [blame] | 648 | /* *INDENT-ON* */ |
| 649 | |
| 650 | VLIB_NODE_FN (esp6_encrypt_tun_node) (vlib_main_t * vm, |
| 651 | vlib_node_runtime_t * node, |
| 652 | vlib_frame_t * from_frame) |
| 653 | { |
| 654 | return esp_encrypt_inline (vm, node, from_frame, 1 /* is_ip6 */ , 1); |
| 655 | } |
| 656 | |
| 657 | /* *INDENT-OFF* */ |
| 658 | VLIB_REGISTER_NODE (esp6_encrypt_tun_node) = { |
| 659 | .name = "esp6-encrypt-tun", |
| 660 | .vector_size = sizeof (u32), |
| 661 | .format_trace = format_esp_encrypt_trace, |
| 662 | .type = VLIB_NODE_TYPE_INTERNAL, |
| 663 | |
| 664 | .n_errors = ARRAY_LEN(esp_encrypt_error_strings), |
| 665 | .error_strings = esp_encrypt_error_strings, |
Neale Ranns | d7603d9 | 2019-03-28 08:56:10 +0000 | [diff] [blame] | 666 | |
| 667 | .n_next_nodes = 1, |
| 668 | .next_nodes = { |
| 669 | [ESP_ENCRYPT_NEXT_DROP] = "ip6-drop", |
| 670 | }, |
Neale Ranns | 25edf14 | 2019-03-22 08:12:48 +0000 | [diff] [blame] | 671 | }; |
| 672 | |
| 673 | VNET_FEATURE_INIT (esp6_encrypt_tun_feat_node, static) = |
| 674 | { |
| 675 | .arc_name = "ip6-output", |
| 676 | .node_name = "esp6-encrypt-tun", |
Neale Ranns | ea5bb77 | 2019-04-02 05:43:34 -0700 | [diff] [blame] | 677 | .runs_before = VNET_FEATURES ("adj-midchain-tx"), |
Neale Ranns | 25edf14 | 2019-03-22 08:12:48 +0000 | [diff] [blame] | 678 | }; |
Neale Ranns | b325983 | 2019-09-27 13:32:02 +0000 | [diff] [blame] | 679 | |
| 680 | VNET_FEATURE_INIT (esp4o6_encrypt_tun_feat_node, static) = |
| 681 | { |
| 682 | .arc_name = "ip4-output", |
| 683 | .node_name = "esp6-encrypt-tun", |
| 684 | .runs_before = VNET_FEATURES ("adj-midchain-tx"), |
| 685 | }; |
| 686 | |
Neale Ranns | 25edf14 | 2019-03-22 08:12:48 +0000 | [diff] [blame] | 687 | /* *INDENT-ON* */ |
| 688 | |
Matthew Smith | 401aedf | 2019-07-08 14:45:04 -0500 | [diff] [blame] | 689 | typedef struct |
| 690 | { |
| 691 | u32 sa_index; |
| 692 | } esp_no_crypto_trace_t; |
| 693 | |
| 694 | static u8 * |
| 695 | format_esp_no_crypto_trace (u8 * s, va_list * args) |
| 696 | { |
| 697 | CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *); |
| 698 | CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *); |
| 699 | esp_no_crypto_trace_t *t = va_arg (*args, esp_no_crypto_trace_t *); |
| 700 | |
| 701 | s = format (s, "esp-no-crypto: sa-index %u", t->sa_index); |
| 702 | |
| 703 | return s; |
| 704 | } |
| 705 | |
| 706 | enum |
| 707 | { |
| 708 | ESP_NO_CRYPTO_NEXT_DROP, |
| 709 | ESP_NO_CRYPTO_N_NEXT, |
| 710 | }; |
| 711 | |
| 712 | enum |
| 713 | { |
| 714 | ESP_NO_CRYPTO_ERROR_RX_PKTS, |
| 715 | }; |
| 716 | |
| 717 | static char *esp_no_crypto_error_strings[] = { |
| 718 | "Outbound ESP packets received", |
| 719 | }; |
| 720 | |
| 721 | always_inline uword |
| 722 | esp_no_crypto_inline (vlib_main_t * vm, vlib_node_runtime_t * node, |
| 723 | vlib_frame_t * frame) |
| 724 | { |
| 725 | vlib_buffer_t *bufs[VLIB_FRAME_SIZE], **b = bufs; |
| 726 | u16 nexts[VLIB_FRAME_SIZE], *next = nexts; |
| 727 | u32 *from = vlib_frame_vector_args (frame); |
| 728 | u32 n_left = frame->n_vectors; |
| 729 | |
| 730 | vlib_get_buffers (vm, from, b, n_left); |
| 731 | |
| 732 | while (n_left > 0) |
| 733 | { |
| 734 | u32 next0; |
| 735 | u32 sa_index0; |
| 736 | |
| 737 | /* packets are always going to be dropped, but get the sa_index */ |
| 738 | sa_index0 = *(u32 *) vnet_feature_next_with_data (&next0, b[0], |
| 739 | sizeof (sa_index0)); |
| 740 | |
| 741 | next[0] = ESP_NO_CRYPTO_NEXT_DROP; |
| 742 | |
| 743 | if (PREDICT_FALSE (b[0]->flags & VLIB_BUFFER_IS_TRACED)) |
| 744 | { |
| 745 | esp_no_crypto_trace_t *tr = vlib_add_trace (vm, node, b[0], |
| 746 | sizeof (*tr)); |
| 747 | tr->sa_index = sa_index0; |
| 748 | } |
| 749 | |
| 750 | n_left -= 1; |
| 751 | next += 1; |
| 752 | b += 1; |
| 753 | } |
| 754 | |
| 755 | vlib_node_increment_counter (vm, node->node_index, |
| 756 | ESP_NO_CRYPTO_ERROR_RX_PKTS, frame->n_vectors); |
| 757 | |
| 758 | vlib_buffer_enqueue_to_next (vm, node, from, nexts, frame->n_vectors); |
| 759 | |
| 760 | return frame->n_vectors; |
| 761 | } |
| 762 | |
| 763 | VLIB_NODE_FN (esp4_no_crypto_tun_node) (vlib_main_t * vm, |
| 764 | vlib_node_runtime_t * node, |
| 765 | vlib_frame_t * from_frame) |
| 766 | { |
| 767 | return esp_no_crypto_inline (vm, node, from_frame); |
| 768 | } |
| 769 | |
| 770 | /* *INDENT-OFF* */ |
| 771 | VLIB_REGISTER_NODE (esp4_no_crypto_tun_node) = |
| 772 | { |
| 773 | .name = "esp4-no-crypto", |
| 774 | .vector_size = sizeof (u32), |
| 775 | .format_trace = format_esp_no_crypto_trace, |
| 776 | .n_errors = ARRAY_LEN(esp_no_crypto_error_strings), |
| 777 | .error_strings = esp_no_crypto_error_strings, |
| 778 | .n_next_nodes = ESP_NO_CRYPTO_N_NEXT, |
| 779 | .next_nodes = { |
| 780 | [ESP_NO_CRYPTO_NEXT_DROP] = "ip4-drop", |
| 781 | }, |
| 782 | }; |
| 783 | |
| 784 | VNET_FEATURE_INIT (esp4_no_crypto_tun_feat_node, static) = |
| 785 | { |
| 786 | .arc_name = "ip4-output", |
| 787 | .node_name = "esp4-no-crypto", |
| 788 | .runs_before = VNET_FEATURES ("adj-midchain-tx"), |
| 789 | }; |
| 790 | |
| 791 | VLIB_NODE_FN (esp6_no_crypto_tun_node) (vlib_main_t * vm, |
| 792 | vlib_node_runtime_t * node, |
| 793 | vlib_frame_t * from_frame) |
| 794 | { |
| 795 | return esp_no_crypto_inline (vm, node, from_frame); |
| 796 | } |
| 797 | |
| 798 | /* *INDENT-OFF* */ |
| 799 | VLIB_REGISTER_NODE (esp6_no_crypto_tun_node) = |
| 800 | { |
| 801 | .name = "esp6-no-crypto", |
| 802 | .vector_size = sizeof (u32), |
| 803 | .format_trace = format_esp_no_crypto_trace, |
| 804 | .n_errors = ARRAY_LEN(esp_no_crypto_error_strings), |
| 805 | .error_strings = esp_no_crypto_error_strings, |
| 806 | .n_next_nodes = ESP_NO_CRYPTO_N_NEXT, |
| 807 | .next_nodes = { |
| 808 | [ESP_NO_CRYPTO_NEXT_DROP] = "ip6-drop", |
| 809 | }, |
| 810 | }; |
| 811 | |
| 812 | VNET_FEATURE_INIT (esp6_no_crypto_tun_feat_node, static) = |
| 813 | { |
| 814 | .arc_name = "ip6-output", |
| 815 | .node_name = "esp6-no-crypto", |
| 816 | .runs_before = VNET_FEATURES ("adj-midchain-tx"), |
| 817 | }; |
| 818 | /* *INDENT-ON* */ |
| 819 | |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 820 | /* |
| 821 | * fd.io coding-style-patch-verification: ON |
| 822 | * |
| 823 | * Local Variables: |
| 824 | * eval: (c-set-style "gnu") |
| 825 | * End: |
| 826 | */ |